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
KMK91KMK91 

how to compare old values and new values using apex class ?

Best Answer chosen by KMK91
Dilip_VDilip_V
Madhukar,

Sorry its a mistake.

Try this 

for(Payment__c  pay:NewMap.values())

Thanks.

All Answers

Dilip_VDilip_V
Madhukar,

Here I am assuming that you are invoking helper class through trigger.

Pass the old and new maps to the helper class.
​for(Account Acc:NewMap.keyset()){
if(Oldmap.get(Acc.Id).name!=Newmap.get(Acc.Id).name){
     //Process records 
}
}
Let me know if you have any issues.

Mark it as best answer if it works.

Thanks.

 
KMK91KMK91
Please can you provide me full code
Dilip_VDilip_V
Madhukar,

Trigger looks like this
 
trigger AccountTrigger On Account(Before insert,After insert,Before update,After update)//Add events //according to your logic
{

if(Trigger.isBefore)
{
    if(Trigger.IsUpdate)
        AccountHelperClass.ProcessAccounts(Trigger.oldMap,Trigger.NewMap);
}
}

And Apex Class
 
Public Class AccountHelperClass
{

Public static void ProcessAccounts(Map<Id,Account> OldMap,Map<Id,Account> NewMap)
{
  ​for(Account Acc:NewMap.keyset())
  { 
    if(Oldmap.get(Acc.Id).name!=Newmap.get(Acc.Id).name)
     { 
     //Process records  
      } 
   }
}

}
Thanks.
 
KMK91KMK91
could you please share for the condition if amount >0  and field should zero
 
KMK91KMK91
for(Payment__c  pay:NewMap.keyset())
showing error

Loop variable must be id
Dilip_VDilip_V
Madhukar,

Sorry its a mistake.

Try this 

for(Payment__c  pay:NewMap.values())

Thanks.
This was selected as the best answer
KMK91KMK91
Thanks Thermo
 
Yashraj Singh 13Yashraj Singh 13
trigger handler class:
public class accounttoAccount {

   
        Public static void ProcessAccounts(Map<Id,Account> OldMap,Map<Id,Account> NewMap)
{
  for(Account Acc:newMap.values())
  { 
    if(Oldmap.get(Acc.Id).type=='Customer - Direct' && Newmap.get(Acc.Id).Type=='Prospect')
     { 
                  Acc.Description='ARA-Zila-Bihar';
       
      } 
   }
}

}


trigger look like...


trigger Accounttocontact on Account (before update) {
    

    if(Trigger.IsUpdate)
       accounttocontact.ProcessAccounts(Trigger.oldMap,Trigger.NewMap);


}