2

I made scheduled class to get access token and save it to the custom object.. I set the scheduled jobs, using the UI (apex class > schedule apex), to run the class everyday.. but its not firing..

here`s my class

    global class Purecloud_Connector implements Schedulable{
    //public static String CRON_EXP = '0 0 1/1 * *';
    global void execute(SchedulableContext ctx){
    String clientId = '[CLIENT ID]';
    String clientSecret = '[CLIENT SECRET]';
    String connId = 'a1cO0000001UZ83IAG';    
    String accessToken;

        HttpRequest req = new HttpRequest();
        req.setMethod('POST');

        req.setHeader('Authorization', 'Basic ' + EncodingUtil.base64Encode(Blob.valueOf(clientId + ':' + clientSecret)));
        req.setHeader('Host','[HOST]');
        req.setHeader('content-type', 'application/x-www-form-urlencoded');
        req.setEndpoint('[END POINT]');

        req.setBody('grant_type=client_credentials');

        Http http = new Http();

        try{
            HTTPResponse res = http.send(req);
            System.debug('res:' + res);
            System.debug('body:' + res.getBody());
            JSONParser parser;
            parser = JSON.createParser(res.getBody());
            integer x = 0;
            while (parser.nextToken() != null) {
                x++;
                system.debug('countLoop:' + x);
                if ((parser.getCurrentToken() == JSONToken.FIELD_NAME) && (parser.getText() == 'access_token')){
                        parser.nextToken();
                        accessToken = parser.getText();
                }

                Purecloud_Connector__c conn = new Purecloud_Connector__c();
                conn.id = connId;
                conn.Access_Token__c = accessToken;
                update conn;
            }  
        } catch(System.CalloutException e){
            System.debug('exception:' + e);
            // do something useful here
        }
    }
}

enter image description here

5

We used to get System.CalloutException: Callout from scheduled Apex not supported Error when we are making an HTTP callout from a class which is implementing Database.Schedulable interface because Salesforce does not allow us to make callouts from Schedulable classes.If you want to do it in the scheduled class you will need to do it in an @future method. Readout : https://developer.salesforce.com/blogs/developer-relations/2010/02/spring-10-saw-the-general-availability-of-one-of-my-favorite-new-features-of-the-platform-the-apex-schedulerwith-the-apex-s.html

  • Oh I see, so technically I have to create 1 class for callout, and 1 class for the scheduler that call the callout class? – Arief Gunawan Tjiptorahardja Oct 13 '18 at 6:50
  • Yes, create a class which have future method with callout annotation. Please read out the suggested link. – Pragati Jain Oct 13 '18 at 6:53

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.