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β
- Someone moves a work item through the transition.
- Jira completes the move and calls the post function.
- 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.
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.
| Property | What it holds |
|---|---|
payload.issue | The work item, including its id and key |
payload.transition | The transition: id, name, and the from and to status IDs |
payload.workflow | The workflow's id and name |
payload.changelog | The changes the transition made |
payload.atlassianId | The account ID of the person who made the transition |
payload.configuration.code | The 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}`);
Remove all work item linksβ
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'
});
}
Relatedβ
- Add a post function: put a post function on a transition.
- Test a post function: run the script against a work item first.
- Workflow validators: stop a transition before it happens.