+ Start a Discussion
sailersailer 

bulkify the code for callout

Hello Everyone,
I worte the code where i am able to connect the endpoint and get the values and update ,and it works awesome.
But i can only 150 DML statement ,so i thought of bulkify
But could not able to get how to add in the list
Code is here
public class JSONParserUtil {

    @future(callout=true)
   
   public static void parseJSONResponse(String Acctname,String Ids)
   // public static void parseJSONResponse()
     {      
        String AcctNameJson;
        system.debug('AccountName______________________________'+AcctName);
        String JSONContent = '{"folionum":'+AcctName+'}';
        System.debug('JSONContent____________________'+JSONContent);
        Http httpProtocol = new Http();
        // Create HTTP request to send.
        HttpRequest request = new HttpRequest();
        // Set the endpoint URL.
        String endpoint = 'http://abc.xyz.net/api/values';
        request.setEndPoint(endpoint);
        request.setHeader('content-type', 'application/json; charset=utf-8');
        request.setMethod('POST');
       // String strbody = '{"folionum":48430721840}';
        request.setBody(JSONContent);
        HttpResponse response = httpProtocol.send(request);
       // String jsonInput = response.getBody(); UCOMMENETED TO READ TEH JSON
        JSONParser parser = JSON.createParser(Response.getBody());
        system.debug('JSON PARESER___________________'+parser );
        system.debug('Respone'+Response.getBody());
        double sqft ;
        String avgrentpsft ;
        String months12pavg ;
        String monthsps12ftavg ;
        String peakspavg ;
        String peakspsftavg ;
       
        
         while (parser.nextToken() != null)
         {
           //if ((parser.getCurrentToken() == JSONToken.FIELD_NAME) &&(parser.getText() == 'sqft') &&(parser.getText() == 'avgrentpsft')&&(parser.getText() == '12monthspavg')
            //&&(parser.getText() == '12monthspsftavg') &&(parser.getText() == '12monthspsftmax') &&(parser.getText() == 'peakspavg') &&(parser.getText() == 'peakspsftavg'))
           
            //if ((parser.getCurrentToken() == JSONToken.FIELD_NAME) &&(parser.getText() == 'sqft') (parser.getText() == 'sqft'))
            if (parser.getCurrentToken() == JSONToken.FIELD_NAME)
             {
                
                 String text = parser.getText();
              parser.nextToken();
              if(text == 'sqft')
              {
               sqft = parser.getDoubleValue();
              }else if(text == 'avgrentpsft'){
               avgrentpsft=parser.getText();
              }
              else if(text == '12monthspavg'){
               months12pavg =parser.getText();
              }
              else if(text == '12monthspsftavg'){
               monthsps12ftavg =parser.getText();
              }
              else if(text == '12monthspsftmax'){
               monthsps12ftavg=parser.getText();
              }
              else if(text == 'peakspavg'){
               peakspavg=parser.getText();
              }
              else if(text == 'peakspsftavg'){
               peakspsftavg=parser.getText();
              }
                 /* sqft=parser.getDoubleValue();
                  avgrentpsft=parser.getText();
                   months12pavg=parser.getText();
                    monthsps12ftavg=parser.getText();
                     peakspavg=parser.getText();
                  peakspsftavg=parser.getText();
                   parser.nextToken();
                   */
     
             }
          }
                    system.debug('TotalAqft' + sqft);
                   
         if (response.getStatusCode() == 200) {

      //update account
    
      Property_Obj__c acc = new Property_Obj__c(Id=Ids);
      acc.Ave_Rent_sf_Per_Area__c =sqft;
      acc.Brkr_Remrks_2__c=avgrentpsft;
      acc.Garag_Desc__c=months12pavg;
      acc.Brkr_Remarks__c=monthsps12ftavg;
      acc.city__c=monthsps12ftavg;
      acc.DOM__c=peakspavg;
      acc.Address__c=peakspsftavg;
      update acc;
    } else {
      System.debug('Callout failed: ' + response);
    }  
         
                   
}
}

Please help out .

Regards,
Sailer
ShaTShaT
Hi,

Add the acc in a list of account and than update the account.
update account list after loop is finished.

public class JSONParserUtil {

    @future(callout=true)
  
   public static void parseJSONResponse(String Acctname,String Ids)
 public list<account>accountUpdate= new list<account>();
   // public static void parseJSONResponse()
     {     
        String AcctNameJson;
        system.debug('AccountName______________________________'+AcctName);
        String JSONContent = '{"folionum":'+AcctName+'}';
        System.debug('JSONContent____________________'+JSONContent);
        Http httpProtocol = new Http();
        // Create HTTP request to send.
        HttpRequest request = new HttpRequest();
        // Set the endpoint URL.
        String endpoint = 'http://abc.xyz.net/api/values';
        request.setEndPoint(endpoint);
        request.setHeader('content-type', 'application/json; charset=utf-8');
        request.setMethod('POST');
       // String strbody = '{"folionum":48430721840}';
        request.setBody(JSONContent);
        HttpResponse response = httpProtocol.send(request);
       // String jsonInput = response.getBody(); UCOMMENETED TO READ TEH JSON
        JSONParser parser = JSON.createParser(Response.getBody());
        system.debug('JSON PARESER___________________'+parser );
        system.debug('Respone'+Response.getBody());
        double sqft ;
        String avgrentpsft ;
        String months12pavg ;
        String monthsps12ftavg ;
        String peakspavg ;
        String peakspsftavg ;
      
       
         while (parser.nextToken() != null)
         {
           //if ((parser.getCurrentToken() == JSONToken.FIELD_NAME) &&(parser.getText() == 'sqft') &&(parser.getText() == 'avgrentpsft')&&(parser.getText() == '12monthspavg')
            //&&(parser.getText() == '12monthspsftavg') &&(parser.getText() == '12monthspsftmax') &&(parser.getText() == 'peakspavg') &&(parser.getText() == 'peakspsftavg'))
          
            //if ((parser.getCurrentToken() == JSONToken.FIELD_NAME) &&(parser.getText() == 'sqft') (parser.getText() == 'sqft'))
            if (parser.getCurrentToken() == JSONToken.FIELD_NAME)
             {
               
                 String text = parser.getText();
              parser.nextToken();
              if(text == 'sqft')
              {
               sqft = parser.getDoubleValue();
              }else if(text == 'avgrentpsft'){
               avgrentpsft=parser.getText();
              }
              else if(text == '12monthspavg'){
               months12pavg =parser.getText();
              }
              else if(text == '12monthspsftavg'){
               monthsps12ftavg =parser.getText();
              }
              else if(text == '12monthspsftmax'){
               monthsps12ftavg=parser.getText();
              }
              else if(text == 'peakspavg'){
               peakspavg=parser.getText();
              }
              else if(text == 'peakspsftavg'){
               peakspsftavg=parser.getText();
              }
                 /* sqft=parser.getDoubleValue();
                  avgrentpsft=parser.getText();
                   months12pavg=parser.getText();
                    monthsps12ftavg=parser.getText();
                     peakspavg=parser.getText();
                  peakspsftavg=parser.getText();
                   parser.nextToken();
                   */
    
             }
          }
                    system.debug('TotalAqft' + sqft);
                  
         if (response.getStatusCode() == 200) {

      //update account
   
      Property_Obj__c acc = new Property_Obj__c(Id=Ids);
      acc.Ave_Rent_sf_Per_Area__c =sqft;
      acc.Brkr_Remrks_2__c=avgrentpsft;
      acc.Garag_Desc__c=months12pavg;
      acc.Brkr_Remarks__c=monthsps12ftavg;
      acc.city__c=monthsps12ftavg;
      acc.DOM__c=peakspavg;
      acc.Address__c=peakspsftavg;
     accountUpdate.add(acc);
    } else {
      System.debug('Callout failed: ' + response);
    } 
        
                  
}

if(accountUpdate.size >0){
update accountUpdate;
}

}

Thanks
Shailu
sailersailer
Hello Shailu,

Thanks for the response,Whenever the records is inserted i am writing the trigger.
trigger AccountCallout on Property__c(after insert)
{
        for (Property__c acc : Trigger.New) {
        String AcctName=acc.Name;
        String ids=acc.Id;
        JSONParserUtil.parseJSONResponse(AcctName,Ids);//here i am calling this method ,but if we take a single record we cross governor limits
                                                                                                          need help in bulkfying this method so that it wont cross governor limits
        //JSONParserUtil.parseJSONResponse();
    
     }
}
SwaroopaSwaroopa
Hi Sailer,

Trigger works on bulk records. As suggested by sailu if you update the account by using list the trigger will call for all Accounts