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
Jim MontgomeryJim Montgomery 

Error "variable does not exist" using custom setting values in apex trigger

Here is my code. Getting error Variable does not exist: OppFieldName at line 21 column 11.

trigger Update_X_Reference on Opportunity (before update) {
        for(Opportunity Opp : Trigger.new){
        if(opp.is_ams_renewal__c)
         {
       
Opportunity oldOpp = Trigger.oldMap.get(opp.Id);  
Account_x_reference__c xref = [select ID,ams_renewal_email__C,X2019_AMS_Additional_Discount__c, ams_renewal_contact__c from account_x_reference__c where ID = :Opp.Account_X_Reference__c LIMIT 1];

    if(oldOpp.AMS_Renewal_contact__c !=opp.AMS_renewal_contact__c){
    
    TaxUpdateYear__C TUY = TaxUpdateYear__c.getInstance('UpdateYear');
    String OppFieldName = TUY.Opportunity_Field_Name__c;
    String XRefFieldName = TUY.X_Ref_Field_Name__c;
    xref.ams_renewal_contact__c = opp.ams_renewal_contact__c;
    
    }
if(oldOpp.ams_renewal_email__c != opp.ams_renewal_email__c)
{
    xref.AMS_renewal_email__c = opp.ams_renewal_email__c;
}
if(oldOpp.OppFieldName != opp.OppFieldName)
{
    xref.XRefFieldName = opp.OppFieldName;
}

update xref;
}
{
return;
}
}
}
Raj VakatiRaj Vakati
You need to use the dynamic apex to get the field value dynamically like below  use  Dynamic Apex and also the methods described on the SObject class, specifically in your case the 'get' methods
 
String mycustomattValue = (String) opportunity.get(mycustomatt);
Or
 
CustomObject.put(customObjFieldName, account.get(accountfieldName));


 https://salesforce.stackexchange.com/questions/8325/retrieving-value-using-dynamic-soql
https://salesforce.stackexchange.com/questions/4193/update-a-records-using-generic-fields
https://developer.salesforce.com/forums/?id=906F00000008xujIAA
Deepali KulshresthaDeepali Kulshrestha
Hi Jim,

Here is a Screenshot. You can see your fault.

User-added image




I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha
Jim MontgomeryJim Montgomery
Where should I move it to?
Tried moving it up to where I am declaring the other variables, but still no luck.