3

I have a Map<String,String> containing <field API Name, field value>

Is there a way to insert them into an SObject that contains those field names exactly?

My SObject has hundreds of fields and I only want to insert those I have in the map.

1 Answer 1

7

All SObjects offer these methods that include a put by field API name method so the values can be transferred like this:

Map<String, String> m = ...;    // Could also be Map<String, Object>
SObject sob = ...;

for (String f : m.keySet()) {
    sob.put(f, m.get(f));
}
3
  • can i also dynamically check the type of field and convert my value to that type? @Kieth C
    – Shyam Raju
    Mar 19, 2017 at 22:12
  • would making my map<String,Object> solve that problem for me?
    – Shyam Raju
    Mar 19, 2017 at 22:13
  • 1
    Using Map<String, Object> is a good way to go as it lets you insert values of type String, Decimal, Boolean etc with the type maintained when the values are put into the SObject.
    – Keith C
    Mar 19, 2017 at 22:18

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.