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
vandana rajuvandana raju 

old and new vales in apex class

Hi
I am trying to understand how to compare old values and new values for sobject.
I have a opportunity custom field , type=check box, which is checked only when stageName='Closed Won'.

when the custom field is checked the trigger further will do some business logic.

any help will ge greatly apprecitaed.

thanks


 
Best Answer chosen by vandana raju
FearNoneFearNone
hi vandana,

In trigger, values are stored in global variables.
old values => Trigger.oldMap
new value => Trigger.new

Here is a sample:
trigger Winning on Opportunity (before update) {
  // read the new values
  for (Opportunity opp : Trigger.new) {
    Boolean newVal = opp.StageName.equals('Closed Won');

    // Access the "old" record by its ID in Trigger.oldMap
    Opportunity oldOpp = Trigger.oldMap.get(opp.Id);
    Boolean oldVal = oldOpp.StageName.equals('Closed Won');
    
    // compare the old and new
    if (oldVal != newVal) {
      opp.isDifferent__c = true;
    }
  }
}


 

All Answers

FearNoneFearNone
hi vandana,

In trigger, values are stored in global variables.
old values => Trigger.oldMap
new value => Trigger.new

Here is a sample:
trigger Winning on Opportunity (before update) {
  // read the new values
  for (Opportunity opp : Trigger.new) {
    Boolean newVal = opp.StageName.equals('Closed Won');

    // Access the "old" record by its ID in Trigger.oldMap
    Opportunity oldOpp = Trigger.oldMap.get(opp.Id);
    Boolean oldVal = oldOpp.StageName.equals('Closed Won');
    
    // compare the old and new
    if (oldVal != newVal) {
      opp.isDifferent__c = true;
    }
  }
}


 
This was selected as the best answer
vandana rajuvandana raju
Hi
How to get trigger.old Map in apex class not in trigger?

Thanks
vandana
vandana rajuvandana raju
Hi
this is what I did.could you pls tell me where I went wrong becuase the field  isDifference is not updating
public class dosomething
{
 
    public void Display(List<Opportunity> newopp,Map<ID,Opportunity> oldopp)
    {
      for(Opportunity o:newopp)
      {
        Opportunity o1 = new Opportunity();
        o1=oldopp.get(o.ID);
        
        system.debug('old opportunitID'+o1);
        
        Boolean oldOppIsWon = o1.StageName.equals('Closed Won');
        system.debug('oldOppIsWon '+oldOppIsWon );
        
        Boolean newOppIsWon = o1.StageName.equals('Closed Won');
        system.debug('newOppIsWon '+newOppIsWon);
        
        // Check that the field was changed to the correct value
        if (!oldOppIsWon && newOppIsWon)
        {
              o.isDifferent__c=true;
        }

      }
    }
}

trigger trg_updatecheckbox on Opportunity (before Insert,before Update)
{
if (trigger.isbefore)
{
  if (trigger.isUpdate)
{
   dosomething d1=new dosomething();
   d1.Display(trigger.new,trigger.oldmap);
  }
}
}

thanks
vandana
vandana rajuvandana raju
Hi I got this working.
The issue was I was comparing the new version with the new version only
Boolean oldOppIsWon = o1.StageName.equals('Closed Won');
Boolean newOppIsWon = o1.StageName.equals('Closed Won');

so I changed as follows:
Boolean oldOppIsWon = o1.StageName.equals('Closed Won');
Boolean newOppIsWon = o.StageName.equals('Closed Won');
 
FearNoneFearNone
sorry for late reply.
it is good to see you are progressing on your own!
is everything okay now? or you still have some clarifications?
vandana rajuvandana raju
Hi
good to see your post. 
sorry for the delay in replying
I got the logic working.thanks

vandana