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
imAkashGargimAkashGarg 

URGENT: Compare old and new value

I have to write a trigger for checking the last modified date of an object before update. I need to compare the old value with the new value and if the new date is smaller, the trigger show be discarded (i.e. no update).

 

How to get both(old and new) values of a field?

Also, if the condition is true how to discard the trigger from updating?

 

Need Help.

Best Answer chosen by Admin (Salesforce Developers) 
KidNikiKidNiki

Since you didn't say what the object is that you are running thee trigger on, I just used the generic term, 'sObject' in its place.  Hopefully this makes sense!

 

if(trigger.isUpdate){

for(sObject s : trigger.new){

if(s.lastModifiedDate < trigger.oldMap.get(s.id).lastModifiedDate)

//throw error to screen
s.addError('The new date is older than the previous date!');

//or to the debugger too
system.Debug('New date is less than old date!');


else{

//do other code here...

}
}
}

 You could also put this in an external class method and send the trigger.old and trigger.new as lists to it and then loop through based off of the trigger size.... as far as I know!

All Answers

raseshtcsraseshtcs

Trigger.old and trigger.new return the old and the new values respectively.... write the update statement if the condition is true..

imAkashGargimAkashGarg

Thanks for the quick response.

 

Can you show them with some code examples?

 

KidNikiKidNiki

Since you didn't say what the object is that you are running thee trigger on, I just used the generic term, 'sObject' in its place.  Hopefully this makes sense!

 

if(trigger.isUpdate){

for(sObject s : trigger.new){

if(s.lastModifiedDate < trigger.oldMap.get(s.id).lastModifiedDate)

//throw error to screen
s.addError('The new date is older than the previous date!');

//or to the debugger too
system.Debug('New date is less than old date!');


else{

//do other code here...

}
}
}

 You could also put this in an external class method and send the trigger.old and trigger.new as lists to it and then loop through based off of the trigger size.... as far as I know!

This was selected as the best answer
Ispita_NavatarIspita_Navatar

 Hi,

You can check old value and new value from trigger.old and trigger.new respectively.

Check the code snippet below:-

                                  trigger Accdata on accont(before update)

                                  {

                                  Account accOld = trigger.old[0];

                                  Account accNew = trigger.new[0];

                                  if(accOld.lastmodifieddate != accNew.lastmodifieddate){//YOUR BUSINESS LOGIC HERE...}

                                  }

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 

imAkashGargimAkashGarg

Thanks.