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 

How To Add Additional Tracking Fields to Apex Trigger

Hi Everyone,
I have an apex trigger that tracks field updates on the custom object Menu__c. When an field in the object Menu__c field set is changed, a new record is created under the custom object 'Menu Price Changes'. 

So far, I only track the field name that is changed and the old and new value. I also want to add the record name to be added to the new Menu Price Changes record, specifically the location name or the Menu Name. 

What would be the best way to do this? If anything it would be great to have a lookup value field for the Menu Name under the Menu Pice Changes object and have it automatically filled in when a field is changed on the Menu Object. 

Any help on this would be amazing!!! I think I am missing something small in my code. 

User-added imageUser-added image
 
trigger AccountHistoryTracker on Menu__c (after update) {

    final List<Schema.FieldSetMember> trackedFields = 
        SObjectType.Menu__c.FieldSets.MenuHistoryFieldSet.getFields();

    if (trackedFields.isEmpty()) return;

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

    if(!trigger.isUpdate)
        return;

    for (Menu__c newAccount : trigger.new) {

        final 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));

            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;

            fieldChanges.add(accountHistory);
        }
    }

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

 
Best Answer chosen by Sam Epstein
Jithesh VasudevanJithesh Vasudevan
Please check this,

trigger AccountHistoryTracker on Menu__c (after update) {

final List<Schema.FieldSetMember> trackedFields =
SObjectType.Menu__c.FieldSets.MenuHistoryFieldSet.getFields();

if (trackedFields.isEmpty()) return;

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

if(!trigger.isUpdate)
return;

for (Menu__c newAccount : trigger.new) {

final 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));

// the additional line to get the value of location name from Menu Object
String locationName = newAccount.<Location_Name__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;

// additional line to update it in the new record
accountHistory.<Location_Name__c> = locationName;

fieldChanges.add(accountHistory);
}
}

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

All Answers

Jithesh VasudevanJithesh Vasudevan
Please check this,

trigger AccountHistoryTracker on Menu__c (after update) {

final List<Schema.FieldSetMember> trackedFields =
SObjectType.Menu__c.FieldSets.MenuHistoryFieldSet.getFields();

if (trackedFields.isEmpty()) return;

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

if(!trigger.isUpdate)
return;

for (Menu__c newAccount : trigger.new) {

final 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));

// the additional line to get the value of location name from Menu Object
String locationName = newAccount.<Location_Name__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;

// additional line to update it in the new record
accountHistory.<Location_Name__c> = locationName;

fieldChanges.add(accountHistory);
}
}

if (!fieldChanges.isEmpty()) {
insert fieldChanges;
}
}
This was selected as the best answer
Sam EpsteinSam Epstein
Thank you!