I need to dynamically set some values to create/update records of a custom object.

The main idea is to create something like this:

MyCustomObject.fields["fieldName"].Value = "some value";

Does Apex have any way to do that?

  • 1
    Is it a custom SObject or an instance of a custom Apex Class?
    – Adrian Larson
    Jul 14, 2017 at 15:42

2 Answers 2


Object is an ambiguous term in this context. If by custom object you mean SObject, then it is straightforward as you can use the put method inherited from the SObject class:

MyCustomObject__c record = new MyCustomObject__c();
record.put('MyCustomField__c', 'Some Value');

You can use the put method of the sObject class. This could look like this: customObj.put('fieldname', field value)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .