[SalesForce] Get FieldType and Cast String As

I have a list of sObjectsToInsert which will be inserted at the end of a set of scripts. It will contain many different sObjectTypes, so it must be built dynamically (get/put).

One problem I'm running into is when a field type is a non-string, yet I'm passing a string value through via values.get(value)

public void addToInsert(sObject so, Map<String, String> values) {
    for (String value : values.keySet()) {
        so.put(value, values.get(value));
    }
    sObjectsToInsert.add(so);
}

How can I cast values.get(value) as the value field type dynamically?

Best Answer

Change Map<String, String> values to Map<String, Object> values and no casting should be needed.

It is also normally better to use SObjectField as the type of the key in this sort of map.

Be careful about the ordering of the different types in the list: Google "Updating Records for Different Object Types" to read about "chunks".

Related Topic