3

I've a simple conceptual doubt - Would really appreciate if someone would explain me the reason for this behaviour:

I'm inserting a new contact from console and following is the snippet:

Contact c = new Contact();
c.Email = 'TSTCONTACTJAN11TEST123@TEST.COM';
c.LastName = 'TEST';
c.CurrencyIsoCode = 'USD';
insert c;
system.debug('+++'+c.Email);

I've written a simple before insert trigger which would check if the Email is not null then would make the Email lower case and add '.com' to it.

  For(Contact c: newCon){
            if(c.Email != null){
                c.Email = c.Email.toLowerCase() + '.com';
            }
        }

So when I'm running the first snippet in developer console the debug result is TSTCONTACTJAN11TEST123@TEST.COM

Shouldn't it be tstcontactjan11test123@test.com.com since before trigger will be acting upon it?

I'm getting tstcontactjan11test123@test.com.com only when I run the following snippet along with the first snippet:

Contact newCon = [SELECT Id, Email FROM Contact Where Id = :c.Id LIMIT 1];
System.debug('+++ After Insert Email Value: '+newCon.Email);
8

When you perform DML on a record, the "in memory" instance (i.e. your c variable) of your object is not automatically updated1.

If you re-query the record, and debug the email value, you would see "tstcontactjan11test123@test.com.com" as you expect.

If you don't re-query the record, the in-memory field values remain untouched.

As a side note, explicitly transforming the email to lower case is not necessary if you're working with an email type field (and not something like, say, a text field). Salesforce automatically makes emails all lower case (learned that one the hard way some months ago).

1: If you're inserting a record, after the DML completes, the in-memory instance will be updated to contain the Id (and recordTypeId too, I believe, if you have multiple recordtypes for that particular object)

4
  • Thanks Derek F - I had the same doubt, so does it mean that only Id and RecordTyepId of the 'in memory' instance is updated and everything else is left untouched?? If yes, are there any other fields that are also updated in the 'in memory' instance?
    – sfdcnewbie
    Jan 11 '20 at 16:33
  • It will be great if you could help me with where I could find the documentation around it :)
    – sfdcnewbie
    Jan 11 '20 at 16:35
  • @sfdcnewbie That only happens for inserts, and only those fields. No documentation on that as far as I know.
    – Derek F
    Jan 11 '20 at 16:44
  • 2
    In case you are interested, you can ask Salesforce to recalculate formula fields in the in-memory instance without an explicit re-querying of the object using SObject.recalculateFormulas. This does, however, count towards your query limits. It may be useful if you are simply wanting formula values for a single object that you just updated.
    – Phil W
    Jan 11 '20 at 17:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.