function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
ItsJustCode2ItsJustCode2 

As of this morning all web service call outs are not testing out

As of this morning anything that inserts a lead which would also invoke a webservice call out separately in code is erroring out with the following error.

" Methods defined as TestMethod do not support Web service callouts null; Methods defined as TestMethod do not support Web service callouts null"

Here is a sample of one of the web service call outs... Please help....  Kudos given...
trigger ImmediateResponseUniversal2 on Lead (after insert) {

ID[] ids = new List<ID>();

    String url = 'https://cmncom.insidesales.com/do=noauth/immediate_response';

    String body;
     
    for (Lead l : Trigger.new) 
    {
         if(l.School__c != 'CMNTest' && l.LeadSource != 'Inbound Call' && l.LeadSource != 'School HST' && 
            l.LeadSource != 'Inbound Call' && l.LeadSource != 'School HST' &&
            l.Status != 'Application' && l.Status != 'Enrolled' && l.Status != 'Started' && l.Status != 'Closed')
         {   
           ids.add(l.id);
         }  

    }                        
      

      if (!ids.isEmpty()) 
      {
        if(trigger.isInsert)
        {
              body = 'method=call_now&dialer_initiative_id=67&ids='+EncodingUtil.urlEncode(JSON.serialize(ids), 'ISO-8859-1');              
              HTTPRequestSender.sendHTTPRequest(url, body);
        }

      }
}




 
KaranrajKaranraj
Steve - Hope you are getting this error message while running the test class. For the code coverage you have to make mock http callouts. Check this blog post for more details - https://developer.salesforce.com/blogs/developer-relations/2013/03/testing-apex-callouts-using-httpcalloutmock.html
 
ItsJustCode2ItsJustCode2
I'm a bit new at doing apex web service call outs can you couch me just a little bit more specifically?  Here is what I have to test out.....
 
trigger ImmediateResponseFIU on Lead (after insert) {

ID[] ids = new List<ID>();

    String url = 'https://cmncom.insidesales.com/do=noauth/immediate_response';

    String body;
     
    for (Lead l : Trigger.new) 
    {
         if(l.School__c == 'FIU' && l.LeadSource != 'Inbound Call' && l.LeadSource != 'School HST' && 
            l.LeadSource != 'School Marketing' && l.Status != 'Application' && 
            l.Status != 'Enrolled' && l.Status != 'Started' && l.Status != 'Closed')
         {   
           ids.add(l.id);
         }  

         }                        
      

      if (!ids.isEmpty()) 
      {
        if(trigger.isInsert)
        {
              body = 'method=call_now&dialer_initiative_id=69&ids='+EncodingUtil.urlEncode(JSON.serialize(ids), 'ISO-8859-1');              
              HTTPRequestSender.sendHTTPRequest(url, body);
        }

      }
}


and class
 
global class HTTPRequestSender{

    @future (callout=true)

    Public static void sendHTTPRequest(String url, String body){

        Http http = new Http();

        HttpResponse res = new HttpResponse();

        HttpRequest req = new HttpRequest();

        req.setEndpoint(url);

        req.setBody(body);

        req.setMethod('POST');

        try {

            res = http.send(req);
        } catch(System.CalloutException e) {

            System.debug('CalloutException: '+ e);

            System.debug(res.toString());

        }

     }

}

Please any help you can assist with would be greatly appriciated.

Thanks,
SL
 
RohRoh
Hello ItsJustCode2,
Salesforce does not encourage doing a callout from the test methods.
Instead they have a concept called as the HttpMockCallout class, using which you can write your test methods.
Please go through the below wiki ,
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_restful_http_testing_httpcalloutmock.htm

PLEASE VOTE FOR THIS ANSWER, IF YOU LIKE IT.

Thanks,
Rohit Alladi