0

Lets say I have an SObject record

{
   Id: "abc",
   Field_A__c : "field A value",
   Field_B__c : "field B value"
}

and a string "Field_A__c".

In apex, is there anyway i can get the data "field A value" using just the record and the string?

if the field was not a string i could have done sobject.Field_A__c to get the value, but that is not the case.

Bonish Koirala
  • 641
  • 5
  • 16

1 Answers1

1

You can use the get() method of SObject class.

The get() method takes String as argument and returns Object

Here is an example:

SObject sObjectObject = [SELECT Id
                         , Name
                         , Field_A__c 
                         FROM Account 
                         WHERE Field_A__c != NULL LIMIT 1];
String valueOfFieldC = (String)sObjectObject.get('Field_A__c');//Typecast Object to String
System.debug( 'valueOfFieldC=' + valueOfFieldC );
वरुण
  • 1,237
  • 3
  • 18
  • 50