0

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.

0

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.

Well that's the answer to your question. Store the old values in another record of the same object, and the new values on another record. Link both records via a lookup field.

When the record is approved, you can use apex to then transfer all the approve fields back to the original record and delete the other one.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.