+ Start a Discussion
nayana pawar 3nayana pawar 3 

Using Batch apex Insert new records in to custom object from json string in salesforce

I am trying to insert json data in to salesforce objects. Json string is too large so i am using batch apex to insert data. For this I had written apex class i didn't got any error But records not inserted in to salesforce. so please check my code below. and tell me what i was doing wrong?

global class CustomerRecordsCtrl implements Database.Batchable<sObject>{
    String query;
 
    global Database.querylocator start(Database.BatchableContext BC){
        return Database.getQueryLocator(query);
    }

    global void execute(Database.BatchableContext BC, List<Customer__c> scope)
    {
            HttpRequest req= new HttpRequest();
            Http http = new Http();
            req.setMethod('GET');
          
            String url = 'http://iprocureafrica.co/index.php/restApi/customer_c/3';
            req.setEndpoint(url);
      
            HttpResponse res = http.send(req);
            String json =  res.getBody().replace('\n','');
          
            parseJSON(json);
  
  
     
    }
      public List<WrapperClass> wrappers{get;set;}
     public void parseJSON(String jsonstr){
        if (jsonstr != null){
        wrappers= new List<WrapperClass>();   
        JSONParser parser = JSON.createParser(jsonstr);
        while (parser.nextToken() != null)
        {
            if (parser.getCurrentToken() == JSONToken.START_ARRAY)
            {
             
                while (parser.nextToken() !=  JSONToken.END_ARRAY)
                {
                    if (parser.getCurrentToken() == JSONToken.START_OBJECT)
                    {
                        
                        WrapperClass ci = new WrapperClass();
                        while (parser.nextToken() !=  JSONToken.END_OBJECT)
                        {
                            
                              
                                if((parser.getCurrentToken() == JSONToken.FIELD_NAME) &&(parser.getText().tolowercase() == 'id'))
                                {
                                    parser.nextToken();    
                                    ci.id = parser.getText();
                                }
                                if((parser.getCurrentToken() == JSONToken.FIELD_NAME) &&(parser.getText().tolowercase() == 'Name'))
                                {
                                    parser.nextToken();
                                    ci.Name = parser.getText();
                                }
                                if((parser.getCurrentToken() == JSONToken.FIELD_NAME) &&(parser.getText().tolowercase() == 'companyname'))
                                {
                                    parser.nextToken()     ;
                                    ci.companyname = parser.getText();
                                }
                                if((parser.getCurrentToken() == JSONToken.FIELD_NAME) &&(parser.getText().tolowercase() == 'phone'))
                                {
                                    parser.nextToken()     ;
                                    ci.phone = parser.getText();
                                }
                                 if((parser.getCurrentToken() == JSONToken.FIELD_NAME) &&(parser.getText().tolowercase() == 'customercode'))
                                {
                                    parser.nextToken()     ;
                                    ci.customercode = parser.getText();
                                }
                                if((parser.getCurrentToken() == JSONToken.FIELD_NAME) &&(parser.getText().tolowercase() == 'agent'))
                                {
                                    parser.nextToken()     ;
                                    ci.agent = parser.getText();
                                }
                                if((parser.getCurrentToken() == JSONToken.FIELD_NAME) &&(parser.getText().tolowercase() == 'supplier'))
                                {
                                    parser.nextToken()     ;
                                    ci.supplier = parser.getText();
                                }
                                if((parser.getCurrentToken() == JSONToken.FIELD_NAME) &&(parser.getText().tolowercase() == 'dateofreg'))
                                {
                                    parser.nextToken()     ;
                                    ci.dateofreg = parser.getText();
                                }
                                if((parser.getCurrentToken() == JSONToken.FIELD_NAME) &&(parser.getText().tolowercase() == 'status'))
                                {
                                    parser.nextToken()     ;
                                    ci.status = parser.getText();
                                }
                                if((parser.getCurrentToken() == JSONToken.FIELD_NAME) &&(parser.getText().tolowercase() == 'geocode'))
                                {
                                    parser.nextToken()     ;
                                    ci.geocode = parser.getText();
                                }
                                if((parser.getCurrentToken() == JSONToken.FIELD_NAME) &&(parser.getText().tolowercase() == 'type'))
                                {
                                    parser.nextToken()     ;
                                    ci.type = parser.getText();
                                }
                              
                                if((parser.getCurrentToken() == JSONToken.FIELD_NAME) &&(parser.getText().tolowercase() == 'salesforcestatus'))
                                {
                                    parser.nextToken()     ;
                                    ci.salesforcestatus = parser.getText();
                                }
                                if((parser.getCurrentToken() == JSONToken.FIELD_NAME) &&(parser.getText().tolowercase() == 'nationalid '))
                                {
                                    parser.nextToken()     ;
                                    ci.nationalid = parser.getText();
                                }
                                if((parser.getCurrentToken() == JSONToken.FIELD_NAME) &&(parser.getText().tolowercase() == 'date'))
                                {
                                    parser.nextToken()     ;
                                    ci.date1= parser.getText();
                                }
                              
                              
                        }
                        wrappers.add(ci);
                         List<Customer__c> InsrtRecord = new List<Customer__c>();  // object list
                          
                    for(WrapperClass wc:wrappers)
                    {
                    Customer__c shp = new Customer__c ();   // object instance with memory allocation.
                    // field mapping
                    shp.Customer_ID__c = Decimal.valueOf (wc.id);  
                    shp.Name= wc.name;
                    shp.Company_Name__c = wc.companyname;
                    shp.Mobile_Number__c = wc.phone;
                  
                    shp.Customer_Code__c = Decimal.valueOf(wc.customercode);
                    shp.Agent__c = Decimal.valueOf(wc.agent);
                    shp.Supplier__c = Decimal.valueOf (wc.supplier);
                    shp.Date_of_Registration__c = Date.valueOf(wc.dateofreg);
                    shp.Status__c = wc.status;
                    shp.Geocode__c = Decimal.valueOf(wc.geocode);
                    shp.Type__c = Decimal.valueOf(wc.type);
                    shp.Salesforce_Status__c = wc.salesforcestatus;
                    shp.National_ID__c = Decimal.valueOf(wc.nationalid);
                
                    InsrtRecord.add(shp); // add to list
                  
                    If(InsrtRecord .size() > 0 ) // check list size
                    {
                      
                         upsert shp ; // insert list.
                      
                    }
                    }
               
      
                  
                            }
                        }
                    }
                }
      }   
    }
  
    public class WrapperClass{
         public string id {get;set;}
        public string name {get;set;}
        public string companyname{get;set;}
        public string phone{get;set;}
        public string customercode{get;set;}
        public string agent{get;set;}
        public string supplier{get;set;}
        public string dateofreg{get;set;}
        public string status{get;set;}
        public string geocode{get;set;}
        public string type{get;set;}
        public string salesforcestatus{get;set;}
        public string nationalid{get;set;}
        public string date1{get;set;}
        public WrapperClass(){
        
        }
    }
   
    global void finish(Database.BatchableContext BC){
    }
  
    public PageReference GoToInsert() {
    return null;
    }
}
Gaurav NirwalGaurav Nirwal
global Database.querylocator start(Database.BatchableContext BC){
    return Database.getQueryLocator(query);
}

global void execute(Database.BatchableContext BC, List<Customer__c> scope)
{
        HttpRequest req= new HttpRequest();
        Http http = new Http();
        req.setMethod('GET');

        String url = 'http://iprocureafrica.co/index.php/restApi/customer_c/3';
        req.setEndpoint(url);

        HttpResponse res = http.send(req);
        String json =  res.getBody().replace('\n','');

        parseJSON(json);  



}
  public List<WrapperClass> wrappers{get;set;}
 public void parseJSON(String jsonstr){  
    if (jsonstr != null){  
    wrappers= new List<WrapperClass>();     
    JSONParser parser = JSON.createParser(jsonstr);  
    while (parser.nextToken() != null)  
    {  
        if (parser.getCurrentToken() == JSONToken.START_ARRAY)  
        {  

            while (parser.nextToken() !=  JSONToken.END_ARRAY)  
            {  
                if (parser.getCurrentToken() == JSONToken.START_OBJECT)  
                {  

                    WrapperClass ci = new WrapperClass();  
                    while (parser.nextToken() !=  JSONToken.END_OBJECT)  
                    {  


                            if((parser.getCurrentToken() == JSONToken.FIELD_NAME) &&(parser.getText().tolowercase() == 'id'))  
                            {  
                                parser.nextToken();      
                                ci.id = parser.getText();
                            }  
                            if((parser.getCurrentToken() == JSONToken.FIELD_NAME) &&(parser.getText().tolowercase() == 'Name'))  
                            {  
                                parser.nextToken();  
                                ci.Name = parser.getText();
                            }  
                            if((parser.getCurrentToken() == JSONToken.FIELD_NAME) &&(parser.getText().tolowercase() == 'companyname'))  
                            {  
                                parser.nextToken()     ;  
                                ci.companyname = parser.getText();  
                            }  
                            if((parser.getCurrentToken() == JSONToken.FIELD_NAME) &&(parser.getText().tolowercase() == 'phone'))  
                            {  
                                parser.nextToken()     ;  
                                ci.phone = parser.getText();  
                            }  
                             if((parser.getCurrentToken() == JSONToken.FIELD_NAME) &&(parser.getText().tolowercase() == 'customercode'))  
                            {  
                                parser.nextToken()     ;  
                                ci.customercode = parser.getText();  
                            } 
                            if((parser.getCurrentToken() == JSONToken.FIELD_NAME) &&(parser.getText().tolowercase() == 'agent'))  
                            {  
                                parser.nextToken()     ;  
                                ci.agent = parser.getText();  
                            } 
                            if((parser.getCurrentToken() == JSONToken.FIELD_NAME) &&(parser.getText().tolowercase() == 'supplier'))  
                            {  
                                parser.nextToken()     ;  
                                ci.supplier = parser.getText();  
                            } 
                            if((parser.getCurrentToken() == JSONToken.FIELD_NAME) &&(parser.getText().tolowercase() == 'dateofreg'))  
                            {  
                                parser.nextToken()     ;  
                                ci.dateofreg = parser.getText();  
                            } 
                            if((parser.getCurrentToken() == JSONToken.FIELD_NAME) &&(parser.getText().tolowercase() == 'status'))  
                            {  
                                parser.nextToken()     ;  
                                ci.status = parser.getText();  
                            } 
                            if((parser.getCurrentToken() == JSONToken.FIELD_NAME) &&(parser.getText().tolowercase() == 'geocode'))  
                            {  
                                parser.nextToken()     ;  
                                ci.geocode = parser.getText();  
                            } 
                            if((parser.getCurrentToken() == JSONToken.FIELD_NAME) &&(parser.getText().tolowercase() == 'type'))  
                            {  
                                parser.nextToken()     ;  
                                ci.type = parser.getText();  
                            } 

                            if((parser.getCurrentToken() == JSONToken.FIELD_NAME) &&(parser.getText().tolowercase() == 'salesforcestatus'))  
                            {  
                                parser.nextToken()     ;  
                                ci.salesforcestatus = parser.getText();  
                            }
                            if((parser.getCurrentToken() == JSONToken.FIELD_NAME) &&(parser.getText().tolowercase() == 'nationalid '))  
                            {  
                                parser.nextToken()     ;  
                                ci.nationalid = parser.getText();  
                            }
                            if((parser.getCurrentToken() == JSONToken.FIELD_NAME) &&(parser.getText().tolowercase() == 'date'))  
                            {  
                                parser.nextToken()     ;  
                                ci.date1= parser.getText();  
                            }


                    }  
                    wrappers.add(ci);  
                     List<Customer__c> InsrtRecord = new List<Customer__c>();  // object list

                for(WrapperClass wc:wrappers)
                {
                Customer__c shp = new Customer__c ();   // object instance with memory allocation.
                // field mapping
                shp.Customer_ID__c = Decimal.valueOf (wc.id);    
                shp.Name= wc.name;
                shp.Company_Name__c = wc.companyname;
                shp.Mobile_Number__c = wc.phone;

                shp.Customer_Code__c = Decimal.valueOf(wc.customercode);
                shp.Agent__c = Decimal.valueOf(wc.agent); 
                shp.Supplier__c = Decimal.valueOf (wc.supplier);
                shp.Date_of_Registration__c = Date.valueOf(wc.dateofreg);
                shp.Status__c = wc.status;
                shp.Geocode__c = Decimal.valueOf(wc.geocode);
                shp.Type__c = Decimal.valueOf(wc.type);
                shp.Salesforce_Status__c = wc.salesforcestatus;
                shp.National_ID__c = Decimal.valueOf(wc.nationalid);

                InsrtRecord.add(shp); // add to list

                If(InsrtRecord .size() > 0 ) // check list size
                {

                     upsert shp ; // insert list.

                }
                }



                        }  
                    }  
                }  
            }  
  }     
}

public class WrapperClass{  
     public string id {get;set;}  
    public string name {get;set;}  
    public string companyname{get;set;}  
    public string phone{get;set;} 
    public string customercode{get;set;} 
    public string agent{get;set;} 
    public string supplier{get;set;} 
    public string dateofreg{get;set;} 
    public string status{get;set;} 
    public string geocode{get;set;} 
    public string type{get;set;}  
    public string salesforcestatus{get;set;} 
    public string nationalid{get;set;}
    public string date1{get;set;}
    public WrapperClass(){  

    }  
}  

global void finish(Database.BatchableContext BC){
}

public PageReference GoToInsert() {
return null;
}