1

I'm getting System.LimitException: Too many callouts: 101 error message in my WSDL class file generated by SFDC.

Repeated error was shown as following:

Class.ServiceHistory_wsdl.SF_ServiceHistoryObj.SrvHist: line 130, column 1 Class.SvcHistWebServiceMock.doInvoke: line 19, column 1

Error on line 130 was from:

WebServiceCallout.invoke(
              this,
              request_x,
              response_map_x,
              new String[]{endpoint_x,
              '',
              'urn:HYU-SF-ServiceHistory:SF_ServiceHistory',
              'SrvHist',
              'urn:HYU-SF-ServiceHistory:SF_ServiceHistory',
              'SrvHistResponse',
              'ServiceHistory_wsdl.SrvHistResponse_element'}
            );

And error on line 19 was from:

sh = sho.SrvHist('001', 'sysid', Date.valueOf('2015-02-01'), Date.valueOf('2015-02-02'));

I needed to do the callout synchronously as I need to upsert the return rows into a custom object.

Included below are the source code:

ServiceHistory_wsdl.cls:

//Generated by wsdl2apex

public class ServiceHistory_wsdl {
    public class FaultDetail_element {
        public String errorMessage;
        public String requestID;
        private String[] errorMessage_type_info = new String[]{'errorMessage','urn:soap-fault:details',null,'1','1','false'};
        private String[] requestID_type_info = new String[]{'requestID','urn:soap-fault:details',null,'1','1','false'};
        private String[] apex_schema_type_info = new String[]{'urn:soap-fault:details','false','false'};
        private String[] field_order_type_info = new String[]{'errorMessage','requestID'};
    }
    public class SrvHist_element {
        public String pEmp;
        public String pUsuario;
        public Date vFromDate;
        public Date vToDate;
        private String[] pEmp_type_info = new String[]{'pEmp','urn:HYU-SF-ServiceHistory:SF_ServiceHistory',null,'1','1','true'};
        private String[] pUsuario_type_info = new String[]{'pUsuario','urn:HYU-SF-ServiceHistory:SF_ServiceHistory',null,'1','1','true'};
        private String[] vFromDate_type_info = new String[]{'vFromDate','urn:HYU-SF-ServiceHistory:SF_ServiceHistory',null,'1','1','true'};
        private String[] vToDate_type_info = new String[]{'vToDate','urn:HYU-SF-ServiceHistory:SF_ServiceHistory',null,'1','1','true'};
        private String[] apex_schema_type_info = new String[]{'urn:HYU-SF-ServiceHistory:SF_ServiceHistory','true','false'};
        private String[] field_order_type_info = new String[]{'pEmp','pUsuario','vFromDate','vToDate'};
    }
    public class SrvHistResponse_element {
        public String result;
        public ServiceHistory_wsdl.SrvHist_ttSalesforceParam ttSalesforce;
        public String pError;
        private String[] result_type_info = new String[]{'result','urn:HYU-SF-ServiceHistory:SF_ServiceHistory',null,'1','1','true'};
        private String[] ttSalesforce_type_info = new String[]{'ttSalesforce','urn:HYU-SF-ServiceHistory:SF_ServiceHistory',null,'1','1','true'};
        private String[] pError_type_info = new String[]{'pError','urn:HYU-SF-ServiceHistory:SF_ServiceHistory',null,'1','1','true'};
        private String[] apex_schema_type_info = new String[]{'urn:HYU-SF-ServiceHistory:SF_ServiceHistory','true','false'};
        private String[] field_order_type_info = new String[]{'result','ttSalesforce','pError'};
    }
    public class SrvHist_ttSalesforceParam {
        public ServiceHistory_wsdl.SrvHist_ttSalesforceRow[] ttSalesforceRow;
        private String[] ttSalesforceRow_type_info = new String[]{'ttSalesforceRow','urn:HYU-SF-ServiceHistory:SF_ServiceHistory',null,'0','-1','false'};
        private String[] apex_schema_type_info = new String[]{'urn:HYU-SF-ServiceHistory:SF_ServiceHistory','true','false'};
        private String[] field_order_type_info = new String[]{'ttSalesforceRow'};
    }
    public class SrvHist_ttSalesforceRow {
        public String recordid;
        public String emp;
        public String taller;
        public Integer ano;
        public Integer nor;
        public Integer nor2;
        public Date fecaper;
        public String recnom;
        public String chasis;
        public String nmotor;
        public String tipc;
        public String tipcdes;
        public String divi;
        public String matric;
        public Date fecmat;
        public Integer km;
        public String custname;
        public String custtel1;
        public String custtel2;
        public String custtelex;
        public String custemail;
        public String raz;
        public String tel1;
        public String tel2;
        public String telex;
        public String email;
        public String complain;
        public Decimal totalamt;
        public Decimal partamt;
        public Decimal labouramt;
        public Decimal subletamt;
        public Decimal vatamt;
        public String rostat;
        public Decimal Reward;
        private String[] recordid_type_info = new String[]{'recordid','urn:HYU-SF-ServiceHistory:SF_ServiceHistory',null,'1','1','true'};
        private String[] emp_type_info = new String[]{'emp','urn:HYU-SF-ServiceHistory:SF_ServiceHistory',null,'1','1','true'};
        private String[] taller_type_info = new String[]{'taller','urn:HYU-SF-ServiceHistory:SF_ServiceHistory',null,'1','1','true'};
        private String[] ano_type_info = new String[]{'ano','urn:HYU-SF-ServiceHistory:SF_ServiceHistory',null,'1','1','true'};
        private String[] nor_type_info = new String[]{'nor','urn:HYU-SF-ServiceHistory:SF_ServiceHistory',null,'1','1','true'};
        private String[] nor2_type_info = new String[]{'nor2','urn:HYU-SF-ServiceHistory:SF_ServiceHistory',null,'1','1','true'};
        private String[] fecaper_type_info = new String[]{'fecaper','urn:HYU-SF-ServiceHistory:SF_ServiceHistory',null,'1','1','true'};
        private String[] recnom_type_info = new String[]{'recnom','urn:HYU-SF-ServiceHistory:SF_ServiceHistory',null,'1','1','true'};
        private String[] chasis_type_info = new String[]{'chasis','urn:HYU-SF-ServiceHistory:SF_ServiceHistory',null,'1','1','true'};
        private String[] nmotor_type_info = new String[]{'nmotor','urn:HYU-SF-ServiceHistory:SF_ServiceHistory',null,'1','1','true'};
        private String[] tipc_type_info = new String[]{'tipc','urn:HYU-SF-ServiceHistory:SF_ServiceHistory',null,'1','1','true'};
        private String[] tipcdes_type_info = new String[]{'tipcdes','urn:HYU-SF-ServiceHistory:SF_ServiceHistory',null,'1','1','true'};
        private String[] divi_type_info = new String[]{'divi','urn:HYU-SF-ServiceHistory:SF_ServiceHistory',null,'1','1','true'};
        private String[] matric_type_info = new String[]{'matric','urn:HYU-SF-ServiceHistory:SF_ServiceHistory',null,'1','1','true'};
        private String[] fecmat_type_info = new String[]{'fecmat','urn:HYU-SF-ServiceHistory:SF_ServiceHistory',null,'1','1','true'};
        private String[] km_type_info = new String[]{'km','urn:HYU-SF-ServiceHistory:SF_ServiceHistory',null,'1','1','true'};
        private String[] custname_type_info = new String[]{'custname','urn:HYU-SF-ServiceHistory:SF_ServiceHistory',null,'1','1','true'};
        private String[] custtel1_type_info = new String[]{'custtel1','urn:HYU-SF-ServiceHistory:SF_ServiceHistory',null,'1','1','true'};
        private String[] custtel2_type_info = new String[]{'custtel2','urn:HYU-SF-ServiceHistory:SF_ServiceHistory',null,'1','1','true'};
        private String[] custtelex_type_info = new String[]{'custtelex','urn:HYU-SF-ServiceHistory:SF_ServiceHistory',null,'1','1','true'};
        private String[] custemail_type_info = new String[]{'custemail','urn:HYU-SF-ServiceHistory:SF_ServiceHistory',null,'1','1','true'};
        private String[] raz_type_info = new String[]{'raz','urn:HYU-SF-ServiceHistory:SF_ServiceHistory',null,'1','1','true'};
        private String[] tel1_type_info = new String[]{'tel1','urn:HYU-SF-ServiceHistory:SF_ServiceHistory',null,'1','1','true'};
        private String[] tel2_type_info = new String[]{'tel2','urn:HYU-SF-ServiceHistory:SF_ServiceHistory',null,'1','1','true'};
        private String[] telex_type_info = new String[]{'telex','urn:HYU-SF-ServiceHistory:SF_ServiceHistory',null,'1','1','true'};
        private String[] email_type_info = new String[]{'email','urn:HYU-SF-ServiceHistory:SF_ServiceHistory',null,'1','1','true'};
        private String[] complain_type_info = new String[]{'complain','urn:HYU-SF-ServiceHistory:SF_ServiceHistory',null,'1','1','true'};
        private String[] totalamt_type_info = new String[]{'totalamt','urn:HYU-SF-ServiceHistory:SF_ServiceHistory',null,'1','1','true'};
        private String[] partamt_type_info = new String[]{'partamt','urn:HYU-SF-ServiceHistory:SF_ServiceHistory',null,'1','1','true'};
        private String[] labouramt_type_info = new String[]{'labouramt','urn:HYU-SF-ServiceHistory:SF_ServiceHistory',null,'1','1','true'};
        private String[] subletamt_type_info = new String[]{'subletamt','urn:HYU-SF-ServiceHistory:SF_ServiceHistory',null,'1','1','true'};
        private String[] vatamt_type_info = new String[]{'vatamt','urn:HYU-SF-ServiceHistory:SF_ServiceHistory',null,'1','1','true'};
        private String[] rostat_type_info = new String[]{'rostat','urn:HYU-SF-ServiceHistory:SF_ServiceHistory',null,'1','1','true'};
        private String[] Reward_type_info = new String[]{'Reward','urn:HYU-SF-ServiceHistory:SF_ServiceHistory',null,'1','1','true'};
        private String[] apex_schema_type_info = new String[]{'urn:HYU-SF-ServiceHistory:SF_ServiceHistory','true','false'};
        private String[] field_order_type_info = new String[]{'recordid','emp','taller','ano','nor','nor2','fecaper','recnom','chasis','nmotor','tipc','tipcdes','divi','matric','fecmat','km','custname','custtel1','custtel2','custtelex','custemail','raz','tel1','tel2','telex','email','complain','totalamt','partamt','labouramt','subletamt','vatamt','rostat','Reward'};
    }
    public class SF_ServiceHistoryObj {
        public String endpoint_x = 'http://xxx.yyyyzzzz.com/aaa/bbb1';
        public Map<String,String> inputHttpHeaders_x;
        public Map<String,String> outputHttpHeaders_x;
        public String clientCertName_x;
        public String clientCert_x;
        public String clientCertPasswd_x;
        public Integer timeout_x;
        private String[] ns_map_type_info = new String[]{'urn:soap-fault:details', 'ServiceHistory_wsdl', 'urn:HYU-SF-ServiceHistory', 'ServiceHistory_wsdl', 'urn:HYU-SF-ServiceHistory:SF_ServiceHistory', 'ServiceHistory_wsdl'};
        public ServiceHistory_wsdl.SrvHistResponse_element SrvHist(String pEmp,String pUsuario,Date vFromDate,Date vToDate) {
            ServiceHistory_wsdl.SrvHist_element request_x = new ServiceHistory_wsdl.SrvHist_element();
            request_x.pEmp = pEmp;
            request_x.pUsuario = pUsuario;
            request_x.vFromDate = vFromDate;
            request_x.vToDate = vToDate;
            ServiceHistory_wsdl.SrvHistResponse_element response_x;
            Map<String, ServiceHistory_wsdl.SrvHistResponse_element> response_map_x = new Map<String, ServiceHistory_wsdl.SrvHistResponse_element>();
            response_map_x.put('response_x', response_x);
            WebServiceCallout.invoke(
              this,
              request_x,
              response_map_x,
              new String[]{endpoint_x,
              '',
              'urn:HYU-SF-ServiceHistory:SF_ServiceHistory',
              'SrvHist',
              'urn:HYU-SF-ServiceHistory:SF_ServiceHistory',
              'SrvHistResponse',
              'ServiceHistory_wsdl.SrvHistResponse_element'}
            );
            response_x = response_map_x.get('response_x');
            return response_x;
        }
    }
}

ServiceHistoryAsync_wsdl (not used):

//Generated by wsdl2apex

public class ServiceHistoryAsync_wsdl {
    public class SrvHistResponse_elementFuture extends System.WebServiceCalloutFuture {
        public ServiceHistory_wsdl.SrvHistResponse_element getValue() {
            ServiceHistory_wsdl.SrvHistResponse_element response = (ServiceHistory_wsdl.SrvHistResponse_element)System.WebServiceCallout.endInvoke(this);
            return response;
        }
    }
    public class AsyncSF_ServiceHistoryObj {
        public String endpoint_x = 'http://xxx.yyyyzzzz.com/aaa/bbb1';
        public Map<String,String> inputHttpHeaders_x;
        public String clientCertName_x;
        public Integer timeout_x;
        private String[] ns_map_type_info = new String[]{'urn:soap-fault:details', 'ServiceHistory_wsdl', 'urn:HYU-SF-ServiceHistory', 'ServiceHistory_wsdl', 'urn:HYU-SF-ServiceHistory:SF_ServiceHistory', 'ServiceHistory_wsdl'};
        public ServiceHistoryAsync_wsdl.SrvHistResponse_elementFuture beginSrvHist(System.Continuation continuation,String pEmp,String pUsuario,Date vFromDate,Date vToDate) {
            ServiceHistory_wsdl.SrvHist_element request_x = new ServiceHistory_wsdl.SrvHist_element();
            request_x.pEmp = pEmp;
            request_x.pUsuario = pUsuario;
            request_x.vFromDate = vFromDate;
            request_x.vToDate = vToDate;
            return (ServiceHistoryAsync_wsdl.SrvHistResponse_elementFuture) System.WebServiceCallout.beginInvoke(
              this,
              request_x,
              ServiceHistoryAsync_wsdl.SrvHistResponse_elementFuture.class,
              continuation,
              new String[]{endpoint_x,
              '',
              'urn:HYU-SF-ServiceHistory:SF_ServiceHistory',
              'SrvHist',
              'urn:HYU-SF-ServiceHistory:SF_ServiceHistory',
              'SrvHistResponse',
              'ServiceHistory_wsdl.SrvHistResponse_element'}
            );
        }
    }
}

SvcHistCallout:

public class SvcHistCallout {

    public static ServiceHistory_wsdl.SrvHistResponse_element ServiceHistory(Date dateFrom, Date dateTo) {

        system.debug('SvcHistCallout: wrapper method.');

        ServiceHistory_wsdl.SF_ServiceHistoryObj sh = new ServiceHistory_wsdl.SF_ServiceHistoryObj();
        sh.endpoint_x = 'http://xxx.yyyyzzzz.com/aaa/bbb1';

        system.debug('SvcHistCallout: endpoint defined.');

        //  invoke SrvHist method
        ServiceHistory_wsdl.SrvHistResponse_element she;

        system.debug('SvcHistCallout: invoking method.');
        she = sh.SrvHist('001', 'sysid', dateFrom, dateTo);
        system.debug('SvcHistCallout: method returned.');

        return she;
    }
}

SvcHistWebServiceMock:

@isTest
global class SvcHistWebServiceMock implements WebServiceMock {
    global void doInvoke(
        Object stub,
        Object request,
        Map<String, Object> response,
        String endpoint,
        String soapAction,
        String requestName,
        String responseNS,
        String responseName,
        String responseType) {


            system.debug('WebServiceMock.doInvoke(): what to do here?');

            // Create response element from the autogenerated class.
            // Populate response element.
            // Add response element to the response parameter, as follows:
            ServiceHistory_wsdl.SF_ServiceHistoryObj sho = new ServiceHistory_wsdl.SF_ServiceHistoryObj();
            ServiceHistory_wsdl.SrvHistResponse_element sh = new ServiceHistory_wsdl.SrvHistResponse_element();
            sh = sho.SrvHist('001', 'sysid', Date.valueOf('2015-02-01'), Date.valueOf('2015-02-02'));

            system.debug('WebServiceMock.doInvoke(): putting response_x out:');
            system.debug(sh);
            response.put('response_x', sh);
    }
}

And finally, SvcHistWebServiceMock_Test:

@isTest
private class SvcHistWebServiceMock_Test {

    @isTest static void myUnitTest() {
        // TO DO: implement unit test
        Test.startTest();

        system.debug('myUnitTest: starting test . . .');
        // fake response
        Test.setMock(WebServiceMock.class, new SvcHistWebServiceMock());

        system.debug('myUnitTest: setMock executed');
        //  webservice callout
        ServiceHistory_wsdl.SrvHistResponse_element she;
        she = SvcHistCallout.ServiceHistory(Date.valueOf('2015-01-01'), Date.valueOf('2015-01-03'));

        //  verify that mock up test returns result
        //System.assertEquals('Mock response', she);
        system.debug('myUnitTest: Mock response ::: ');
        system.debug(she);

        Test.stopTest();
    }
}

Thank you for any feedback.

  • Try capturing the debug log during the test. It should show where the multiple callouts are occuring. – Daniel Ballinger Mar 20 '15 at 2:57
  • Edited my sources to reflect my current code with system.debug checkpoints. Below is the debug log output: – Cω Tham Mar 20 '15 at 7:54
  • I noticed repeated lines on "WebServiceMock.doInvoke(): what to do here?" though I never explicitly execute that method. No idea what causes the loop. Will try to link to the debug output file later. – Cω Tham Mar 20 '15 at 8:07
  • Debug log (2mb): click to view – Cω Tham Mar 20 '15 at 8:18
0

From the log, it appears you have an infinite loop in your code and the callout limit is the first one to break:

System.LimitException: Too many callouts: 101

Class.ServiceHistory_wsdl.SF_ServiceHistoryObj.SrvHist: line 130, column 1 Class.SvcHistWebServiceMock.doInvoke: line 22, column 1 Class.ServiceHistory_wsdl.SF_ServiceHistoryObj.SrvHist: line 130, column 1 Class.SvcHistWebServiceMock.doInvoke: line 22, column 1 Class.ServiceHistory_wsdl.SF_ServiceHistoryObj.SrvHist: line 130, column 1 Class.SvcHistWebServiceMock.doInvoke: line 22, column 1
.......................................
Class.SvcHistWebServiceMock.doInvoke: line 22, column 1 Class.ServiceHistory_wsdl.SF_ServiceHistoryObj.SrvHist: line 130, column 1 Class.SvcHistCallout.ServiceHistory: line 16, column 1 Class.SvcHistWebServiceMock_Test.myUnitTest: line 15, column 1

It would appear that your mock implementation is calling the web service that it is mocking. Which is calling the mock, and so on.

I.e. Remove this line from the mock.

sh = sho.SrvHist('001', 'sysid', Date.valueOf('2015-02-01'), Date.valueOf('2015-02-02'));

The implementation of the mock should not rely on the thing it is mocking. You should return data that you create independently.

| improve this answer | |
  • Apex Test Execution runs fine without any error after removing that line. But how would I know if the call to the API is working? I have done up a VF page to execute the web method on button click. Is this the only way to test actual web service call-out? Thanks for the reply. – Cω Tham Mar 23 '15 at 2:53
  • Test methods should not, and indeed cannot, make callouts to actual web services. The WebServiceMock's are provided so you can simulate the function of the web service in your test methods. Test methods don't call actual web services as you can't generically roll back a web service call when the test finishes. Also, testing would take a lot longer while you wait for the web service to respond. – Daniel Ballinger Mar 23 '15 at 8:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.