Skip to main content

Workflow post functions

A Scripted Post-function runs your script after a work item moves through a transition. Use it to change the work item or call another service. For example, add a comment when work moves to review.

Applies to: Script Master for Jira Β· Cloud

How a post function runs​

  1. Someone moves a work item through the transition.
  2. Jira completes the move and calls the post function.
  3. Script Master sends the script to your site's Companion App, which runs it.

The move has already happened when the script runs. If the script fails, the work item stays in its new status, and nobody sees an error on the work item. Script Master records the failure in the post function's Recent runs and in the Last 7 days column of the Workflows tab.

Companion App required

Post functions run on the server, through the Companion App. Until a Jira admin sets it up, post functions don't run. See Set up the Companion App.

Call Jira as the app​

Use api.asApp().requestJira() for Jira calls. A post function sometimes runs without a user, and the Companion App runs every script without one, so api.asUser() doesn't work here. Changes the script makes appear as made by the Companion App's app account.

For everything api, route and fetch offer, see Forge API for server-side scripts.

The payload​

The script gets the transition's details in payload.

PropertyWhat it holds
payload.issueThe work item, including its id and key
payload.transitionThe transition: id, name, and the from and to status IDs
payload.workflowThe workflow's id and name
payload.changelogThe changes the transition made
payload.atlassianIdThe account ID of the person who made the transition
payload.configuration.codeThe script itself

Recent runs​

When you edit a saved post function, Recent runs under the editor shows its latest runs, the oldest on the left. Select a run to see its console output and error. Failed runs are always kept. See Run history.

Examples​

For a full walkthrough, see Add a comment after a transition. Both examples below are also in the editor's Snippets.

Update the work item​

Set the summary of the work item that moved.

const response = await api.asApp().requestJira(route`/rest/api/3/issue/${payload.issue.id}`, {
method: 'PUT',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({
fields: { summary: 'Completed orders still displaying in pending' }
})
});

console.log(`Response: ${response.status} ${response.statusText}`);

Delete every link from the work item that moved.

const itemResponse = await api.asApp().requestJira(route`/rest/api/3/issue/${payload.issue.key}`, {
headers: { 'Accept': 'application/json' }
});
const item = await itemResponse.json();
const linkIds = item.fields.issuelinks?.map(link => link.id) ?? [];

for (const linkId of linkIds) {
await api.asApp().requestJira(route`/rest/api/3/issueLink/${linkId}`, {
method: 'DELETE'
});
}