Posts

Showing posts from December, 2019

Salesforce: Read JSON Data in Apex class and map with object instance

//JSON Request/Body from the Server/External system String response = '[ {  "name": "account1", "Phone": "12345", "Opportunities" : {records:[{ "name": "opportunity1", "StageName": "1 - Qualified Lead"},{ "name": "opportunity2", "StageName": "1 - Qualified Lead"}]} }, {  "name": "account1", "Phone": "12345" } ]'; //Parse the JSON Body into List of Object (Key with value) List<Object> accountResponse = (List<Object>)JSON.deserializeUntyped(response); Map<String, Object> jsonResponseOpportunityValues; List<Account> AccountList = (List<Account>)JSON.deserialize(response, List<Account>.class); List<Opportunity> OpportunityList = new List<Opportunity>(); //Read one by one list of object instance values to retrive the child object Instance values for(Object ...

Salesforce : Insert parent and child record with Single DML Operation

STEP 1 //******Create the external unique Id field in the parent object**** STEP 2 //create the Parent object instance Account accountRecord = new Account(Name ='Test1',sample_uniqueField__c='12345'); //create the parent record instance with unique key field reference Account caseRecordWithKey = new Account(sample_uniqueField__c='12345'); //create the child record Instance and map with the parrent record instance Opportunity opportunityRecord = new Opportunity(name='Test'2,Accounts = caseRecordWithKey); //add both parent and child records instance into one SObject list SObject[] sobjList = new List<SObject>(); sobjList.add(accountRecord); sobjList.add(opportunityRecord); //perform the DML operation Database.SaveResult[] results = Database.insert(sobjList);