If you've ever integrated Salesforce with SSO or user provisioning tools like Azure AD, you've likely run into a common issue: Salesforce record IDs in the UI are 15 characters, but your API or integration often requires the 18-character version.
To make this easier, I wrote a small PHP snippet some years ago that converts 15-character Salesforce IDs into the 18-character version, which is safe to use in integrations.
This snippet is especially useful for mapping Azure AD attributes to Salesforce Roles/Profiles or any automation
requiring long IDs.
sf_id.php
<?php
if (isset($_GET['id']) && $_GET['id'] != null) {
header("Content-Type: application/json");
print_r(json_encode(array(
"id"=> to18char($_GET['id'])
)));
}
function to18char(string $inputId) {
$suffix = '';
for ($i = 0; $i < 3; $i++) {
$flags = 0;
for ($j = 0; $j < 5; $j++) {
$start = $i * 5 + $j;
$end = ($i * 5 + $j + 1) - $start;
$c = substr($inputId, $start, $end);
if (ctype_upper($c) && $c >= 'A' && $c <= 'Z') {
$flags = $flags + (1 << $j);
}
}
if ($flags <= 25) {
$suffix .= substr('ABCDEFGHIJKLMNOPQRSTUVWXYZ', $flags, 1);
} else {
$suffix .= substr('012345', $flags - 26, 1);
}
}
return $inputId . $suffix;
}
?>
Usage
- Save the script as a PHP file (e.g.,
sf_id.php
). - Call it via your browser or API request with a 15-character Salesforce ID:
curl https://yourdomain.com/sf_id.php?id=0013j00000abcdE
The script will return the 18-character ID in JSON format:
{
"id": "0013j00000abcdEFA"
}
This little script saved me a lot of time when working with Azure AD SSO and user provisioning for Salesforce, and it's a quick helper for anyone working with Salesforce API integrations.