+ Start a Discussion
anup ramanup ram 

executebatch(b,>5)returns too many callouts, how to seperate login & callout methods

Hi All,

I am coding for a S2S integration. I am using batch program to accomplish this. I have written a class for login and callout to external system. I had written both login and callout statements in a single method. The WS is working fine till database.executebatch(batchname,5) i.e scope of 5, scope more than 5 is returning too many callout error.

I was told to seperate the login to external system in Start method of batch and callout in execute method of batch.But I am not able to seperate the same. Please help me.


public class TestWebService {

    public String errMsg{get;set;}
    public String displayError{get;set;}
    public TestWebService()
displayError = 'none';

    public void login(){
        // Login via SOAP/XML web service api
        HttpRequest request = new HttpRequest();
        errMsg+=' 1';
        request.setHeader('Content-Type', 'text/xml;charset=UTF-8');
        request.setHeader('SOAPAction', '""');
        request.setBody('<Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/"><Header/><Body><login xmlns="urn:partner.soap.sforce.com"><username>' + 'uname'+ '</username><password>' + 'pwd'+ '</password></login></Body></Envelope>');
      Dom.XmlNode resultElmt = (new Http()).send(request).getBodyDocument().getRootElement().getChildElement('Body','http://schemas.xmlsoap.org/soap/envelope/').getChildElement('loginResponse','urn:partner.soap.sforce.com').getChildElement('result','urn:partner.soap.sforce.com');       
        // Grab session id and server url
       final String SERVER_URL = resultElmt.getChildElement('serverUrl', 'urn:partner.soap.sforce.com').getText().split('/services')[0];
       final String SESSION_ID = resultElmt.getChildElement('sessionId', 'urn:partner.soap.sforce.com') .getText();
        errMsg+=SERVER_URL+' <--> '+SESSION_ID;
        MyclassWS.myclass ws = new MyclassWS.myclass();
        MyclassWS.SessionHeader_element session = new MyclassWS.SessionHeader_element();
        errMsg+= ' ****** ' + ws.mastermetdh('test') + 'juice in org2';       

I am calling this login() from Batch execute. But i want to split login() to login ( and call it from start) and callout ( and call it from execute ) from this.

Mohith Kumar ShrivastavaMohith Kumar Shrivastava
public class TestCallout {

     public static  void TestCallout(){

    String clientId = '3MVG982oBBDdwyHirurznY0bXVCZCrB6.9sxBfCGtr8ZKVUUzOPjhOcLypgBhgBugAN5XMKbPpNnQKwDCL8rk';
    String clientSecret = '9066826186886123605';
    String reqbody = 'grant_type=password&client_id=clientId&client_secret=clientSecret&username='username'&password='password';

    Http h = new Http();
    HttpRequest req = new HttpRequest();
    req.setEndpoint('https://data-na3--data.cs13.my.salesforce.com/services/oauth2/token');//Note if my domain is set up use the proper domain name else use login.salesforce.com for prod or developer or test.salesforce.com for sandbox instance

    HttpResponse res = h.send(req);



The above code you can run in start method to fetch the oauth token and i suggest you to use REST instead of SOAP