0

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;
    }
4

You are looking for the SObject.put method:

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.