Select your language

  • Davide Gammone

            

Davide Gammone.com
How to convert Salesforce ID from 15 to 18 chars

Salesforce record ID's have 2 forms: 15 chars case sensitive, and 18 chars case insensitive. It's very important that we always use 18 chars ID for all data manipulations.

For facilitating this, Salesforce has already given an out-of-the-box formula function named CASESAFEID(id), which accepts the 15 chars ID and returns the 18 chars equivalent.

How does the conversion from 15 to 18 chars actually take place?

Paste your list of IDs into the first text area.

   

Let's consider a 15 chars record ID (for example): a0D30000001n7Pi

1. Divide the 15 chars into 3 chunks of 5 chars each:

(a0D30)-(00000)-(1n7Pi).

2. Invert the chars of each chunks:

(03D0a)-(00000)-(iP7n1)

3. For each char, give a value of 1 if that digit is in uppercase. Give a value of 0, if that char is in lowercase or a number. The 1 values are highlighted in bold for reading convenience:

(03D0a)-(00000)-(iP7n1)

(00100)-(00000)-(01000)

4. Combine the bits from each chunk to its decimal equivalent. This will yield a decimal value between 0 (which is decimal equivalent of binary 00000) and decimal 31 (which is decimal equivalent of binary 11111) for each chunk.

In our example, it would be:

4 - 0 - 8

5. Construct an array of 32 values containing the sequence A-Z and 0-5:

A  B  C  D  E  F  G  H  I  J  K  L  M  N  O  P  Q  R  S  T  U  V  W  X  Y  Z  0  1  2  3  4  5

6. Use the integer from each chunk to choose a digit from the array:

A

B

C

D

E

F

G

H

I

J

K

L

M

N

O

P

Q

R

S

T

U

V

W

X

Y

Z

0

1

2

3

4

5

0

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

Therefore, 4 - 0 - 8 = E A I

7. Append the resulting 3 digits, in the chunk order, to the end of the 15 chars ID, and you have the 18 chars ID:

a0D30000001n7PiEAI

Codice Apex:

String idSalesforce = 'a0D30000001n7Pi'; // change with your 15 chars ID
if(idSalesforce.length() == 18){
	system.debug('Input Id is 18 char');
}
else if(idSalesforce.length() != 15){
	system.debug('Input Id error');
}
else{
	String suffix = '';
	String idOut= '';
	String InChars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ012345';
	for(Integer i = 0; i<3; i++){
		Integer flags = 0;
		for(Integer j = 0; j<5; j++){
			String c = idSalesforce.substring(i*5+j, i*5+j+1);
			if((c.compareTo('A')>=0) && (c.compareTo('Z')<=0)){
				flags += 1 << j;
			}
		}
		suffix = suffix + InChars.substring(flags, flags+1);
	}
	idOut = idSalesforce + suffix;
	system.debug('Id Output 15: '+idSalesforce);
    system.debug('Id Output 18: '+idOut);
}

Codice JavaScript:

var idSalesforce = 'a0D30000001n7Pi'; // change with your 15 chars ID
if(idSalesforce == ""){
	alert("Please enter into the left column a list of IDs of 15 chars");
	return;
}
var y = idSalesforce.value.split("\r").join("").split("\n");
var z=new Array();
for(var x=0;x<y.length;x++){
	if(y[x].length == 15){
		var s="";
		for(var i=0;i<3; i++)
		{
			var f=0;
			for(var j=0;j<5;j++){
				var c=y[x].charAt(i*5+j);
				if(c>="A" && c<="Z")
					f+=1<<j;
			}
			s+="ABCDEFGHIJKLMNOPQRSTUVWXYZ012345".charAt(f);
		}
		z.push(y[x]+s);
	}
	else{
		alert("Error : "+y[x]+" has not a length of 15 characters ("+y[x].length+")");
		return;
	}
}

Codice PHP:

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

Codice Excel (sostituire "A2" con la la cella contenente l'id di 15 caratteri) :

=A2&MID("ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789",1
+IF(CODE(MID(A2,1,1))>64,1,0)*IF(CODE(MID(A2,1,1))<91,1,0)*1
+IF(CODE(MID(A2,2,1))>64,1,0)*IF(CODE(MID(A2,2,1))<91,1,0)*2
+IF(CODE(MID(A2,3,1))>64,1,0)*IF(CODE(MID(A2,3,1))<91,1,0)*4
+IF(CODE(MID(A2,4,1))>64,1,0)*IF(CODE(MID(A2,4,1))<91,1,0)*8
+IF(CODE(MID(A2,5,1))>64,1,0)*IF(CODE(MID(A2,5,1))<91,1,0)*16,1)
&MID("ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789",1
+IF(CODE(MID(A2,6,1))>64,1,0)*IF(CODE(MID(A2,6,1))<91,1,0)*1
+IF(CODE(MID(A2,7,1))>64,1,0)*IF(CODE(MID(A2,7,1))<91,1,0)*2
+IF(CODE(MID(A2,8,1))>64,1,0)*IF(CODE(MID(A2,8,1))<91,1,0)*4
+IF(CODE(MID(A2,9,1))>64,1,0)*IF(CODE(MID(A2,9,1))<91,1,0)*8
+IF(CODE(MID(A2,10,1))>64,1,0)*IF(CODE(MID(A2,10,1))<91,1,0)*16,1)
&MID("ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789",1
+IF(CODE(MID(A2,11,1))>64,1,0)*IF(CODE(MID(A2,11,1))<91,1,0)*1
+IF(CODE(MID(A2,12,1))>64,1,0)*IF(CODE(MID(A2,12,1))<91,1,0)*2
+IF(CODE(MID(A2,13,1))>64,1,0)*IF(CODE(MID(A2,13,1))<91,1,0)*4
+IF(CODE(MID(A2,14,1))>64,1,0)*IF(CODE(MID(A2,14,1))<91,1,0)*8
+IF(CODE(MID(A2,15,1))>64,1,0)*IF(CODE(MID(A2,15,1))<91,1,0)*16,1)

Codice Visual Basic for Applications:

Function FixID(InID As String) As String
	If Len(InID) = 18 Then
		FixID = InID
		Exit Function
	End If
	Dim InChars As String, InI As Integer, InUpper As String
	Dim InCnt As Integer
	InChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ012345"
	InUpper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"

	InCnt = 0
	For InI = 15 To 1 Step -1
		InCnt = 2 * InCnt + Sgn(InStr(1, InUpper, Mid(InID, InI, 1), vbBinaryCompare))
		If InI Mod 5 = 1 Then
			FixID = Mid(InChars, InCnt + 1, 1) + FixID
			InCnt = 0
		End If
	Next InI
	FixID = InID + FixID
End Function

GDPR Alert

This site uses cookies, including third-party requests, to offer you services in line with your preferences. By closing this banner, scrolling this page or clicking any element, you consent to the use of cookies. If you want to learn more or opt out of all or some cookies, click on Privacy Policy...