[SalesForce] Get Field value from object using string key

I am trying to do get values from the object using string key, I have typecast the Contact object into the object but not getting value. Please help how to get value from the object using string API filed

List<String> lstfiedName = new List<String>{'LastName','Email','Phone'};     
for(Contact objContact : lstofContact){
 object obj = (object)objContact;
  for(String fielName : lstfiedName){
      System.debug('lastname::'+obj.get(fiedName));
  }
}

Getting Error :
Method does not exist or incorrect signature: void get(String) from the type Object

Best Answer

try with sObject instead of Contact since get and set methods are available in sObject class. Replace your Contact objContact with sObject objContact in for loop. Also, try without Object obj as it should work fine without casting it to Object type

Related Topic