What are the Salesforce IDs composed of?

417    Asked by DipikaAgarwal in Salesforce , Asked on Apr 27, 2023

I remember seeing somewhere that IDs are composed of a few pieces. I always have a hard time trying to find that information when I'm looking for it. What I mean by the above is that the various places in the ID represent different things - for example the first few characters represent what type of sObject it is.

Answered by David Edmunds

Not detracting from anything above, and not being part of the SF community, here follows a PHP class for converting between type 18 and type 15 ids. Note that many people suggest merely cutting the last three characters off the type 18 id, in order to get the type 15. Since type 18 is 'case-safe' and type 15 is case-sensitive, such a stratagem is not a good idea.  Although, strictly, the 18-character Salesforce ids are not case-safe, in that case-insensitive sorting will differ for 18-character due to the digits coming after the alphabet, and the encoding bit being set for upper case. To be correctly case-safe, the encoding characters should have been in ansi-order, and the encoding bit should have been reset for uppercase). Consider the following id15s..

AAAAbAAAAbAAAAb
aaAABaaAABaaAAB
aaaAbaaaAbaaaAb
aaaabaaaabaaaab
These will sort (case-sensitive) to 1,2,3,4 - because 'A' < 'a' and 'B' < 'b'.
However, their id18 equivalents,
AAAAbAAAAbAAAAbPPP
aaAABaaAABaaAAB222
aaaAbaaaAbaaaAbIII
aaaabaaaabaaaabAAA
sorts (using case-INsensitive, as they are 'case-safe') to 2,4,3,1 - because 2 < A xss=removed>validate($idIn);
        if($this->valid) {
            $this->length = strlen($idIn);
            if($this->length == 18) {
                $this->id18 = $idIn;
            } else {
                $this->id15 = $idIn;
            }
        }
    }
    // Helper function. Use this to default to
    // The id that you wish to use.
    public function id() : ?string {
        return $this->id15();
    }
    public function id15() : ?string {
        if(is_null($this->id15) && $this->valid) {
            $this->id15 = $this->sf18to15($this->id18);
        }
        return $this->id15;
    }
    public function id18() : ?string {
        if(is_null($this->id18) && $this->valid ) {
            $this->id15 = $this->sf15to18($this->id15);
        }
        return $this->id15;
    }
    private function validate(string $id) : bool {
        if(!is_null($id)) {
            if(preg_match('/^[a-zA-Z0-9]+$/',$id)) {
                $length = strlen($id);
                if($length !== 18 && $length !== 15) {
                    $this->errs[] = "Salesforce Key Id has an incorrect length";
                    return false;
                }
                if(($id === "000000000000000") || ($id === "000000000000000AAA")) {
                    $this->errs[] = "SalesForce Empty Key Id is not acceptable";
                    return false;
                }
            } else {
                $this->errs[] = "SalesForce Key Id is not valid";
                return false;
            }
        } else {
            $this->errs[] = "SalesForce Key is null";
            return false;
        }
        return true;
    }
    private function sf15to18($str): string {
        $lib = "ABCDEFGHIJKLMNOPQRSTUVWXYZ012345";
        $retval = $str;
        foreach (str_split($str, 5) as $segment) {
            $idx = 0; $base = 1;
            $chars = str_split($segment);
            foreach($chars as $char) {
                $idx += ctype_upper($char) ? $base : 0;
                $base <<=1;
            }
            $retval .= $lib[$idx];
        }
        return $retval;
    }
    private function sf18to15($str): string {
        $retval = "";
        $idx = str_split(substr($str, -3)); //This marks upper case.
        $val = str_split(substr($str, 0, -3),5);
        for($i=0; $i < 3 xss=removed xss=removed xss=removed xss=removed xss=removed>valid;
    }
    public function errors() : array {
        return $this->errs;
    }
}
function doTest() {
    $salesForceId =
        [
            '0032400000QZbmt'=>'0032400000QZbmtAAD',
            '0032400000QZbnG'=>'0032400000QZbnGAAT',
            '0032400000QZbnZ'=>'0032400000QZbnZAAT',
            '0032400000eGqdZ'=>'0032400000eGqdZAAS'
        ];
   foreach($salesForceId as $key => $base ) {
        $x8 = new SalesForceID($key);
        $xx = $x8->id18();
        $tt = $xx === $base ? 'Y' : 'N';
            print("$key should $xx = $base $tt
");
        }
    foreach($salesForceId as $key => $base ) {
        $x8 = new SalesForceID($key);
        $xx = $x8->id15();
        $tt = $xx === $key ? 'Y' : 'N';
        print("$base should $xx = $key $tt
");
    }
}
doTest();


Your Answer

Interviews

Parent Categories