0

I am trying to update a Picklist value efg__c based on Picklist value abc__c on Account record, this should happen after I click on Save.

Below is the trigger and Apex class, unable to get the correct Trigger for AfterInsert

Class:

public class fieldUpdate_Handler{
public void afterInsert(){

    List<Account> fUpdateList1 = new List<Account>();        

    for(Account acc: [SELECT Id, abc__c, efg__c 
                      FROM Account
                      WHERE Id
                      IN: Trigger.New]){
        if(acc.abc__c == 'Processing'){
            fUpdateList1.add(acc);
        }

        if(fUpdateList1 <> null)
            update1(fUpdateList1);
    }
 }

 public void update1(List<Account> aList){
     for (Account a: aList){
         a.efg__c = 'Initial Review';
     }
 } }

Trigger:

trigger fieldUpdate on Account (before insert, before update, after insert, after update) {
if(Trigger.isAfter){                                                                               
    if(Trigger.isUpdate){                                                                                                
       fieldUpdate_Handler.afterUpdate(Trigger.New, Trigger.oldMap); //I am receiving an error here
    }
}}

Can someone help me with this Trigger.

Thanks in Advance!

1 Answer 1

2

First, a quick note: You do not need to re-query the records. Trigger.new already contains the field values for the Account, including Id, abc__c, and efg__c. You only need to query during execution of the trigger for related records and their field values.

The code, as shared, would not compile because there is no such method fieldUpdate_Handler.afterUpdate(List<Account>, Map<Id, Account>). I'm assuming you would have figured that out on your own though.

So assuming that it compiles and runs when you write it as an after update trigger, but not an after insert trigger, your problem would be that Trigger.oldMap does not exist in the insert context. The reason it does not exist is because that map contains the prior state of the record before the update that caused the trigger to fire, and in the insert context the record is new and therefore there is no prior state.

Finally, for a trigger that is reading one value on a record and updating another value on the same record, it's best to use a before trigger which eliminates the need for DML statements and the possibility of recursive trigger execution (recursive trigger execution occurs when an after update trigger updates the record causing the after update trigger to update the record causing the after update trigger to update the record forever until it crashes into a governor limit). A before trigger is simpler and more efficient and fully meets the requirements you've laid out here. You could modify your code like so:

public class fieldUpdate_Handler {

    private static final String ABC_PROCESSING = 'Processing';
    private static final String EFG_INITIAL_REVIEW = 'Initial Review';

    public void beforeInsert(List<Account> newAccounts) {
        setEfgValue(newAccounts);
    }

    public void beforeUpdate(List<Account> newAccounts, Map<Id, Account> oldMap) {
        setEfgValue(newAccounts);
    }

    private void setEfgValue(List<Account> newAccounts) {
        for(Account acc: newAccounts) {
            if(acc.abc__c == ABC_PROCESSING) {
                acc.efg__c = EFG_INITIAL_REVIEW;
            }
        }
    }
}

trigger fieldUpdate on Account (before insert, before update) {
    if(Trigger.isBefore) {
        if(Trigger.isInsert) {
            fieldUpdate_Handler.beforeInsert(Trigger.new);
        } else if(Trigger.isUpdate) {                                                                                                
            fieldUpdate_Handler.beforeUpdate(Trigger.new, Trigger.oldMap);
        }
    }
}

Having the insert and update triggers call into different handler methods will allow for having some processes that run in the update trigger that do need the oldMap while still having both share a common method to set the picklist value, but besides that you can eliminate some complexity by just updating the records in the loop since before triggers are in a pre-DML context and there's no need to create a separate list.

1
  • +1 but I think you meant beforeInsert.
    – sfdcfox
    Nov 29, 2017 at 2:24

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .