Skip to content

Instantly share code, notes, and snippets.

@alexed1
Created April 15, 2020 05:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexed1/b326cc3973d9fe1ab053eedd8cf4d697 to your computer and use it in GitHub Desktop.
Save alexed1/b326cc3973d9fe1ab053eedd8cf4d697 to your computer and use it in GitHub Desktop.
global with sharing class DeserializeJSONtoSObject {
@InvocableMethod
global static List <Results> parse (List<Requests> requestList) {
System.debug('entering object json parse');
//Wrap the Results object in a List container (an extra step added to allow this interface to also support bulkification)
List<Results> responseWrapper= new List<Results>();
for (Requests curRequest : requestList) {
String serializedSObjectString = curRequest.serializedSObjectString;
//Create a Results object to hold the return values
Results response = new Results();
//extract the object type that the user specfied when defining the instance in Flow Builder
Schema.SObjectType curType = response.outputSObject.getSObjectType();
System.debug ('curtype: ' + curType);
String sObjectTypeName = curType.getDescribe().getName();
System.debug ('sObjectTypeName: ' + sObjectTypeName);
Type systemType = Type.forName(sObjectTypeName);
System.debug ('systemType: ' + systemType);
//make the magic happen
SObject curSObject = (SObject)JSON.deserialize(serializedSObjectString, systemType);
System.debug ('curSObject: ' + curSObject);
//add the return values to the Results object
response.outputSObject = curSObject;
responseWrapper.add(response);
}
return responseWrapper;
}
global class Requests {
@InvocableVariable(required=true)
global String serializedSObjectString;
}
global class Results {
@InvocableVariable
global SObject outputSObject;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment