0

I have written apex code to copy the data of a few specified fields from Order record to Order Line Item record when their values are modified.

The problem here is every time a new field has to be added to follow this logic, I have to modify this code and write a few more lines separately for that particular field. As this is happening often, I want to move the list of fields to Custom Settings or Custom Metadata.

Can anyone please Let me know or guide me on how I can achieve this? If there are any helpful resources that provide a solution for this problem will also be appreciated. Thanks in advance.

if(ord.Pacing__c != Trigger.oldMap.get(ord.id).Pacing__c || ord.Weekly_Reporting_Days__c != Trigger.oldMap.get(ord.id).Weekly_Reporting_Days__c ||ord.Delivery_Contact_Email__c != Trigger.oldMap.get(ord.id).Delivery_Contact_Email__c || ord.EffectiveDate != Trigger.oldMap.get(ord.id).EffectiveDate || ord.End_Date__c != Trigger.oldMap.get(ord.id).End_Date__c || ord.Customer_Success_Manager__c != Trigger.oldMap.get(ord.id).Customer_Success_Manager__c || ord.Pacing_Notes__c != Trigger.oldMap.get(ord.id).Pacing_Notes__c || ord.Purchase_Order__c != Trigger.oldMap.get(ord.id).Purchase_Order__c) {
        changeTrackList.add(ord.Id);
    }
        
    if(changeTrackList != null && !changeTrackList.isEmpty() && changeTrackList.contains(ordItm.OrderId)) {
        
        if(ordItm.Customer_Success_Manager__c == Trigger.oldMap.get(ordItm.OrderId).Customer_Success_Manager__c) {
            ordItm.Customer_Success_Manager__c = Trigger.newMap.get(OrdItm.OrderId).Customer_Success_Manager__c;
        }
                        
        if(ordItm.Line_Item_Start_Date__c == Trigger.oldMap.get(ordItm.OrderId).EffectiveDate) {
            ordItm.Line_Item_Start_Date__c = Trigger.newMap.get(OrdItm.OrderId).EffectiveDate;
        }
                        
        if(ordItm.Line_Item_End_Date__c == Trigger.oldMap.get(ordItm.OrderId).End_Date__c) {
            ordItm.Line_Item_End_Date__c = Trigger.newMap.get(ordItm.OrderId).End_Date__c;
        }
        
        if(ordItm.Pacing__c == Trigger.oldMap.get(ordItm.OrderId).Pacing__c) {
            ordItm.Pacing__c = Trigger.newMap.get(ordItm.OrderId).Pacing__c;
        }
        
        if(ordItm.Pacing_Notes__c == Trigger.oldMap.get(ordItm.OrderId).Pacing_Notes__c) {
            ordItm.Pacing_Notes__c = Trigger.newMap.get(ordItm.OrderId).Pacing_Notes__c;
        }
        
        if(ordItm.Delivery_Contact_Email__c == Trigger.oldMap.get(ordItm.OrderId).Delivery_Contact_Email__c) {
            ordItm.Delivery_Contact_Email__c = Trigger.newMap.get(ordItm.OrderId).Delivery_Contact_Email__c;
        }
        
        if(ordItm.Weekly_Reporting_Days__c == Trigger.oldMap.get(ordItm.OrderId).Weekly_Reporting_Days__c) {
            ordItm.Weekly_Reporting_Days__c = Trigger.newMap.get(ordItm.OrderId).Weekly_Reporting_Days__c;
        }
        
        if(ordItm.Purchase_Order__c == Trigger.oldMap.get(ordItm.OrderId).Purchase_Order__c) {
            ordItm.Purchase_Order__c = Trigger.newMap.get(ordItm.OrderId).Purchase_Order__c;
        }
                                
        Updates.add(ordItm);
    }
2
  • what have you tried so far? have you created a custom metadata? Jan 18, 2022 at 9:52
  • Perhaps a field set would do what you need? These can be managed through the Setup UI for the Object (in the Object Manager).
    – Phil W
    Jan 18, 2022 at 10:59

1 Answer 1

0

A quick approach that can be followed is instead of metadata, you can create a FieldSet, lets say "Copy Attribute Fields" Field Set on Order object (assuming that Order is source and Order Line Item is target). Now using the field set you can determine at run time which attributes needs to be copied on the target object, at this point of time make it necessary that the fields that are available on this fieldset has to be available on Order Line (and also put a appropriate checks while copying and querying on target object that fields does exist and has edit permission as well) object with same API. Write a logic to query/copy the attributes from Order Object and copy it on target Order Line object.

While adding a field on Order Object do make sure that you are adding a field in Order Line object as well if you are including that field in a "Copy Attribute Fields" field set. This way you do not need to modify the copy logic every time you add a new field to Order Object that needs to be copied on Order Line object.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .