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
OnurKOnurK 

oldMap and newMap in Class

Hi All;

 

I have a class runs after trigger. I need my class not run after an update when the certain field not updated. (I need my trigger runs after tax number changes) In trigger body it is possible to accomplish this with;

 

System.trigger.oldMap.get(c.id).Tax_Number__c != System.trigger.newMap.get(c.id).Tax_Number__c )

 

I can not add this code in a class. There must be some other way we can compare old value vs new value in a class. Can anyone help me with the issue I have?

 

Thank you very much for your help.

Best Answer chosen by Admin (Salesforce Developers) 
SrikanthKuruvaSrikanthKuruva

Yes you cannot add the code in the class. you have to do that filtering in the apex trigger and then pass only the qualified records to the class

All Answers

SrikanthKuruvaSrikanthKuruva

Yes you cannot add the code in the class. you have to do that filtering in the apex trigger and then pass only the qualified records to the class

This was selected as the best answer
AmitSahuAmitSahu

You can try this in your trigger:

 

Trigger triggername on object (trigger conditions)

{

if(IsUpdate)

{

if(System.trigger.oldMap.get(c.id).Tax_Number__c != System.trigger.newMap.get(c.id).Tax_Number__c )

{

//do your stuff here

}

}

 

}

 

I hope this helps

OnurKOnurK

Thank you SrikanthKuruvaSR,

 

This is what I thought as well. 

OnurKOnurK

Thanks j020,

 

I think we can not use the code you suggested in the class.

AmitSahuAmitSahu

Ofcourse we can't use the code in class but it was ment for trigger only. You need to call the specific method from your trigger based on this condition.

 

Regards,

OnurKOnurK
for(Case cs : Trigger.new)
if(cs.Sales_Representative__c != System.trigger.newMap.get(cs.id).Sales_Representative__c){
	   					SalesRepRegion.updateSalesRepresantativeID(Trigger.new);
}
   			}

 I have a class updates a field from Sales Representative field. I need trigger not run if the Sales Representative field not changed. ı tried something like this but I have another before case trigger test give error now.

 

Can you please help?

SrikanthKuruvaSrikanthKuruva

i see that you are doing a mistake in the code.

 

cs corresponds to trigger.new and you are comparing this again with the trigger.newMapinstead of trigger.oldmap. you should be doing something like following (changed System.trigger.newMap to System.trigger.oldMap)

for(Case cs : Trigger.new)
{
if(cs.Sales_Representative__c != System.trigger.oldMap.get(cs.id).Sales_Representative__c)
{
SalesRepRegion.updateSalesRepresantativeID(Trigger.new);//here you are passing the whole list of cases in trigger.new...check if this is really required
}
}

 

OnurKOnurK

Thank you very much for your response.