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
vishesh91vishesh91 

dynamically access sobject field

Hi I have a situation where users wants to update fields of an sobject.User will provide field name to be updated and tha new value.How do I access the same field on SObject.Do I have to use switch for all fields.then What if new field is added to sobject ?

Best Answer chosen by Admin (Salesforce Developers) 
sfdcfoxsfdcfox

See SObject.get() and SObject.put() in the language reference for details, but basically, you can pass in strings to get and set fields.

 

Examples:

 

user u = [select id,isactive,email,username from user where id = :userinfo.getuserid()];
system.assertequals((boolean)u.get('isactive'));
u.put('isactive',false);
database.update(u,false); // this won't work, can't deactivate self