3

This is a question I have always wondered. I have a series of classes and methods that modify the Trigger.new case list records as the list is handed off to each class and method.

Apex goes down the lines of classes and methods synchronously in the trigger. I assume after each class and method changes trigger.new (field values updated, etc.), it then hands the new list of case records to the next class and method(s) and so on.

I assume the original unmodified trigger.new list is not handed to the next class or method, the modified one in is.

That is my understanding of how the transaction works. I ask this to prevent my cobweb of classes and methods from becoming too dependent on the order of execution within each trigger context variable.

trigger CaseTrigger on Case (before insert) {

if(Trigger.isBefore) {
   if(Trigger.isInsert) {

        QualifyCase.QualifyRecordType(Trigger.new);
        ProcessCase.ConvertChars(Trigger.new);
        RouteCase.DetermineOwner(Trigger.new);

    }
  }    
}
3

The collection itself remains the same, but the context records are modified. Consider the following contrived example:

Service Layer

public with sharing class MyObjectService
{
    public static void setTextField1(List<MyObject__c> records)
    {
        for (MyObject__c record : records)
        {
            record.Field1__c = 'Hello';
        }
    }
    public static void setTextField2(List<MyObject__c> records)
    {
        for (MyObject__c record : records)
        {
            record.Field2__c = 'World';
        }
    }
    public static void setTextField3(List<MyObject__c> records)
    {
        for (MyObject__c record : records)
        {
            record.Field3__c = record.Field1__c + record.Field2__c;
        }
    }
}

Trigger

trigger MyObject on MyObject__c (before insert)
{
    if (trigger.isBefore)
    {
        if (trigger.isInsert)
        {
            MyObjectService.setTextField1(trigger.new);
            MyObjectService.setTextField2(trigger.new);
            MyObjectService.setTextField3(trigger.new);
        }
    }
}

In this example, when you call setTextField2, each record being processed will have Field1__c already set to 'Hello'. Then when you call setTextField3, both Field1__c and Field2__c will already be set, so Field3__c should equal 'HelloWorld' for each record.

  • +1 for the clean and simple code. But I'm still not understanding the question here. :( – Mahmood May 3 '17 at 22:47
  • 1
    I'm not certain, but I think OP is trying to understand if changes made to the context records will persist across different method calls, to which the answer would be yes. – Adrian Larson May 3 '17 at 22:49
  • @AdrianLarson Yes, that is what I am asking. It was hard to put it in wording. – All about the W May 4 '17 at 13:11
  • @ManifestDestiny Cool. Yeah then you can depend on each individual record being modified for the remainder of the transaction. If you had multiple methods writing to one field, last executed would win. If you have one method that writes to a field then another that depends on that value, it will be populated. – Adrian Larson May 4 '17 at 13:14

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.