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
ezdhanhussainezdhanhussain 

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.

Best Answer chosen by Admin (Salesforce Developers) 
ezdhanhussainezdhanhussain

Ok i am going with this code, it is working fine.

trigger trigafupdate on member__c (before update,after update) {
     List<Member__c> toUpdate=new List<Member__c>();
    for (Member__c member : trigger.new)
    {
        String newVal=member.Text__c;
        String oldVal=trigger.oldMap.get(member.id).Text__c;

        if (newVal!=oldVal)
        {
           member.Text__c=oldVal;
           toUpdate.add(member);
        }
    }
}   

 

All Answers

bob_buzzardbob_buzzard

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?

ezdhanhussainezdhanhussain

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.

bob_buzzardbob_buzzard

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.

ezdhanhussainezdhanhussain

If given any related example code it would be greatly helpful

bob_buzzardbob_buzzard

It would be something like:

 

trigger au_member on Member__c (after update)
{
    List<Member__c> toUpdate=new List<Member__c>();
    for (Member__c member : trigger.new)
    {
        String newVal=member.Text__c;
        String oldVal=trigger.oldMap.get(member.id).Text__c;

        if (newVal!=oldVal)
        {
           member.Text__c=oldVal;
           toUpdate.add(member);
        }
    }

    if (toUpdate.size()>0)
    {
       update toUpdate;
    }
}

 

ezdhanhussainezdhanhussain

Remove update toupdate statement else you will get an error dml operation won't run on trigger.new or trigger.old values.

ezdhanhussainezdhanhussain

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:

bob_buzzardbob_buzzard

My bad - you have to clone the object and then update that. Something like the following:

 

trigger au_member on Member__c (after update)
{
    List<Member__c> toUpdate=new List<Member__c>();
    for (Member__c member : trigger.new)
    {
        String newVal=member.Text__c;
        String oldVal=trigger.oldMap.get(member.id).Text__c;

        if (newVal!=oldVal)
        {
           Member__c newMem=member.clone(true, true);
           newMem.Text__c=oldVal;
           toUpdate.add(newMem);
        }
    }

    if (toUpdate.size()>0)
    {
       update toUpdate;
    }
}

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. 

ezdhanhussainezdhanhussain

Ok i am going with this code, it is working fine.

trigger trigafupdate on member__c (before update,after update) {
     List<Member__c> toUpdate=new List<Member__c>();
    for (Member__c member : trigger.new)
    {
        String newVal=member.Text__c;
        String oldVal=trigger.oldMap.get(member.id).Text__c;

        if (newVal!=oldVal)
        {
           member.Text__c=oldVal;
           toUpdate.add(member);
        }
    }
}   

 

This was selected as the best answer
ezdhanhussainezdhanhussain

A very sincere thanks to bob buzzard.

bob_buzzardbob_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.