2

I have a scenario where I need to check in opportunity update if particular field values of opportunity which are mentioned in metadata are changed or not, if these values are changed meaning that that record would be considered for update. I have done this with field sets but not sure how we can achieve the same using custom metadata. Attaching the code used for field sets here

Public static boolean isValuesChanged(List<Opportunity> newOpportunitiesList, Map<id,Opportunity> oldOpportunityMap)
{
    for(Opportunity oppRecord : newOpportunitiesList)
    {
         
       
            for(Schema.FieldSetMember fieldSetMemberObj : SObjectType.Opportunity.FieldSets.Opportunity_Comparision_FieldSet.getFields()) 
            {
                if(oppRecord.get(fieldSetMemberObj.getFieldPath()) != oldOpportunityMap.get(oppRecord.id).get(fieldSetMemberObj.getFieldPath()) && oppRecord.Amount > 0)
                {
                    return true;
                }
            }
        
    }
    return false;
}

This is what I have done when I used field sets. The same I want to do using custom metadata.How can I check changes in Apex ? Thanks in Advance

unflagged.destination
  • 1,576
  • 3
  • 19
  • 38

1 Answers1

0

Cut it into 2 problems.

  1. Given a set of strings with field api names - how to detect changes
Set<String> fields = new Set<String>{'Name', 'CloseDate', 'StageName'};
Set<Id> oppsToProcess = new Set<Id>();

for(Opportunity opp: newList){
    Opportunity old = oldMap.get(opp.Id);
    for(String field : fields){
        if(opp.get(field) != old.get(field)){
            oppsToProcess.add(opp.Id);
            break;
        }
    }
}
  1. Given a custom metadata with field names - how do I make a set of strings out of it.

Solution for this one depends what exactly you have in custom meta:

  • list with multiple records, each with single field's name?
  • 1 record with field names saved in columns like Field1__c, Field2__c, Field3__c
  • 1 record with list of fields stored comma-separated, like Fields__c = 'Name,CloseDate,StageName'

You'll have to do it yourself, query and loop through first one or call String.split(','); on the last one or...

eyescream
  • 18,088
  • 2
  • 34
  • 46