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
mgodseymgodsey 

Improving Code Efficiency - Getting field for specific Opportunity from field name

I have a class that compares the values of fields on two different Opportunities, and if they have different values some logic occurs. Right now I am using a long list of "IF" statements, but I've been tasked to make it more efficient. Here is a sample of the code right now:

 

//check each field to see if has a different value from the cloned from opp.
//I'm doing this one field at a time, because they have to be added to the Whats Changed field in a specific (page layout) order
//also, a few fields will be treated differently in terms of what goes into the What's Changed field
    if(opp.ClientStrategist__c != clonedFromOpp.ClientStrategist__c){          
        Changes.add(createChangeStringNoChangeDisplay(Opportunity.ClientStrategist__c, fieldToLabel, 'Opportunity'));
    }
            
    if(opp.AccountId != clonedFromOpp.AccountId){
        Changes.add(createChangeStringNoChangeDisplay(Opportunity.AccountId, fieldToLabel, 'Opportunity'));
    }
            
    if(opp.Buyer__c != clonedFromOpp.Buyer__c){
        Changes.add(createChangeStringDisplayChanges(Opportunity.Buyer__c, fieldToLabel, clonedFromOpp.Buyer__c));
    }
            
    if(opp.Agency__c != clonedFromOpp.Agency__c){              
        Changes.add(createChangeStringNoChangeDisplay(Opportunity.Agency__c, fieldToLabel, 'Opportunity'));
    }

 

I want to try and change it so that rather than creating an IF statement for each field, I use a for loop to check each one. Something along the lines of:

 

List<sObjectField> fieldsList = new List<sObjectField>{
    Opportunity.ClientStrategist__c,
    Opportunity.AccountId,
    Opportunity.Buyer__c            
};
            
for(String field: fieldsList){
    if(opp.field != clonedFromOpp.field){
        Changes.add(creativeChangeStringDisplayChanges(field, fieldToLabel, clonedFromOpp.field, 'Opportunity')
    }
}

The issue I'm having is that in some parts I need the sObject Field name (i.e. 'Opportunity.ClientStrategist__c) and in other places I need the variable (i.e. 'opp.ClientStrategist__c).

 

Is there any way I can use the sObject Field name to get the variable? For example, in java I think we chould use something like 

opp.getField(field). Is there something similar in Apex?

 

Thank you in advance for any help!

firechimpfirechimp

Hi mgodsey,

 

If I understand you correctly what you can use the following:

List<String> fieldsList = new List<String>{
    'OClientStrategist__c',
    'AccountId',
    'Buyer__c'            
};
            
for(String field: fieldsList){
    if(opp.get(field) != clonedFromOpp.get(field)){
        Changes.add(creativeChangeStringDisplayChanges(field, fieldToLabel, clonedFromOpp.get(field), 'Opportunity')
    }
}

 So as you can see the sObject has an instance method of .get() to use this you pass in the API name of the field as a string, and then apex investigates the sObject to see if it can return the value for that field, if not it returns null.

 

You can find more on this method if you take a look at the apex doc's for sObject instance methods.

 

Hope this helps!