I have a solution that I'm working on where one object has a mapping to a field on another object using a text field.

Let's say Account is the source object, I have a text field called "target field API Name". I want to write a trigger that will create a custom Object called Response and will populate one of the fields on the Response object with a value from the Account. The exact field that will be written from the Account can change, hence the reason for inserting the API name of the target field on the Account record itself.

So, if my Response object has a field with an API name of Test_field__c and the Account object is updated, how could I populate this custom object field?

Something like this? Where I use dynamic data to determine the field to write to on the new record? Could I use getDescribe() to get a map of the object fields and somehow use a get() method to set that value?

    Account acct = new Account(Name='testacct', Target_API_Field_Name__c = 'Test_field__c');    
    for(account a : [SELECT Id, Name, Target_API_Field_Name__c FROM Account]){
        Response__c r = new Response__c();
        string apiName = a.Target_API_Field_Name__c;
        r.apiName = a.Name;

1 Answer 1


You are looking for the SObject.put method:

r.put(apiName, a.Name);
  • You're a gentleman and a scholar! Commented Feb 23, 2018 at 16:59

You must log in to answer this question.

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