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
Sam EpsteinSam Epstein 

Store and Revert Prior Field Values on Object After Approval Process Rejection

Hello!

​​​​​​I am building a process where partner users make multiple field updates to an object Fatburger_Menu__c (all the fields are currency), which will activate an approval process for one of our team members.

If the approval is rejected, I would want to create an Apex trigger that will revert all the field values that were changed back to their original values.
Another issue is that this object has over 100 fields available for editing so can't use field tracking in any way. I would also like to have an email triggered to the approver showing a list of the updated fields like this:

Hello,
Here are the list of fields that are being requested for update:

Field 1 Name: Field 1 Prior Value | Field 1 New Value
Field 2 Name: Field 2 Prior Value | Field 2 New Value
etc...

I do have an apex trigger that will run if the approval process is approved since this trigger stores the old and new values in a separate object so we can run reports. This grabs the fields from a field set on the object. Here is the code:
 
trigger FatburgerMenuPriceTracker on Fatburger_Menu__c (after update) {

final List<Schema.FieldSetMember> trackedFields = 
    SObjectType.Fatburger_Menu__c.FieldSets.MenuFieldSet.getFields();

if (trackedFields.isEmpty()) return;

final List<Menu_Price_Tracker__c> fieldChanges = 
    new List<Menu_Price_Tracker__c>();

if(!trigger.isUpdate)
    return;

for (Fatburger_Menu__c newAccount : trigger.new) {

    final Fatburger_Menu__c oldAccount = trigger.oldmap.get(newAccount.Id);

    for (Schema.FieldSetMember fsm : trackedFields) {

        String fieldName  = fsm.getFieldPath();
        String fieldLabel = fsm.getLabel();

        if (newAccount.get(fieldName) == oldAccount.get(fieldName))
            continue;

        String oldValue = String.valueOf(oldAccount.get(fieldName));
        String newValue = String.valueOf(newAccount.get(fieldName));
        String brand = newAccount.Brand__c;
        String locationName = newAccount.Location__c;


        if (oldValue != null && oldValue.length()>255) 
            oldValue = oldValue.substring(0,255);

        if (newValue != null && newValue.length()>255) 
            newValue = newValue.substring(0,255); 

        final Menu_Price_Tracker__c accountHistory = 
            new Menu_Price_Tracker__c();

        accountHistory.name         = fieldLabel;
        accountHistory.apiName__c   = fieldName;
        accountHistory.OldValue__c  = oldValue;
        accountHistory.NewValue__c  = newValue;

        accountHistory.Location_Name__c = locationName;
        accountHistory.Brand__c = locationName;


        fieldChanges.add(accountHistory);
    }
}

if (!fieldChanges.isEmpty()) {
    insert fieldChanges;
}
}
}

Any help on this would be amazing and thank you all so much for your help!