Oh my god. It's full of code!

Posts tagged “ID


Apex update sobject without querying for it first

Hey all,
This is just another quick handy tip. Sometimes when working with Apex you will want to update an object you already know the ID of without having to query for it first. Say for example you modify an account related to a contact. You already know the ID of the account, and don’t want to waste an SOQL query having to query for it. You might think you can do this

list<contact>  theContacts = [select id, accountid, name from contact limit 5];
list<account> updateAccounts = new list<account>();
for(contact con : theContacts)
{
    account thisContactsAccount = new account();
    thisContactsAccount.id = con.accountId;
    thisContactsAccount.name = 'blah blah blah';
    updateAccounts.add(thisContactsAccount);
}
update updateAccounts;

But you can’t. You’ll get the error ‘Field is not writeable: Account.Id’.
Instead, you can do this!

list<contact>  theContacts = [select id, accountid, name from contact limit 5];
list<account> updateAccounts = new list<account>();
for(contact con : theContacts)
{
    account thisContactsAccount = new account(Id=con.accountid);
    thisContactsAccount.name = 'blah blah blah';
    updateAccounts.add(thisContactsAccount);
}
update updateAccounts;

That will work and allow you to create a reference to an sObject without having to query for it first. Pretty handy sometimes. Anyway, just one of those quick time saving tips that isn’t super obvious at first. Hope this helps someone!


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!