Saturday, 10 November 2012

Difference between 15 digit Id and 18 digit Id in Salesforce

In salesforce each record Id represents a unique record within an organisation. There are two versions of every record Id in salesforce :
  • 15 digit case-sensitive version which is referenced in the UI
  • 18 digit case-insensitive version which is referenced through the API

The last 3 digits of the 18 digit ID are a checksum of the capitalizations of the first 15 characters, this ID length was created as a workaround to legacy systems which were not compatible with case-sensitive IDs.

The API will accept the 15 digit ID as input but will always return the 18 digit ID.

You can create a custom formula field that returns type text, place the code below  in the formula field , you will got an 18 digit ID that you can easily add to any report.

Id
& MID("ABCDEFGHIJKLMNOPQRSTUVWXYZ012345",(
    IF(FIND(MID(Id,1,1), "ABCDEFGHIJKLMNOPQRSTUVWXYZ")>0,1,0)
    +IF(FIND(MID(Id,2,1), "ABCDEFGHIJKLMNOPQRSTUVWXYZ")>0,2,0)
    +IF(FIND(MID(Id,3,1), "ABCDEFGHIJKLMNOPQRSTUVWXYZ")>0,4,0)
    +IF(FIND(MID(Id,4,1), "ABCDEFGHIJKLMNOPQRSTUVWXYZ")>0,8,0)
    +IF(FIND(MID(Id,5,1), "ABCDEFGHIJKLMNOPQRSTUVWXYZ")>0,16,0)
    )+1,1)
& MID("ABCDEFGHIJKLMNOPQRSTUVWXYZ012345",(
    IF(FIND(MID(Id,6,1), "ABCDEFGHIJKLMNOPQRSTUVWXYZ")>0,1,0)
    +IF(FIND(MID(Id,7,1), "ABCDEFGHIJKLMNOPQRSTUVWXYZ")>0,2,0)
    +IF(FIND(MID(Id,8,1), "ABCDEFGHIJKLMNOPQRSTUVWXYZ")>0,4,0)
    +IF(FIND(MID(Id,9,1), "ABCDEFGHIJKLMNOPQRSTUVWXYZ")>0,8,0)
    +IF(FIND(MID(Id,10,1), "ABCDEFGHIJKLMNOPQRSTUVWXYZ")>0,16,0)
    )+1,1)
& MID("ABCDEFGHIJKLMNOPQRSTUVWXYZ012345",(
    IF(FIND(MID(Id,11,1), "ABCDEFGHIJKLMNOPQRSTUVWXYZ")>0,1,0)
    +IF(FIND(MID(Id,12,1), "ABCDEFGHIJKLMNOPQRSTUVWXYZ")>0,2,0)
    +IF(FIND(MID(Id,13,1), "ABCDEFGHIJKLMNOPQRSTUVWXYZ")>0,4,0)
    +IF(FIND(MID(Id,14,1), "ABCDEFGHIJKLMNOPQRSTUVWXYZ")>0,8,0)
    +IF(FIND(MID(Id,15,1), "ABCDEFGHIJKLMNOPQRSTUVWXYZ")>0,16,0)
    )+1,1)

How to calculate the 18 Digit Id from 15 Digit Id programatically:

//Our 15 Digit Id
String id = '00570000001ZwTi' ;

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);
          }
     }

//18 Digit Id with checksum
System.debug(' ::::::: ' + id + suffix) ;

Apex Data Loader errors

1. Insert failed: inactive user
Ans. If you are trying to insert record for inactive user then this error come.

2. Entity deleted
Ans. If you are trying to insert any lookup filed of deleted record this error will come

3. Time Zone mismatch (unexpected result occur)
Ans. you should set the settings of data loader ( time zone filed) according to your sandbox or production environment. Otherwise it will compare with the previous day value of the current date day value.

Before event in trigger

* In before mode of the trigger, If you are trying to update records of the object on which you are performing the trigger operation then no need to use DML statements manually, automatically salesforce will update those records.
* If you are trying to update other object records then you should use DML statements.
--------------------
* Suppose one of the record which is not there in sObject based on that record only if you can perform operation(like inserting other records based on that record), in this case :
insert record in before mode(Trigger.isBefore) then based on that record you can insert other records in after mode(Trigger.inAfter)
--------------------

Data Loader batch size

* If the batch size is 50 then if one of the record in those 50 records failed to insert due to error all remaining records in that batch also fail.
* If you try to insert those failed records again with batch size '1' then most of the records will get insert apart from those records which causing problem really.

Labels