So how to pass sObject to future methods, even when we know that the future methods do not accept the sobjects. So how do we do it?
We will use the JSON methods to first serialize the list of account which will be serialized as string. This string is passed to the future method and then in the future method we will de-serialize the string back to list of account and insert the accounts.
Here’s the code;
This is the main class, we will call it GenerateJson
public class GenerateJson{ //Constructor public GenerateJson(){ // I have hardcoded the name and the account number in Kannada!! account acc1 = new account(name = 'ಬೆಂಗಳೂರು', AccountNumber= '೧೦೨೯೩೭'); account acc2 = new account(name ='ಹುಬ್ಬಳ್ಳಿ', AccountNumber= '೬೮೬೮೮೮೦'); //Initialize a list of account list<account> accList = new list<account>(); accList.add(acc1); accList.add(acc2); //Using the JSON.serializePretty we will serialize the list of account. string jsonString = JSON.serializePretty(accList); //Pass the serialized list to the future method. DeserializeJsonRecords.insertAccount(jsonString); } }
We will create another class with a future method. We will name the class DeserializeJsonRecords
public class DeserializeJsonRecords{ @future public static void insertAccount(string accountString){ //Deserialize the list and cast it to account list. list<account> insertAccList = (list<account>)JSON.deserializeStrict(accountString, list<account>.class); //Insert the accounts insert insertAccList; } }
To check if the code works just run the below line in the developer console
GenerateJson genJson = new GenerateJson();
One of the catch here is if the size of the list is very huge then there are chances that the heap size limit will be reached. Also I am not sure about how the the rich text area fields are deserialized, will need to test that.
Hope it helps!
Reblogged this on Sutoprise Avenue, A SutoCom Source.
Reblogged this on Sutoprise Avenue, A SutoCom Source.