1

I'm trying to use object's fields updated in a trigger in the context where the DML statement is executed, however I'm not being able to get the new values assigned by the trigger.

I did the following tests executing anonymous code:

Trigger

trigger TriggerPOC on MyObject__c (before insert)
{
    for (MyObject__c myObj : Trigger.New)
    {
        myObj.Name = 'Yay!';
    }
}

Test code

MyObject__c objTest = new MyObject__c(Name='should be replaced');
system.debug(objTest.Name); // displays 'should be replaced'
insert objTest;
system.debug(objTest.Name); // displays 'should be replaced' (expected 'Yay!')
system.debug([SELECT name FROM MyObject__c WHERE Id=:objTest.Id].Name); // displays 'Yay!'

My objective is to use the 'Yay!' value after execute the DML statement, but I can't get it unless I run a query to retrieve it. I would like to avoid do the query.

Now my question is:

why the new values assigned in the trigger are not reflected in the DML statement context?

also, is there an easier way to get the new assigned values without running a query?

  • 1
    You will need to do the query to get the updated value. – Daniel Ballinger Jul 15 '15 at 23:30
  • 1
    A way to think of this is that the trigger is executing in a separate process (the database) compared to the code that initiates the insert (an app server). Only the ID value comes back from the database to the app server automatically. There could be hundreds of fields and automatically syncing all of them between the two processes is not part of the design, hence the need to explicitly query. – Keith C Jul 15 '15 at 23:55
  • why not use an after insert trigger? Trigger.new will then have all of the fields inserted as well as formula fields available to you (though not parent or child relationship fields - those require querying) – cropredy Jul 16 '15 at 4:26
  • @KeithC Do you have a source doc/article where I can read about the separate process that you mention? Your comment seems to be close to the answer I'm looking for. – Joca Jul 16 '15 at 13:33
  • @JoseCarlos Perhaps there is a document but I don't know of one. What I described is what I infer from reading various things. – Keith C Jul 16 '15 at 13:45
2

When you perform a DML statement, only the record Id is returned. So after you've inserted objTest only the Id will come back as a result of the DML statement. Another query is a must if you want to retrieve other values from that record.

| improve this answer | |
  • Thanks for your answer, I didn't know that only Id will come back from DML operation. However I'm curious what's the explanation behind that behavior, I was guessing that it has to do with different process contexts or something like that. I tried to find something about it in SFDC docs but I didn't have success. – Joca Jul 16 '15 at 13:29
  • I don't have that level of detail, perhaps for performance. It might be worthwhile posting another question on this. – Boris Bachovski Jul 16 '15 at 23:41

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.