function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
SFDC Dev1 13SFDC Dev1 13 

Wrapper class for Multiple objects

Hello Developers,

I need your pointers for updating data into three objects, Location,Account and Contact. My scenario is, we have to consume 3rd party service and parse the data into three above mentioned objects and this should de done in batches.Records in salesforce will be there, we need to just update the same records with response received from json. So i have written batch class,
1. I was able to consume service, get the response, parse json.
2. I was able to map fields on child object(Location)
3. I'm having difficulties in mapping Account and contact. Can you share some pointers on how to acheive this.
Below is my code for your reference.
global class BatchUpdate implements Database.Batchable<sObject>,Database.AllowsCallouts,Database.Stateful{

   global Database.QueryLocator start(Database.BatchableContext BC){
   String SOQL = 'Select Name from Location__c where name =\'T4375\'';     
      return Database.getQueryLocator(SOQL);
   }
     
   global void execute(Database.BatchableContext BC,List<Sobject> scope){
        String[] otNames = new String[] {};
        string s;
           system.debug('im from Execute');
           for(Sobject lc : scope){           
               for(Location__c loc: [Select Name from Location__c where name = 'T4375']){
                   otNames.add(loc.Name);
                }
                s = String.join(otNames, '|');                      
            } 
 
            system.debug(s);
            Http http = new Http();
            HttpRequest request = new HttpRequest();
            request.setEndpoint(ENDPOINT URL);
            request.setMethod('POST');
            request.setHeader('Content-Type', 'application/json;charset=UTF-8');
            request.setHeader('Accept', 'application/json');
            // Set the body as a JSON object
            request.setBody(s);
            request.setTimeout(60000);
            HttpResponse response = http.send(request);
            // Parse the JSON response
            System.debug(response.getBody());
            string jsonresponse = response.getBody();
            String ssss = jsonresponse.unescapeEcmaScript();
            system.debug(ssss);
            ssss = ssss.substring(1, ssss.length()-1);
            system.debug('response after trimming---->>>>'+ssss);

            List<Object> results = (List<Object>)JSON.deserializeUntyped(ssss);
            system.debug('results'+results);
        
            List<ParseJson> responseList = (List<ParseJson>)JSON.deserialize(ssss, List<ParseJson>.class);
            system.debug('response is '+responseList);
            
  
            List<location__c> lct = new List<location__c>();
            List<location__c> finallst = Database.query('SELECT Id,Location__c,Account__r.MSD_Code__c FROM location__c WHERE Name = \'OT43757\'');
            
             for(ParseJson lcc:responseList){
                 location__c mylct= new location__c();
                 mylct.location__c=lcc.Address_3;
                 mylct.key__c     =lcc.Key;
                 mylct.Account__r.MSD_Code__c=lcc.Chain_Name_Code ;
                 mylct.id='a090k000001UoAN';
                 finallst.add(mylct);
             }
            map<id,location__c> lccmap = new map<id,location__c>();
            //put all the values from the list to map. 
            lccmap.putall(finallst);
            if(lccmap.size()>0){
            update lccmap.values();
            }
            update finallst;

   }

    

   global void finish(Database.BatchableContext BC){
     system.debug('im from finish');
   }
   

            public class ParseJson {
                public String Key;  // Child object field
                public String No;   // Child object field
                public String Name_Code;  // Parent(Account)
                public String Contact;        // Contact Info
                public String Address; // Parent(Account
                public String Address_A; // Parent(Account
                public String Address_B; // Parent(Account
                public String Post_Code; // Parent(Account
               }

    
            public static List<ParseJson> parse(String ssss) {
               return (List<ParseJson>) System.JSON.deserialize(ssss, List<ParseJson>.class);
            }
        
        
 }