I am trying to populate a field with the value of another field dynamically based on custom metadata types.
I use the method variable.put(fieldAPIName, value)
in order to populate the fields.
Here is an example of my custom metadata types
Basically, I need to copy the value from the 'From Field' to the 'To Field' based on the metadata.
As you can see in the example, the from field contains a reference field that I am not able to pass to the to field. I get the error "Invalid field csordtelcoa__Replaced_Product_Configuration__r.cscfga__originating_offer__c for cscfga__Product_Configuration__c"
.
My code is like this:
for(cscfga__product_configuration__c parentConfig: csConfigs){
for(el_Change_Request_Mappings__mdt tmpCMT : changeMappings){
parentConfig.put(tmpCMT.To_Field__c, parentConfig.get(tmpCMT.From_Field__c));
}
}
My csConfigs list is an SOQL and the result is this:
Select Id, csordtelcoa__Replaced_Product_Configuration__r.cscfga__originating_offer__c,el_Current_Offer__c,cscfga__originating_offer__c,el_Previous_Offer__c , cscfga__Parent_Configuration__c from cscfga__product_configuration__c where id in: clonedIds
The parentConfig
variable is just an outside iteration containing another for loop. Any thoughts of how I could accomplish it?
Best Answer
To retrieve references you will need to use
getSObject(fieldName)
instead ofget(fieldName)
.Something around these lines (a simplified version that needs to be enhanced if more than one level is desired):