+ Start a Discussion
Salesforce Developer 60Salesforce Developer 60 

how to scheduler this webservicecall class daily 10 AM

Hello Team,
                  I created following class now i want to schedule this class to run daily at 10 AM . I already created a scheduler class and it schedule correctly but when it run it will give error like arguments not be null .

Here is my webservice class.
 
public with sharing class WebserviceCall {

    public WebserviceCall(ApexPages.StandardController controller) {

    }

    public String message {get; set;}
    
    public PageReference getaccesstoken() { 
      string jsonstring ='userName=test@gmail.com&Password=123&grant_type=password';
        HttpRequest req = new HttpRequest();
        HttpResponse res = new HttpResponse();
        Http http = new Http();
       // req.setClientCertificateName('Client Certificate');
       
        req.setEndpoint('http://google.com');
        req.setMethod('POST');
       // req.setHeader('SOAPAction', 'Soap Action');
        //req.setHeader('Authorization','Auth Token');
      
       req.setBody(jsonstring);
 
           try {
            res = http.send(req);
            message = res.getBody();
    TokenInformation token = new TokenInformation();
    token =(TokenInformation)System.JSON.deserialize(message, TokenInformation.class);
  // JSONParser parser = JSON.createParser(res.getBody());
            System.debug(token.access_token);
                 //HttpServletResponse httpResponse = (HttpServletResponse)response;
              string s = token.access_token;
            string a1 = s.substring(0,255);
            string a2 = s.substring(255,510);
            string a3 =s.substring (510,s.length());
            //Here SessionData__c is custom setting used for store access_token
       SessionData__c objToken = new SessionData__c();
       objToken.Name ='token2';
       objToken.Id ='a0G28000000ENAI';
    objToken.Access_Token1__c =a1;objToken.Access_Token2__c =a2;
               objToken.Access_Token3__c =a3;

upsert objToken;
               
        } catch(System.CalloutException e) {
            System.debug('Callout error: '+ e);
             message = res.toString() + res.getBody();
            System.debug(res.toString());
        }
        return null;
        
}       
    
    
    
    
}

here is Scheduler class
 
global class scheduledWebServiceCall implements Schedulable {
    
   global void execute(SchedulableContext sc) {
   Account acc = new Account();
   Apexpages.Standardcontroller controller_inv = new Apexpages.Standardcontroller(acc);
      WebserviceCall b = new WebserviceCall(controller_inv); 
      b.getaccesstoken();
      
       system.debug( b.getaccesstoken());
   }
}

 
Arunkumar RArunkumar R
Hi,

Might be you response returned as null. I just added few condition in your code. Can you try your schedule with the below code,
 
public with sharing class WebserviceCall {

    public WebserviceCall(ApexPages.StandardController controller) {

    }

    public String message {get; set;}
    
    public PageReference getaccesstoken() { 
      string jsonstring ='userName=test@gmail.com&Password=123&grant_type=password';
        HttpRequest req = new HttpRequest();
        HttpResponse res = new HttpResponse();
        Http http = new Http();
       // req.setClientCertificateName('Client Certificate');
       
        req.setEndpoint('http://google.com');
        req.setMethod('POST');
       // req.setHeader('SOAPAction', 'Soap Action');
        //req.setHeader('Authorization','Auth Token');
      
       req.setBody(jsonstring);
 
           try {
            res = http.send(req);
            message = res.getBody();
    TokenInformation token = new TokenInformation();
    token =(TokenInformation)System.JSON.deserialize(message, TokenInformation.class);
  // JSONParser parser = JSON.createParser(res.getBody());
  if(token != null)
  {
            System.debug(token.access_token);
                 //HttpServletResponse httpResponse = (HttpServletResponse)response;
              string s = token.access_token;
            string a1 = s.length() >=255 ? s.substring(0,255) : null;
            string a2 = s.length() >= 510 ? s.substring(255,510) : null;
            string a3 = s.length() >=510 ? s.substring (510,s.length()) : null;
            //Here SessionData__c is custom setting used for store access_token
       SessionData__c objToken = new SessionData__c();
       objToken.Name ='token2';
       objToken.Id ='a0G28000000ENAI';
    objToken.Access_Token1__c =a1;objToken.Access_Token2__c =a2;
               objToken.Access_Token3__c =a3;

upsert objToken;
   }
          }     
        } catch(System.CalloutException e) {
            System.debug('Callout error: '+ e);
             message = res.toString() + res.getBody();
            System.debug(res.toString());
        }
        return null;
        
}       
    
    
    
    
}

 
Salesforce Developer 60Salesforce Developer 60
@Arun , i tried your solution but it's not works. Verify my scheduler class is ok? I want to run this getaccesstoken method to daily on 10 am so it will update my token for call.
Arunkumar RArunkumar R
Hi,

Is your webservice call working in vf? or totally your class not working?
Salesforce Developer 60Salesforce Developer 60
yes my class is working properly if i run this in developer console but now i want to run this method automatically daily so it will refresh accesstoken daily.
Arunkumar RArunkumar R
I ran your code in my org and it's not working for me. I have given my gmail cred.

could you please elobarte what you are trying to do ?
Salesforce Developer 60Salesforce Developer 60
@Arun , I know this is not working for you because there are register user required for call this api. This api call accesstoken from .net  and i store this in custom setting session data and after that i am calling other api method using this accesstoken . This token will expire in 24 hour so i want to run this accesstoken method daily 10 am so i will update the latest token.