0

I am trying to understand how I can compare the the old and new values of the fields in the Apex Class. I have a VF page on Leads object which has the inputCheckbox. From the VF pages's extension controller I am updating the Lead record which fires the after update trigger on the Lead record which inturn calls the apex Class. The Apex Class is like below

Public class LeadTriggerHandler{ 
public static void updateAOI(set<Id> lIds){
    List<Lead> Leadlist = [Select Id, Autoimmune__c ,Immunology__c 
                           from Lead WHERE Id IN: lIds];
    List<Lead> updateList = new List<Lead>();
    set<id> LeadIds= new set<Id>();
    for(Lead l : Leadlist){
        if(l.Autoimmune__c == True)
        {
            l.Immunology__c = true;
            if(!leadIds.contains(l.Id)){
                leadIds.add(l.Id);
                updateList.add(l);
        }}}                
    checkRecursiveTrigger.ranOnce = true;
    if(!updatelist.isempty())       
    update updatelist;
   }}

In the Apex Class I should check if the Autoimmune__c new value is True I need to do something and new value of the field is false I should be do something else. I was reading how how I can do the comparison of the old and new values in apex class and found that cloning the orginal record. can anyone suggest if I need to handle this in the Trigger or Apex Class or VF page. The trigger is like below where we call the apex class with the list of the Lead ID's

trigger LeadTrigger on Lead (after insert, after update) {
    set<Id> leadIds = new set<Id>();
    for(Lead l : Trigger.new){
        leadIds.add(l.Id);
    }
    if(!leadIds.isempty() && checkRecursiveTrigger.ranOnce == false)
    LeadTriggerHandler.updateAOI(leadIds);
}

Any help is greatly appreciated

1 Answer 1

3

Old and New values exist only in the Trigger context. So if you want to use them, you need to pass the trigger old and new list/maps variables to your apex method.

For example:

trigger LeadTrigger on Lead (after update) {
    LeadTriggerHandler.updateAOI(trigger.newMap, trigger.oldMap);
}

The apex method:

public static void updateAOI(Map<Id, Lead> newLeads, Map<Id, Lead> oldLeads){
   for (Lead l : newLeads.values()) {
        if (oldLeads.get(l.Id).Autoimmune__c == false) {
            // some codes
        }
   }
}
4
  • Thank you, just for clarification I am Passing the set of Lead Ids from the triggers , I added the trigger to the question, will it be the same when passing the (trigger.newMap, trigger.oldMap); }
    – user81642
    Jul 13, 2020 at 3:38
  • You can pass the Ids, but the old values are stored in trigger.old and trigger.oldMap, so if you pass them over you don't have to reference the trigger context variable in your apex method so it is more reusable. Jul 13, 2020 at 4:00
  • I tried to follow this answer in another scenario salesforce.stackexchange.com/questions/312504/… posted here. Can you please let me know what I am missing
    – user81642
    Jul 13, 2020 at 13:39
  • The page has been removed. Jul 14, 2020 at 1:54

You must log in to answer this question.

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