Tag Archives: Force.com

Function to Calculate the 18 Character ID From 15 Character ID

public String Convert15CharTo18CharId(String id) {
    String suffix = '';
    Integer flags;

    for (integer i = 0; i < 3; i++) {
        flags = 0;
        for (Integer j = 0; j < 5; j++) {
            String c = id.substring(i * 5 + j, i * 5 + j + 1);
            //Only add to flags if c is an uppercase letter:
            if (c.toUpperCase().equals(c) && c >= 'A' && c <= 'Z') {
                flags = flags + (1 << j);
            }
        }
        if (flags <= 25) {
            suffix = suffix + 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'.substring(flags, flags + 1);
        } else {
            suffix += '012345'.substring(flags - 26, flags - 25);
        }
    }
    String convertId = id + suffix;
    //18 Digit Id with checksum
    System.debug('convertId-' + convertId);
    return 18 digitId;
}

Example:

String Id = '0019000001EJNfj';
String convertedId = Convert15CharTo18CharId(Id);

Formula Field to Convert 15 Character ID to 18 Character ID

Follow the below steps to create a formula field that will give you the 18 characters ID of the records.

  • Go to Setup | Customize | object name | click Fields
    (For Custom objects: Setup | Create | Objects | Object Name)
  • In the related list “Custom Fields & Relationships” click New.
  • Click the Formula radio button.
  • Click the Text radio button for “Formula Return Type”.
  • 6. Input the following Formula into the Formula Editor:
    CASESAFEID(Id)
  • Set Field Visibility, add/ remove from Page Layout(s).
  • Click Save.

NOTE: When dealing with record types only custom record types have ids.

PHP Function to Calculate the Salesforce 15 Character ID to 18 Charachter ID

function salesForceIdCalculate($id)
{
    $map      = implode('', array_merge(range('A', 'Z'), range(0, 9)));
    $checksum = '';
    
    foreach (str_split($id, 5) as $chunk) {
        $checksum .= substr($map, bindec(strrev(array_reduce(str_split($chunk, 1), function($carry, $item)
        {
            $carry .= (!is_numeric($item) && $item == strtoupper($item)) ? '1' : '0';
            return $carry;
        }, ''))), 1);
    }
    
    return $checksum;
}

// example
//$_15ID = '0019000001EJNfj';
//$_18ID = $_15ID. salesForceIdCalculate($_15ID);

Convert Salesforce 15 Character ID to 18 Character ID Using Javascript

function Convert15CharTo18CharId(id) {
    if (id == null) {
        return id;
    }
    //Scrub quotes from this id
    id = id.replace(/\"/g, '');
    if (id.length != 15) {
        return null;
    }
    var suffix = "";

    for (var i = 0; i < 3; i++) {
        var flags = 0;
        for (var j = 0; j < 5; j++) {
            var c = id.charAt(i * 5 + j);
            if (c >= 'A' && c < = 'Z') {
                flags += 1 << j;
            }
        }
        if (flags <= 25) {
            suffix += "ABCDEFGHIJKLMNOPQRSTUVWXYZ".charAt(flags);
        } else {
            suffix += "012345".charAt(flags - 26);
        }
    }
    return id + suffix;
}

External ID in Salesforce

An External Id is a custom field that has enable the “External ID” attribute. This field is usually references an ID from another (external) system. When a field is marked as external Id, the field will be automatically indexed.

Note: External Id is not case-sensitive – for example, “XYZ” will be matched with “xyz”. if the custom field has enable the “Unique” attribute and the case-sensitive option for that attribute is selected, uppercase and lowercase letters will not be considered identical.

Considerations:

  • External Id is an attribute that can be added to a custom field to indicate that it should be indexed and treated as an Id.
  • It is a user-defined cross-referenced field.
  • It can be created for custom field type (Text, Number or Email ONLY).
  • Each object can have up to 7 external Ids (as of Spring ’15).
  • Configure UPSERT action to traverse object relationships defined in Salesforce, but use External Id’s from legacy system to discover Salesforce record ID’s.

Benefits:

  • It helps improve Report and API SOQL performance.
  • It can be used with UPSERT DML operation to seamlessly integrate apps with other systems.
  • No need to know Salesforce record Id’s to load data.
  • Very convenient for data integrations and migrations.
  • The import wizard will detect existing records in Salesforce that have the same external ID.
  • It becomes searchable in the sidebar search.

Example: If you have an Oracle Financials system that they will be linking with Salesforce, it may be easier for them to be able to refer to the Oracle ID of account records from within Salesforce. So, You can create an external ID in Salesforce and you can load the Oracle ID into that field for each account. You can then refer to that ID field, rather than the Salesforce id.