0

I'm trying to get a field value of a Sobject. for example for Case.

String cas = 'Case';
String caseid = '5003O000008QNUOQA4';
String query = 'SELECT Id,RecordTypeId,Answer_Type__c FROM ' + cas+  ' WHERE Id = \'' + caseid + '\'';
List<SObject> casList = Database.query(query);
System.debug(casList[0]); // Debug Result: Case:{Id=5003O000008QNUOQA4, RecordTypeId=0121v000000X31eAAC, Answer_Type__c=Qst17}
System.debug(casList[0].Id); //Debug Result: 5003O000008QNUOQA4

But System.debug(casList[0].Answer_Type__c); give me an error :

Variable does not exist: Answer_Type__c.

And also System.debug(casList[0].RecordTypeId); give me the same error :

Variable does not exist: RecordTypeId.

I guess I cant run in because Sobject as a class doesn't have the fields, but how can I get this data?

Many thanks,

0

1 Answer 1

4

Using the SObject Class, you can leverage get() where the single input parameter is the field API name you are looking to retrieve a value within.

String answerType = (String) casList[0].get('Answer_Type__c');
1
  • Thank you @TSmith
    – Salvation
    Jun 7, 2022 at 13:39

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.