+ Start a Discussion
Swamy P R NSwamy P R N 

Problem in Bulkifying code, Callout failed..Uncommitted work pending

Hello Everyone, While doing call-outs facing an issue..when i call for few records, its working perfectly but when i call for batches its giving an error:: Uncommitted work pending. IS there a way that we can mass delete the existing records in salesforce in the sense mass delete the existing requisition records

Apex Methods:

public static Boolean bulkify = false;
     public static Map<id, Recruiting_Activities__c> requisitionRecMap ;
      @future(callout=true)
    public static void processAllOpenRequisition(){
        bulkify  = true;
          //Id recssid = ApexPages.currentPage().getParameters().get('id');

        u = [SELECT Username_Broadbean__c,Password_Broadbean__c From USER WHERE id =:UserInfo.getUserId()];
        HttpRequest req = new HttpRequest();
        req.setMethod('POST');
        req.setEndpoint('https://api.adcourier.com/hybrid/hybrid.cgi');
        req.setHeader('Content-Type', 'text/xml');
        requisitionRecMap = new Map<id, Recruiting_Activities__c>([SELECT  Time_now__c,Time_From__c,Recruiter__c, Id FROM Recruiting_Activities__c WHERE EL_Status__c <> 'Closed' AND REcordtype.Name=:'Requisition' /*And (Id=:'a04e0000005RhypAAC' OR Id =: 'a04e0000005Rhyp')*/ AND Req_Id__c IN: reqid LIMIT 5]);
        system.debug('hellooooo'+requisitionRecMap);
        for(Id recid: requisitionRecMap .keySet()){
            //Savepoint sp = Database.setSavepoint();
            readObject(recid);                                    --->Here calling another method, hope that here i'm facing issue. so please clarify me.
           req.setBody(xmlData);
            Http http = new Http();
            req.setTimeout(60000);
             if(!test.isrunningtest()){
                HttpResponse res = http.send(req);
                resbody = res.getBody();
                System.debug('*****'+res.getBody()); // Here you will get the response
             } 
        }
    }

 
  public static void readObject(Id recid){
     rev = [SELECT Time_now__c,Time_From__c,Time_To__c FROM Recruiting_Activities__c WHERE Id=:recid LIMIT 1];
       xmlData ='<?xml version="1.0" encoding="utf-8" ?>'
                +'<AdCourierAPI>'
                +'<Method>RetrieveApplications</Method>'
                +'<APIKey>3012861772</APIKey>'
                +'<Account>'
                    +'<UserName>'+u.Username_Broadbean__c+'</UserName>'
                    +'<Password>'+u.Password_Broadbean__c+'</Password>'
                +'</Account>'
            +'</AdCourierAPI>';
       system.debug('--------'+xmlData);   
 }

So, pleaase update me with the bulkification ASAP.
ShashankShashank (Salesforce Developers) 
For betch records, you might want to use batch apex callouts: http://salesforce.stackexchange.com/questions/15605/http-callouts-in-batch-apex