You need to sign in to do that
Don't have an account?
Trigger after update save new value but revert to previous value
I Have a sobject name member__c in which there is a field text__c. Text__c has a value called HI. i saved the record.
now i changed the value of text__c from HI to new and click on save, after saving it should again display HI in text__c instead of new.
Is it possible through trigger ? If yes through your valuable suggesstions. This criteria should be met only using after update.
Ok i am going with this code, it is working fine.
All Answers
After update would require you to update the field and go through the whole save again, which sounds like it could lead to recursion.
If you use a before update trigger you can simply change the value of the field to the previous value based on the trigger.old records. Is there a particular reason why you are mandating an after update trigger?
Yes i am getting recursion error. There are no particular reasons, i lost an job offer because of this and i just want to do it.
You need to change your trigger to check the old/new values before copying and updating. If they are the same, take no action. This should solve the recursion.
If given any related example code it would be greatly helpful
It would be something like:
Remove update toupdate statement else you will get an error dml operation won't run on trigger.new or trigger.old values.
Hey bob it's not getting done for after update. it's working fine for before update only. after update is extracting this error message System.FinalException: Record is read-only:
My bad - you have to clone the object and then update that. Something like the following:
but this would be the wrong way to go about it - a before update can change the record before it is committed to the database, hence much more efficient in terms of code and database operations.
Ok i am going with this code, it is working fine.
A very sincere thanks to bob buzzard.
You don't need the after update side of things, as you are guaranteeing the values will be identical due to the before update firing. Change it to simply be a before update only and you will be good to go.