Can i dynamically assign a sobject field a value.? Due to the poor data model setup of existing I am forced to do this

My issue is fieldname on object is stored at some other object.Say for eg i have a string fieldname where i store actualfieldname of the object.Now i want to update this field of the object as below

string fieldname = 'Name'
test__C c = new test__C();
c.fieldname = 'Hello';

This throws me an error.Is there any other way to achieve this?


Yes, that is possible (see also the documentation for all sObject methods):

Use c.put(fieldname,'Hello')

  • 3
    You can also use c.get(fieldname); to get the value from the SObject dynamically. See here for more info
    – BarCotter
    Oct 24 '14 at 15:13

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.