Executing a Job Template from Apex

Updated 1 month ago by Copado Solutions

In the article How to Execute a Job Template, we showed you the different options to run a job template. In this article, you will learn how to execute a job template from Apex.

To execute a template from Apex, you can use the below code snippet:

copado.CreateExecution.Request request = new copado.CreateExecution.Request();
request.parentId = 'a0P4X00000j6I5ETAU'; 
request.templateName = 'templateApiName'; 
request.transactionId = 'optional';
request.pipelineId = 'optional';
request.sourceId = 'optional'; 
request.destinationId = 'optional';
request.callback = 'optional'; 
request.dataJson = 'optional';
request.preSteps = new List<copado__JobStep__c>();
request.postSteps = new List<copado__JobStep__c>(); 
request.runAfterInstantiation = true;  copado.CreateExecution.execute(new List<copado.CreateExecution.Request>{ request });

Please note that this is a sample script, and you need to edit it to include your record Ids and optional parameter details. Additionally, if you don’t want to include any optional parameters, you must remove the string entirely. Leaving it blank will cause the job execution to fail.

Let’s describe some of the strings included in the above snippet in more detail:

String

Description

request.parentId = 'a0P4X00000j6I5ETAU';

This string contains the context Id in which the job template will be executed. It can be the id of a Promotion, Function, Deployment or User Story record.

request.templateName = 'templateApiName';

This string contains the API name of the job template, e.g., Heroku_Promote_1.

request.transactionId = '';

This parameter is optional. You can use it to pass a common transactionId if you want to preserve volume (file storage) across multiple job executions.

request.pipelineId = '';

An optional parameter to pass the 15/18-digit Pipeline record Id.

request.sourceId = ''; 

An optional parameter to pass the 15/18-digit source Environment record Id

request.destinationId = ''

An optional parameter to pass the 15/18-digit destination Environment record Id

request.callback = ''; 

An optional parameter to run an Apex class that implements the copado.JobExecutionCallback interface. This class will be executed when the job completes (both if it succeeds or fails).

request.dataJson = '';

Serialized Json. E.g., if you are executing a promotion job template, you need to include the Id of the user stories that are part of the promotion:

{

"userStoryIds" : [ "a171n00000EyJvyPUF" ],

"userStoryBranches" : [ "feature/US-0353750" ],

"sourceCredentialId" : "a0T1n00000BYUXnTAP",

"promotionId" : "a0Z1n00000EWYdREAX",

"promotionBranchName" : "promotion/P21749",

"isBackPromotion" : false,

"executePromotion" : false,

"executeDeployment" : false,

"destinationCredentialId" : "a0T1n00000BXRZsCAO",

"destinationBranchName" : "int",

"deploymentDryRun" : false

}

request.preSteps = new List<copado__JobStep__c>();

An optional list of additional steps to be executed before the job execution steps. This parameter and the one below are only used if you are executing a deployment job template.

request.postSteps = new List<copado__JobStep__c>();

An optional list of additional steps to be executed after job execution steps. You need to provide the payload.

request.runAfterInstantiation = true;

This parameter is set to false by default. Copado creates a Job Execution record with the status Not Started. If you set it to true, Copado starts the execution immediately.

If the job template execution has failed and you want to restart it, you can use the below snippet:

copado.RunJob.Request request = new copado.RunJob.Request();
request.jobId = 'a225I00000X6A1tPAF'; 
request.transactionId = 'optional';
request.allSteps = true; 
copado.RunJob.execute(new List<copado.RunJob.Request>{ request });

String

Description

request.jobId = 'a225I00000X6A1tPAF';

This string contains the Id of the Job Execution record.

request.allSteps = true;

This parameter is set to true by default. If you want to restart only the steps that failed in the previous execution, you can set it to false.


How did we do?