1

I'm writing an Apex class to make an http callout to an external web service using a Username and Password stored on the User object. The debugger shows it runs successfully but never makes the call out. I have created a User with the Username and password fields populated. Can anyone tell me why it fails to make the callout? Thanks.

    global class cstschedulable implements schedulable{


    global User cstUser {get;set;}
     public Id cstUserId;
     public string cstUsername {get;set;}
     public string cstPassword {get;set;}
     //public string cstNode {get;set;}
     public Id ClientAcctId {get;set;}
     public datetime cstLastUpdate{get;set;}
     public string loginstring {get;set;}
     public map<String, string> cstUserMap{get;set;}



    global User getUserData(){

        If (cstUser == null){
        User cstUser = [select Id, CST_User_Name__c,CST_Password__c, CSTlastupdate__c from User where CST_User_Name__c != null ];
        }
        return cstUser;


       }

        public string UserData(User cstUser){
        if(cstUser.CST_User_Name__c == null)
        {
        system.debug('cstUser null at UserData');
        }
        else{
        system.debug('cstUser not null at UserData');
        }
        if(cstUser.CST_User_Name__c != null){
        String loginstring = ('"'+cstUser.CST_User_Name__c+'" "password": "'+cstUser.CST_Password__c+'"}');

        }
       return loginstring;

}           

     global void execute(SchedulableContext sc){
     }

     global void returnAuth(string loginstring){    
          string xAuthToken;
          string st_Today;
    If(loginstring != null){

        HttpRequest req = new HttpRequest();
        HttpResponse res = new HttpResponse();
        Http http = new Http();

        req.setMethod('POST' ); // Method Type

        req.setEndpoint('web210.ntree.com/twig-cstc3/v1/login');//salestree url

        req.setHeader('Content-Type','application/json');
        req.setHeader('{ "username" : ', loginstring);


            res = http.send(req);

            if(res.getBody() != null){
                if(res.getBody() == 'Rejected: check_login returned false:'){
                    system.debug('Rejected: check_login returned false:');
                }

            }else{
                system.debug('SalesTree Failed to respond');
            }



  // Parse entire JSON response.
  JSONParser parser = JSON.createParser(res.getBody());
  while (parser.nextToken() != null) {
    if (parser.getCurrentToken() == JSONToken.START_ARRAY) {
        while (parser.nextToken() != null) {
            if ((parser.getCurrentToken() == JSONToken.FIELD_NAME) &&
            (parser.getText() == 'authenticationToken')) {
                // Get the value.
                parser.nextToken();
                xAuthToken = parser.getText();

            if ((parser.getCurrentToken() == JSONToken.FIELD_NAME) &&
            (parser.getText() == 'st:today')){
                parser.nextToken();
                parser.nextToken();
                st_Today = parser.getText();

             } 
             if (xAuthToken != null){
                system.debug('Auth Token received');
             }
             else
             {
             system.debug('Auth Token not received');
             }
             if(xAuthToken != null){
                User UpdateUser = new User();
                UpdateUser.Id = Id.valueof(cstUser.Id);

                UpdateUser.X_Auth_token__c = xAuthToken;
                UpdateUser.CSTlastupdate__c = Datetime.now();
                Update UpdateUser;


             }   

            }           
           }        
          }
         }
        }   
        else{
       system.debug('loginstring null');
       }                
       }

}
4
  • i think you need to implement Database.AllowCallouts interface.
    – javanoob
    Aug 25, 2015 at 17:08
  • 3
    there is nothing in your execute method so nothing will happen. Since you cannot make a callout from a scheduled apex method so you will have to write you batch and call it from the execute method ensuring the batch has Database.AllowCallouts implementation
    – Eric
    Aug 25, 2015 at 17:12
  • @Eric Hmm, originally I had the execute method on the http callout but I wasn't able to pass any variables because you can't add references to execute, thought maybe I'd found a work around, but i guess not. Any suggestions?
    – Tim
    Aug 25, 2015 at 17:30
  • 1
    what is starting this schedule? is a schedulable class really required or can this be done with a trigger + @future?
    – cropredy
    Aug 25, 2015 at 23:47

1 Answer 1

2

System.Schedulable chokes on any kind of callouts, even with Database.AllowsCallouts

Scheduler: failed to execute scheduled job: jobId: 707j0000004w8PC,

common.apex.async.AsyncApexJobModel

Callout from scheduled Apex not supported.

A workaround is to wrap the callout itself in a Queueable implementation or a @Future method:

@Future(callout=true) static public void doStuff() {
    HttpRequest request = new HttpRequest();
    new Http().send(request);
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .