-
ChatterFeed
-
0Best Answers
-
0Likes Received
-
0Likes Given
-
7Questions
-
6Replies
How to insert more than 30000 records in salesforce using batch apex.
 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"
- nayana pawar 3
- October 31, 2014
- Like
- 0
“Read Timed Out” Error : Batch Processing
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.
- nayana pawar 3
- October 18, 2014
- Like
- 0
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;
   }
}
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;
   }
}
- nayana pawar 3
- October 13, 2014
- Like
- 0
Using Batch apex Insert new records in to custom object from json string in salesforce.
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;
   }
}
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;
   }
}
- nayana pawar 3
- October 13, 2014
- Like
- 0
Insert new records in to custom object from json string in salesforce.
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.
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.
- nayana pawar 3
- October 08, 2014
- Like
- 0
JSON Parser from url in apex
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.
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.
- nayana pawar 3
- September 26, 2014
- Like
- 0
How to get previously added record
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 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.
- nayana pawar 3
- September 04, 2014
- Like
- 0
“Read Timed Out” Error : Batch Processing
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.
- nayana pawar 3
- October 18, 2014
- Like
- 0
Using Batch apex Insert new records in to custom object from json string in salesforce.
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;
   }
}
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;
   }
}
- nayana pawar 3
- October 13, 2014
- Like
- 0
Insert new records in to custom object from json string in salesforce.
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.
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.
- nayana pawar 3
- October 08, 2014
- Like
- 0
How to get previously added record
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 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.
- nayana pawar 3
- September 04, 2014
- Like
- 0