I'm trying to query a record's fields based on a calculated result from another record. So, if the result is "A", then on State_Mappping__c object I want to query A__c field and get the value.
Executed in Anonymous Window
String StateMapID = 'a0323000000n7CcAAI'; //Setting ID String Result = 'A'; String oquery; String OwnerField = Result + '_Owner__c'; // this should set the A_Owner__c field oquery = 'Select ' + OwnerField + ' from State_Mapping__c where ID = \''+ String.escapeSingleQuotes(StateMapID)+'\'' ; // Query the A_Owner__c field State_Mapping__c OwnerMap = database.query(oquery); Schema.SObjectType sot = OwnerMap.getSObjectType(); Schema.DescribeSObjectResult res = sot.getDescribe(); Set<String> StateFields = res.fields.getMap().keyset(); system.debug(OwnerMap.valueOf(OwnerField));
The current debug result is = "A_Owner__c"
I want it to be the value of the field so I can assign it to a variable and run further queries/code against it.