Is it possible to refer a string as a field API name?

I'm trying to achieve something like below:

Method(string fieldAPI){
   Contact con = new Contact();
   con.fieldAPI = 'Abc';
   update con;
  • What do you mean by "refer a string"? Do you mean "reference a string"? – Peter Mortensen May 15 at 23:08
  • I mean instead of Field API name, I would like to use string variable, which will contain my API name. – salesforce Developer May 16 at 6:22

What you have will still work because you are referring the field name here.

con.fieldName__c = 'Abc';

You can additionally use Sobject.put(fieldName, value) here if you want to refer the API name as string value, as:

con.put('fieldName__c', 'Abc');
  • Doesn't "refer" / "referring" go hand in hand with "to"? – Peter Mortensen May 15 at 23:10
  • @PeterMortensen English was never my first language :), if you feel it can be corrected/improved, please feel free to edit it. – Jayant Das May 16 at 1:14

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.