Refresh Dashboard Automatically in Salesforce

Introduction to Salesforce Dashboard Auto-Refresh

Salesforce dashboards help organizations track KPIs, sales funnels, service queues, and performance trends. However, Salesforce does not automatically refresh dashboards in Lightning. Users may see outdated data unless they manually click Refresh.

This manual effort becomes inefficient when:

  • Teams monitor dashboards throughout the day
  • Call centers rely on real-time queue updates
  • Executives want instant visibility during peak hours

That’s why solutions that refresh dashboard automatically in 15 minutes have become increasingly valuable.

SOLUTION

Apex Scheduled Job

global class AutoRefreshDashboards implements Database.Batchable<sObject>, schedulable, Database.AllowsCallouts {
    
    /*
    //Scheduled the job to run every hour
    String CRON_EXP = '0 0/15 * * * ?'; //run every 15 mins        
    AutoRefreshDashboards cls = new AutoRefreshDashboards('Sales Dashboard');        
    System.schedule('Sales Dashboard - Refresh', CRON_EXP, cls);
    */
    String dashboardName;
    global AutoRefreshDashboards(String dbName) {
        this.dashboardName = dbName;
    }
    
    global void execute(SchedulableContext SC) {
        database.executebatch(new AutoRefreshDashboards(dashboardName), 1);
    }
    
    global Database.QueryLocator start(Database.BatchableContext BC) {
        return Database.getQueryLocator('SELECT Id FROM Dashboard WHERE Title=:dashboardName');
    }
    
    global void execute(Database.BatchableContext BC, List<SObject> scope) {
        // execute code
        for(SObject afd : scope){
            HttpRequest req = new HttpRequest();
            req.setHeader('Authorization', 'Bearer ' + UserInfo.getSessionId());
            req.setMethod('PUT');
            req.setEndpoint(URL.getOrgDomainUrl().toExternalForm() + '/services/data/v60.0/analytics/dashboards/'+afd.Id);
            
            Http http = new Http();
            HttpResponse res = http.send(req);                
            system.debug(res.getStatusCode());
            system.debug(res.getBody());
        }
    }
    
    global void finish(Database.BatchableContext BC) {
        // finish code
    }
    
}

SCHEDULE THE JOB FROM DEVELOPER CONSOLE > Anonymous Window

String CRON_EXP = '0 0/15 * * * ?'; //run every 15 mins        
AutoRefreshDashboards cls = new AutoRefreshDashboards('Sales Dashboard');        
System.schedule('Sales Dashboard - Refresh', CRON_EXP, cls);