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
Sumant Kuchipu 1Sumant Kuchipu 1 

Schedulers for Box Token refresh

Hi,

We are developing customized attachments to upload documents to BOX. for this I needed clent_id,client_secret,access_token, and refresh_token and I was able to get them manually and stored them in custom settings object but I think Box has limited time setup for access and refresh tokens so for that I need to create a scheduler class which internally calls box api to get refresh tokens and store them into custom_settings. 
Here is the approach I called a scheduler runs for 10 mins but as soon as run first time the job scheduled for 1 hour, so after I googled I came to know that SF uses per hour so I crreated 6 jobs for running but still no use, please see the below code.

Note: to crate first job I called the following line manually from Ananymous window
System.schedule('BoxRefreshTokensJob 00', '0 0 * * * ?', new BoxAuthRefreshTokensScheduler());
Apex Scheduler class
global class BoxAuthRefreshTokensScheduler implements Schedulable{
    global void execute(SchedulableContext SC) {
        updateBoxCustomSetting();
        System.schedule('BoxRefreshTokensJob 00', '0 0 * * * ?', new BoxAuthRefreshTokensScheduler());
        System.schedule('BoxRefreshTokensJob 10', '0 10 * * * ?', new BoxAuthRefreshTokensScheduler());
        System.schedule('BoxRefreshTokensJob 20', '0 20 * * * ?', new BoxAuthRefreshTokensScheduler());
        System.schedule('BoxRefreshTokensJob 30', '0 30 * * * ?', new BoxAuthRefreshTokensScheduler());
        System.schedule('BoxRefreshTokensJob 40', '0 40 * * * ?', new BoxAuthRefreshTokensScheduler());
        System.schedule('BoxRefreshTokensJob 50', '0 50 * * * ?', new BoxAuthRefreshTokensScheduler());
        
    }
    
    global void updateBoxCustomSetting(){
        try{
            List<Cust_Sett__c> bx=[SELECT id,Access_Token__c, Refresh_Token__c FROM Cust_Sett__c 
                                                    where name='MyCustSett' limit 1] ;
            BoxApiConnection api;
            if (api==null){
            	api = new BoxApiConnection(bx[0].client_id__c, bx[0].Client_secret__c, bx[0].Access_Token__c, bx[0].Refresh_Token__c);    
            }
            api.refresh();
            bx[0].Access_Token__c=api.getAccessToken();
            bx[0].Refresh_Token__c=api.getRefreshToken();
            update bx[0];
            
        }catch(exception e){
            system.debug('*********Exception while updating custom setting:'+e.getmessage());
        }
    }
}