function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
FilikinFilikin 

How to access a field dynamically, given its name

I have a list of fields obtained using a fieldset.

I want to set a value on these fields, but I can't figure out the syntax in Apex.

For example, the email field in the contact object.

 

Is there a way to preform this operation:

contact.email = 'me@somewhere.com'

 

if the token 'email' is in a string?

 

thanks

Best Answer chosen by Admin (Salesforce Developers) 
FilikinFilikin

ok, figured it out using put

contact.put('email', 'me@somewhere.com') seems to do the job

All Answers

FilikinFilikin

ok, figured it out using put

contact.put('email', 'me@somewhere.com') seems to do the job

This was selected as the best answer
LukashPLukashP

Yes, you should do it like that.

Also,

sobject.get('fieldName');

gives you the current field value

FilikinFilikin

thanks, good to have confirmation