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
Steve Thurston 3Steve Thurston 3 

How to get object field value when the field name is stored in a Custom Setting field?

I am trying to integrate with an external third party system that sends an XML response.  I need to map from their XML tags to the Salesforce fields I want to write the values to.  These fields change, so I don't want to hard code the mapping in the APEX code, as that would require a deployment every time there is a change, which means the admin can't do it.

I figure I can use Custom Settings for this.  Namely, I have a Custom Setting with the fields "Salesforce Field" and "Service Field".

My problem is that, while I can read the "Salesforce Field" into a variable in Apex - it's a variable.  How do I tell Apex to write to the actual field?

So it's easy enough to say something like:
   String salesforceWriteField = Custom_Setting__c.Salesforce_Field__c

But all I've achieved is to put the name of the field I want to write to in a variable.  Just as a simple example, if I want to write to the Name field on an Account, it could be:

MyAccount.Name

But I need to use the Custom Setting to tell Apex to write to the "Name" field.  And I can't think of how to do that.

I would like to avoid SOQL, as that is one of the benefits of Custom Settings, and deploying is already a challenge because of the number of SOQL statements that occur across classes during testing.