Skip to main content
I created a trigger in my Sandbox that works great but I'm unclear how to write the Class for it. If someone can please help a Newbie:

trigger updateAccountStatus on Opportunity (after insert, after update) 

{

    list<Id> accIds = new list<Id>();

    

    list<Account> accounts = new list<account>();

    

    for(opportunity o:trigger.new){

    

        accIds.add(o.accountId);

    

    }

    

    for(account a:[select Id, Status__c from account where Id IN :accIds]){

    

        for(opportunity opp:trigger.new){

    

    a.Status__c = opp.Status__c;

    a.Approval_Progress__c = opp.StageName;

    

    accounts.add(a);

    

/*            if(opp.Status__c=='Active'){

    

                a.Status__c='Active';

    

                accounts.add(a);

    

            }

*/    

        }

    

    }

    

    update accounts;

}
8 answers
  1. May 11, 2015, 4:44 PM
    Hi Robert ,

    If you want handler class for trigger code . Then try below code .

     

    Trigger code

    trigger updateAccountStatus on Opportunity (after insert, after update) {

    OpportunityTriggerHandler.afterEvent(Trigger.New);

    }

    ///Handler class code

    public class OpportunityTriggerHandler{

    public static afterEvent(List<opportunity> oppList){

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

    for(opportunity opp : oppList){

    if(opp.AccountId != null){

    Account acc=new Account(Id=opp.AccountId,Status__c = opp.Status__c,pproval_Progress__c = opp.StageName);

    accListToupdate.add(acc);

    }

    }

    try{

    update accListToupdate;

    }catch(DMLException de){

    System.debug(de);

    }

    }

    }

    I have optimised code little bit ,In case  any doubt please let me know .

    Thanks

    Manoj

     
0/9000