This is very common ask from business admins or end users to get list of schedule reports or schedule jobs (batch jobs, scheduled jobs etc) from Salesforce.
There is no standard view provided by Salesforce to view list of scheduled reports or reports. Users have to navigate to Monitor section under SetUp.
Hope this will help!!
Looking forward for everyone's comments and feedback.
There is no standard view provided by Salesforce to view list of scheduled reports or reports. Users have to navigate to Monitor section under SetUp.
This section display list of jobs either schedule or completed jobs but does not provide complete list of jobs.
By using apex, you get the list of schedule reports or jobs in csv format via email.
Please find below the apex code for that:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class SK_ScheduleJobUtility{ | |
public static void findScheduledJobDetails(string jobType){ | |
string jobTypeNumber=jobTypeMap.get(jobType); | |
if(jobTypeNumber!=null && jobTypeNumber!=''){ | |
List<CronTrigger> cjList=new List<CronTrigger>(); | |
Set<string> userIdSet = new Set<string>(); | |
for(CronTrigger cj :[select id, CronJobDetailId,CronJobDetail.Name,CronJobDetail.JobType, ownerId from CronTrigger where CronJobDetail.JobType=:jobTypeNumber]){ | |
cjList.add(cj); | |
userIdSet.add(cj.ownerId); | |
} | |
//fetch user information as we cannot access user details using child to parent query using dot notation | |
Map<string,User> userMap= new Map<string,User>(); | |
for(User userRecord:[select id,username,email,IsActive from User where Id IN:userIdSet]){ | |
userMap.put(userRecord.Id,userRecord); | |
} | |
string header = 'CronJobDetailId , CronJobDetail Name, CronJobDetail JobType,Submittedby Username,SubmittedBy Userid, User IsActive, User Email \n'; | |
string finalstr = header ; | |
for(CronTrigger jb: cjList){ | |
string recordString=''; | |
if(userMap.get(jb.ownerId)!=null){ | |
recordString = jb.CronJobDetailId+','+jb.CronJobDetail.Name+','+jobType + ','+userMap.get(jb.ownerId).username + ','+jb.ownerId + ','+userMap.get(jb.ownerId).IsActive + ',' +userMap.get(jb.ownerId).email+'\n'; | |
}else{ | |
recordString = jb.CronJobDetailId+','+jb.CronJobDetail.Name+','+jobType + ', ,'+jb.ownerId + ', ,\n'; | |
} | |
finalstr = finalstr +recordString; | |
} | |
Messaging.EmailFileAttachment csvAttc = new Messaging.EmailFileAttachment(); | |
blob csvBlob = Blob.valueOf(finalstr); | |
string csvname= jobType+ ':Schedule Job List'+system.now()+'.csv'; | |
csvAttc.setFileName(csvname); | |
csvAttc.setBody(csvBlob); | |
Messaging.SingleEmailMessage email =new Messaging.SingleEmailMessage(); | |
String[] toAddresses = new list<string> {UserInfo.getUserEmail()}; | |
String subject =jobType+': Scheduled jobs report'; | |
email.setSubject(subject); | |
email.setToAddresses( toAddresses ); | |
email.setPlainTextBody(subject); | |
email.setFileAttachments(new Messaging.EmailFileAttachment[]{csvAttc}); | |
Messaging.SendEmailResult [] r = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email}); | |
} | |
} | |
public static Map<string,string> jobTypeMap = new Map<string, string>{ | |
'Data Export' => '0', | |
'Weekly Export' => '1', | |
'Test' => '2', | |
'Dashboard Refresh' => '3', | |
'Reporting Snapshot' => '4', | |
'System' => '5', | |
'Scheduled Apex'=>'7', | |
'Report Run' => '8', | |
'Batch Job' => '9' | |
}; | |
//run below script to get job list in email | |
//SK_ScheduleJobUtility.findScheduledJobDetails('Report Run'); | |
//SK_ScheduleJobUtility.findScheduledJobDetails('Batch Job'); | |
//SK_ScheduleJobUtility.findScheduledJobDetails('Scheduled Apex'); | |
} |
Now if you have to get list of scheduled reports in your org, then just execute below script in developer console:
- For getting list of scheduled reports: SK_ScheduleJobUtility.findScheduledJobDetails('Report Run');
- For getting list of scheduled batch jobs: SK_ScheduleJobUtility.findScheduledJobDetails('Batch Job');
- For getting list of scheduled apex jobs: SK_ScheduleJobUtility.findScheduledJobDetails('Scheduled Apex');
Important Use case
Sometime reports are scheduled by user which becomes inactive, then all system admin start getting email saying:
"The user account that runs the report (the running user) is inactive."
This utility will send email along with user details which will help in identifying all inactive user for whom reports are scheduled as running user.
Hope this will help!!
Looking forward for everyone's comments and feedback.