• nayana pawar 3
  • NEWBIE
  • 0 Points
  • Member since 2014

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 7
    Questions
  • 6
    Replies
 I have json string containing 30,000 record data i want to insert that records into salesforce.I have API url that contains this json string.Can any one please tell me is it possible to insert records more than 30,000 in salesforce.If yes how can i do this task.Using batch apex or any other method to do this. Please reply me as soon as possible.
My API URL:    "http://iprocureafrica.co/index.php/restApi/sales_c/3"
I am trying to add records from API URL using batch apex in salesforce.I got an error while running batch apex. Please help me to solve this error.
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;
    }
}
I am trying to insert new records in to salesforce. but when json string is long then due to governor limit it gives me error. so now i am tying to use batch apex. But my code is not working. I didn't got any error but records not inserted.  please help me. check my code below.

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;
    }
}
I am working with the new JSON Parsing functionality,
I have API URL that contain json string.I have created one VF page that displays JSON string data in pageblock table in record format from API URL.
Now i want to insert that json string into custom object records. How can I insert new records fron json string to my custom object.
Hello All,
I am new in salesforce apex. Please help me to write VF apex code that parse JSON from API url and also shows value in VF page. Insert every record in salesforce contact object.
Hello,
I have created one VF page that shows last added record in edit mode But in that page i also have Previous button when
I click on this button I want record which is previously added by me IN EDIT MODE. I tried different logics but It's not working.
I also have next button in that VF page.
Please help me to write code and SOQL query so that I fetch record.
I am trying to add records from API URL using batch apex in salesforce.I got an error while running batch apex. Please help me to solve this error.
I am trying to insert new records in to salesforce. but when json string is long then due to governor limit it gives me error. so now i am tying to use batch apex. But my code is not working. I didn't got any error but records not inserted.  please help me. check my code below.

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;
    }
}
I am working with the new JSON Parsing functionality,
I have API URL that contain json string.I have created one VF page that displays JSON string data in pageblock table in record format from API URL.
Now i want to insert that json string into custom object records. How can I insert new records fron json string to my custom object.
Hello,
I have created one VF page that shows last added record in edit mode But in that page i also have Previous button when
I click on this button I want record which is previously added by me IN EDIT MODE. I tried different logics but It's not working.
I also have next button in that VF page.
Please help me to write code and SOQL query so that I fetch record.