Have you ever wanted to create an SObject in memory, and write to one or more fields that are normally unwriteable? For instance, CreatedDate, or a formula field. You might want to do this in a unit test class. Or you might have a formula field in an SObject held by a VisualForce controller that you want to update before committing to the database. Normally, Apex won’t let you do that. Here’s a workaround.

public static SObject makeSObjectFromFields(Map<String, String>fields, System.Type targetType) {
    String jsonrep = json.serialize(fields);
    return (SObject)json.deserialize(jsonrep, targetType);

Here’s an example of calling that method:

OpportunityLineItem oli = (OpportunityLineItem)SchemaUtility.makeSObjectFromFields(
    new Map<String,String>{
        'ListPrice' => String.valueOf(pbe.UnitPrice)
Written on