for developers
Our 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.
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);
}
}
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:
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.
If you look at Scheduled Jobs in setup, you can see the job we scheduled and the next time it is set to run.
With this implementation your projects are continually versioned even after being completed or set to inactive. You have to uncheck the ‘Versioning’ checkbox or set some sort of workflow validation rule that unchecks ‘Versioning’ when a project reaches completion or is set to inactive.
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.
Now you’ll receive emails every month after your project versions have successfully been created.
Notifications also include job failures and version deletions.
Simple enough right? Now all we have to do is test your Apex code:
@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');
}
}