0

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.

2
  • Have you tried OwnerMap.get(OwnerField)? – Lance Shi Sep 15 '16 at 13:34
  • There is no valueOf method on the SObject class, nor is it a Custom Setting method. Curious that this compiles. What API Version are you using? I can't even compile your example. – Adrian Larson Sep 15 '16 at 13:45
0

Change .valueOf to .get (don't know how I missed this)

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.get(OwnerField)); 

ALSO, during my research/testing, I found an alternate method.

            State_Mapping__c StateMap = [SELECT  **all fields needed** FROM State_Mapping__c where ID = :StateMapID];

            String[] OwnerMap = new List<String>();
            OwnerMap.add((String)Statemap.get(OwnerField));
            system.debug(OwnerMap);

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.