ShowAll Questionssorted byDate Posted
tgk1

# Trigger to pull 18 digit Account ID

Hey everyone-

I need to create a field within the account object that holds the account records ID.  I can get the 15 digit ID very easily via a formula field, but unfortunately I need to get the full 18 digits.  Has anybody made a trigger like this in the past?  If so can you please share your code??  Thank you very much in advance.

-Tom

Shashikant Sharma

Use this example for your case in trigger

You can directly save it in formula field return type text  like using this formula :

```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)```

Ankit Arora

I have a bit simplified code I guess, please visit :

http://forceguru.blogspot.com/2010/12/how-salesforce-18-digit-id-is.html﻿

```//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 = suffix + '012345'.substring(flags-25,flags-24);
}
}

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

Thanks

Ankit Arora

tgk1

Thank you both, this is very helpful.

Ankit Arora

Cool! I would suggest you to mark the post as solution so others may get benefit.

Thanks

Ankit Arora

Rahul Sharma

Nice work Ankit,

I was just wondering how its done, could you please explain the code.

Thanks a lot bro.