Scheduled Versioning
DEVELOPER HOMEOur Version Service API allows you to create versions of projects that contain a record of how a project has progressed. With project versions, you can compare previous and current details.
However, creating project versions isn't really something that comes to mind when you’re busy managing projects. Fortunately, we have good news: you can easily schedule a job to create project versions periodically using the Apex Scheduler and our API.
Sample Scenario: Project Versions Created Monthly
Using the Apex Scheduler you can schedule a job that runs once a month. Knowing this, you can have the job call a class with a method that calls our API and creates a project version.
Implementation
- Add a custom checkbox field to project named ‘Versioning__c’, which allows you to decide which projects get versioned each month and which don’t. We will use this field to query for Projects that we should create versions of.
- Create global class that implements the Schedulable interface and have it call our Version Service API:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | public class ScheduleVersioning implements Schedulable{ public void execute(SchedulableContext ctx){ //Call our versioning method callVersioning(); } private static void callVersioning() { //Query for Projects that need versioning List<pse__Proj__c> p = [SELECT Name, Id FROM pse__Proj__c WHERE Versioning__c = true]; //Create list of versions List<pse.VersionService.Version> versions = new List<pse.VersionService.Version>(); //Create versions for each project for(pse__Proj__c proj : p){ Id projectId = proj.Id; //Version setup pse.VersionService.Version dto = new pse.VersionService.Version(); dto.ProjectId = projectId; dto.VersionName = 'Monthly Version ' + Date.Today(); dto.Notes = 'Demo notes.'; dto.Baseline = false; versions.add(dto); } //Call API List<Id> versionIds = pse.VersionService.createAsync(versions); } } |
Usage
Now all we have to do is call the Apex Scheduler to schedule our job. Reminder: Make sure you have the correct permission controls set up for your selected projects to allow versioning.
This can be done in two ways:
Execute Anonymous Apex
1 2 3 | ScheduleVersioning sv = new ScheduleVersioning(); String cronExpr = '0 0 0 1 * ?'; System.schedule('Monthly Versioning', cronExpr, sv); |
We use a CRON expression to state when we want our job to run. Here we have it set to run on the first of every month.
Schedule from UI
- From Setup, enter Apex in the Quick Find box, then select Apex Classes.
- Click Schedule Apex.
- For the job name, enter ‘Monthly Versioning’.
- Click the lookup button next to Apex class and enter ‘ScheduleVersioning’ for the search term and click the name of your scheduled class.
- Select ‘Monthly on day 1’ for the frequency, your preferred start and date dates, and ‘00:00’ for you preferred start time.
- Click Save.
If you look at Scheduled Jobs in setup, you can see the job we scheduled and the next time it is set to run.

It is already hard enough to remember your daily schedule, and you don’t need to add on a monthly task on top of that to check on. Good thing our API allows an easy way to send notifications and reminders.
- Head over to Setup, enter ‘Custom Settings’ in the Quick Find box and select it.
- Find and click Project Versioning Settings.
- Click Manage.
- Depending on if you want to set an org-wide default or a profile specific default click the top or bottom ‘New’ button respectively. For this example, we are selecting the top button for an org-wide default.
- Enter your email address(es) in ‘Notifications_Recipients’ as a semicolon(;) separated list.
- You can choose to receive notifications through chatter, email, or in the form of tasks. For this example, we select ‘Notifications Email’.
- Click Save.
Now you’ll receive emails every month after your project versions have successfully been created.
Notifications also include job failures and version deletions.
Unit Test
Simple enough right? Now all we have to do is test your Apex code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | @isTest private class TestScheduleVersioning { @isTest static void testScheduleAndVersion() { //Create test data pse__Proj__c proj = new pse__Proj__c(); proj.pse__Start_Date__c = Date.Today(); proj.Name = 'Test Project'; proj.pse__Is_Active__c = true; proj.Versioning__c = true; insert proj; //Permission controls to allow versioning pse__Permission_Control__c permissions = new pse__Permission_Control__c(); permissions.pse__Compare_Project_Version__c = true; permissions.pse__Create_Project_Version__c = true; permissions.pse__Delete_Project_Version__c = true; permissions.pse__User__c = UserInfo.getUserId(); permissions.pse__Project__c = proj.Id; insert permissions; String cronExpr = '0 0 0 1 3 ? 2022'; Test.StartTest(); //Schedule the test job ScheduleVersioning sv = new ScheduleVersioning(); System.schedule('test', cronExpr, sv); //Verify job has not run yet List<pse__Version__c> ver = [SELECT Name, Id FROM pse__Version__c WHERE pse__Project__c = :proj.Id]; System.assertEquals(0, ver.size(), 'Version created before job has run'); //Job will run synchronously when stopping test Test.StopTest(); //Verify version was created ver = [SELECT Name, Id FROM pse__Version__c WHERE pse__Project__c = :proj.Id]; System.assertEquals(1, ver.size(), 'Version was not created'); } } |