0

I have the following function:

public SObject PopulateSObjectFieldsForJSON(JSONParser parser)
{
   if (sobjectType == 'Queue_BH__c') {
        Queue_BH__c qbh = (Queue_BH__c)parser.readValueAs(Queue_BH__c.class);
        return qbh;
    } else if (sobjectType == 'Time_Zone__c') {
        Time_Zone__c tz = (Time_Zone__c)parser.readValueAs(Time_Zone__c.class);
        return tz;
}

This function receives JSONParse and returns an object with the fields from the JSON. how can i make it generic so i dont have to handle any object separately like in the above?

1
  • 1
    FYI, usually the JSON class is easier to use than the JSONParser class; the JSON class has several deserialize options.
    – Keith C
    Aug 28, 2019 at 10:27

1 Answer 1

2

You can directly use SObject:

public SObject PopulateSObjectFieldsForJSON(JSONParser parser)
{
    return (SObject)parser.readValueAs(SObject.class);
}

In fact, you dont really need separate method. You can do:

Queue_BH__c qbh = (Queue_BH__c)parser.readValueAs(SObject.class);

And actually better to use below using JSON class as you dont really need parser:

Queue_BH__c qbh = (Queue_BH__c)JSON.deserializeStrict(resp, SObject.class);

You must log in to answer this question.

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