function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Damon LongDamon Long 

Converting Trigger to Class

Hey everyone,
I need some help converting my Apex trigger into a class. I am just unsure about the syntax differences between the trigger and class and I'm aware that I can't use trigger.new in the class. Do I need to create a new list to be able to use trigger.new? Any help or direction would be greatly appreciated!

Trigger:
trigger TotalAumUpdate on Account (after update) {
    for(Account acc : Trigger.new){
        Account oldAcc = Trigger.oldMap.get(acc.Id);
        if(oldAcc.Total_AUM__c != acc.Total_AUM__c){
            List<Opportunity> opps = [SELECT Id, AccountId, Current_Balance__c from Opportunity where AccountId = :acc.Id];                              
            List<Opportunity> newids = new List <Opportunity>();
            	for(Opportunity opp: opps){
                    if(opp.Current_Balance__c != acc.Total_AUM__c){
                        opp.Current_Balance__c = acc.Total_AUM__c;
                        newids.add(opp);
                    }
            }
            if(newids.isEmpty()== false){
                update newids;
            }
        }
    }

}

 
Best Answer chosen by Damon Long
v varaprasadv varaprasad
Hi Damon,

Please check once below code : 
trigger TotalAumUpdate on Account (after update) {
    updateOppAmount.updateAmount(trigger.new,Trigger.oldmap);
}

public class updateOppAmount {
    
    public static void updateAmount(List < Account > accs, Map < id, Account > oldMap) {
        set < id > accIds = new set < id > ();
        
        for (account acc: accs) {
            Account oldAcc = oldMap.get(acc.Id);
            if (oldAcc.Total_AUM__c != acc.Total_AUM__c) {
                accIds.add(acc.Id);
                
            }
        }
        
        
        
        List < Opportunity > opps = [SELECT Id, AccountId, Current_Balance__c from Opportunity where AccountId in : accIds];
        List < Opportunity > newids = new List < Opportunity > ();
        for (Opportunity opp: opps) {
            if (opp.Current_Balance__c != acc.Total_AUM__c) {
                opp.Current_Balance__c = acc.Total_AUM__c;
                newids.add(opp);
            }
        }
        if (newids.isEmpty() == false) {
            update newids;
        }
    }
    
}

Hope this helps you.

If it helps you please mark it as best Answer.


Thanks
Varaprasad
For Support: varaprasad4sfdc@gmail.com




 

All Answers

Saravanan Gengan 9Saravanan Gengan 9
you can use trigger.new in the class. when the trigger executes trigger.new get the appropriate values. 
v varaprasadv varaprasad
Hi Damon,

Please check once below code : 
trigger TotalAumUpdate on Account (after update) {
    updateOppAmount.updateAmount(trigger.new,Trigger.oldmap);
}

public class updateOppAmount {
    
    public static void updateAmount(List < Account > accs, Map < id, Account > oldMap) {
        set < id > accIds = new set < id > ();
        
        for (account acc: accs) {
            Account oldAcc = oldMap.get(acc.Id);
            if (oldAcc.Total_AUM__c != acc.Total_AUM__c) {
                accIds.add(acc.Id);
                
            }
        }
        
        
        
        List < Opportunity > opps = [SELECT Id, AccountId, Current_Balance__c from Opportunity where AccountId in : accIds];
        List < Opportunity > newids = new List < Opportunity > ();
        for (Opportunity opp: opps) {
            if (opp.Current_Balance__c != acc.Total_AUM__c) {
                opp.Current_Balance__c = acc.Total_AUM__c;
                newids.add(opp);
            }
        }
        if (newids.isEmpty() == false) {
            update newids;
        }
    }
    
}

Hope this helps you.

If it helps you please mark it as best Answer.


Thanks
Varaprasad
For Support: varaprasad4sfdc@gmail.com




 
This was selected as the best answer
Damon LongDamon Long
Yes that does help me understand it better, you didnt have to code the entire class for me but I'm not going to complain about it. Thank you!