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
SujjiSujji 

Http.Send not working

Hi,

I am trying to make a callout from a Queuable Class that allows callouts.

public without sharing class ClassA implements Queueable, Database.AllowsCallouts {
 
  public void execute(QueueableContext context) {
    sendRef(varA, varB);
  }

  public static void sendRef(String varA, String varB) {
    
      HTTP call1 = new HTTP();
      HTTPRequest request1 = new HTTPRequest();
      .....
     response = call1.send(request1)
    ...
}

This class is called from a) Batch process and also from b) Custom Button.

When called from batch:
 System.enqueueJob(
                new ClassA(Parm1, Parm2)
            );

When called from button:
ClassA.sendRef(Parm1, Parm2);


Now the issue is that the statement http.send(request) is not working recently from Batch. Can anyone please help.

The same piece of code is working when submitted as a queuable .
Raj VakatiRaj Vakati
You can do it from the Queueable by extending Database.AllowsCallouts as shown below ... can u give me compplete code


 
public class QueueableExampleOne implements Queueable, Database.AllowsCallouts {
    public Integer max;
    private Integer counter = 1;
    public void execute(QueueableContext context){
        System.debug('Counter: '+counter);
        HttpRequest req = new HttpRequest();
        req.setEndpoint('http://requestb.in/10x02w41');
        req.setMethod('GET');
        Http http = new Http();
        HttpResponse resp = http.send(req);
        String html = resp.getBody();
        System.debug('Request: ' + req.toString());
        System.debug('Body: ' + html);
        QueueableExampleOne job = new QueueableExampleOne();
        job.max = max;
        job.counter = counter+1;
        if(max >= job.counter) {
        	System.enqueueJob(job);    
        }
    }
}

 
Raj VakatiRaj Vakati
Can u give me code 
SujjiSujji
@Raj,

Thanks for reply. Heres the complete code.

The class where the call out is happening:

public without sharing SendQueueable implements Queueable, Database.AllowsCallouts {

  public SendQueueable(String varA, String varB) {
    this.varA = varA;
    this.varB = varB;
  }

  public void execute(QueueableContext context) {
    requestCallout(varA, varB);
  }

  public static void requestCallout(String varA, String varB) {
   Logger logger = new Logger();
    try {
      HTTP call1 = new HTTP();
      HTTPRequest request1 = new HTTPRequest();
      HTTPResponse response1 = new HTTPResponse();
      String endPointURL = End_Point_URL__c;
      String reqBodyForAuth =
        'grant_type=password&client_id=' + EncodingUtil.urlEncode(Consumer_Key__c, 'UTF-8') +
        '&client_secret=' + EncodingUtil.urlEncode(Consumer_Secret__c, 'UTF-8') +
        '&username=' + EncodingUtil.urlEncode(User_Name__c, 'UTF-8') +
        '&password=' + EncodingUtil.urlEncode(Password__c, 'UTF-8');
      String securityToken = Security_Token__c;

      if (securityToken != null && securityToken != '') {
        reqBodyForAuth = reqBodyForAuth + securityToken;
      }
      System.debug('reqBodyForAuth:'+ reqBodyForAuth);
      request1.setBody(reqBodyForAuth);
      request1.setMethod('POST');
      request1.setTimeout(30000);
      request1.setEndpoint(endPointURL + '/services/oauth2/token');
      String body;

      System.debug('@@@Before call for access token:  '+request1.getBody());
      body = call1.send(request1).getBody();


      }
    } catch (Exception e) {
      logger.error(e);
    } finally {
      logger.flush('Log - ' + Date.today());
    }
  }
}


The Trigger code where this class is called:
    public static void callQueueable(varA varB) {

        if (callFromCustomButton) {           
            System.enqueueJob(new ClassA(varA, varB));
        }
       else if (callFromBatchClass) {
           SendQueueable.requestCallout(varA, varB);
         }
    }
Sujeet PatelSujeet Patel
I hope my suggestion will helpfull you.
defined below variable in class before constructor

String varA,varB;

and can You tell me where you defined in class varible  'End_Point_URL__c'.
If this is sObject then how you getting these value in class