Oh my god. It's full of code!

Coldfusion Expand Salesforce object Id

Hey all.
Just another quick handy ColdFusion function here. This one will allow you to take a fifteen char Salesforce ID, and expand it into an eighteen character one. This is useful since if you want to store Salesforce Ids in ColdFusion, you’ll want a none case sensitive version, which is what the 18 character long one is good for. The 15 char one is case sensitive, while the 18 char one is not. So I always work with the 18 char one, since it’s easier.

<cffunction name="ExpandSFID" hint="I take a 15 char salesforce ID and turn it ino the 18 char one">
    <cfargument name="SFID" type="string" required="yes">
    <cfset var map = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ012345'>
    <cfset var = arraynew(1)>
    
    <cfoutput>
    
    <cftry>
        <cfif len(arguments.SFID) eq 15>
            <cfset ChunkTotal[1] = 0>
            <cfset ChunkTotal[2] = 0>
            <cfset ChunkTotal[3] = 0>
            
                <cfloop from="1" to="3" index="i"> 
                    <cfloop from="5" to="1" index="j" step="-1">
                        <cfset c = mid(arguments.sfid, ((i-1)*5)+j, 1)>
                        <cfif reFind("^[A-Z]$", c) GT 0>
                            <cfset ChunkTotal[i] += 2^(j-1)>
                        </cfif>
                    </cfloop>
                        <cfset arguments.SFID &= mid(map,ChunkTotal[i]+1,1)>      
                </cfloop>
        </cfif>
        
            <cfreturn arguments.SFID>
            
        <cfcatch type="any">
            <cfreturn "Error Converting Salesforce ID #cfcatch.message# #cfcatch.detail#">
        </cfcatch>
    </cftry>    
    </cfoutput>
    
    
</cffunction>

Hope this helps!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s