2. Update Asset workflow post function

Adding post function

Add "[AIP] - Update Asset workflow postfunction" to the workflows to update an asset object on transition. If the condition passes, post function is executed for each asset separately. If an asset is removed from the asset custom field, it will be executed as well. You can control it with assetStatus context parameter. If you do want to do nothing for the removed assets you can control assetStatus == 'removed' and then return null. Please see context parameters details in this document.

Return null if you do not want to change the asset attribute value. Return empty string '' if you want to clear value.

See examples, inventory management and common classes.



 Put the post function in the middle; after issue updates and before GenerateChangeHistoryFunction and Re-index post functions.

Workflow Parameters

ParamaterDescription
ConditionGroovy script for the post function condition.  Function must return true or condition must be empty to pass. If condition fails post function will be ignored.
Target Asset custom fields
Specify the custom field to update assets. Leave it blank to update all Asset custom fields.
Default groovy script
This is the default script to be executed for the options that has no specific groovy script. Type script returning the value to update asset attribute, e.g return issue.summary
Attributes to be updated (one or more)

Form: Define specific Form or select "Any form" to update the assets having the attribute.

Attribute: Target attribute to set it's value

Groovy Scripts to execute

(one for each attribute definition)

There 2 options to use as source value:

  • Use default groovy script: "Default groovy script" result will be set to the attribute value
  • Custom groovy script: Specify attribute specific groovy script to set as value.

JIRA Custom field value will be added later as 3rd value source.

Context parameters for Groovy Scripts

Please see Sample Groovy Scripts to Create/Update Asset workflow post function for more examples.

Variable NameDescription
asset

Access AIP asset class instance. Only valid for Default Groovy Script and Attribute Groovy Script. See https://confluence.snapbytes.com/x/-YZ2AQ.

assetStatus

String value gives status of edit operation of each assets for Asset Custom fields. This value may help for the increase or decrease decision for inventory tracking.

  • noChange: asset already exists before transition and it still exists
  • removed: asset is removed in this transition
  • added: asset was not exist before transition and it is added in this transition.
  • unknown: a serious error occurred and failed to determine the status.


issue

Access current issue. Instance of com.atlassian.jira.issue.Issue. see https://docs.atlassian.com/software/jira/docs/api/latest/index.html?com/atlassian/jira/issue/Issue.html

originalIssue

Access original issue before the transition. Instance of com.atlassian.jira.issue.Issue. see https://docs.atlassian.com/software/jira/docs/api/latest/index.html?com/atlassian/jira/issue/Issue.html

ComponentAccessorAccess JIRA components. See https://docs.atlassian.com/software/jira/docs/api/latest/index.html?com/atlassian/jira/component/ComponentAccessor.html
customFieldManager

Access JIRA Custom Field Manager class. See https://docs.atlassian.com/software/jira/docs/api/latest/index.html?com/atlassian/jira/issue/CustomFieldManager.html

loggedInUser

ApplicationUser instance for current logged in user. See https://docs.atlassian.com/software/jira/docs/api/latest/index.html?com/atlassian/jira/user/ApplicationUser.html

Example:

 loggedInUser == issue.getAssignee()
DefaultIssueChangeHolder

Default implementation of a change holder. It is used to update a custom field.

aipUtilsHelper class for the post function groovy script. See aipUtils for details


Sample Post Function configuration page

You can add multiple post functions or multiple attributes for one post function.

Try Groovy Scripts

You can immediately execute groovy script so see result. This will let you write and try your groovy scripts faster. Please keep in mind that scripts will be actually executed, if you modify anything please use test objects (issue, asset, etc.)!

Groovy examples for Asset Attribute Types

Attribute TypeDescriptionGroovy example
Cascading DropdownThe value must be cascading dropdown option ids. Add "-" between options. 
return "1-2"
return "1-2-3-4"
CheckboxList

Return valid option values wrapped with three @ characters. i.e: @@@ada@@@@@@ist@@@

For a single option value no need to wrap with @ characters.

return "@@@ada@@@@@@ist@@@"

DatePicker

Result must be in ISO format ("yyyy-MM-dd"), i.e: "2018-12-26".

You do not need to do formatting if you use LocalDateTime class as it returns in ISO format by default.

Jira Issue's date field example:

  • issue.created.format("yyyy-MM-dd'T'HH:mm")


import java.time.*  
  LocalDateTime t = LocalDateTime.now();  
  return (t as String) 
DateTimePicker

Result must be in ISO format ("yyyy-MM-ddTHH:mm"), i.e: "2018-12-26T20:20".

You do not need to do formatting if you use LocalDateTime class as it returns in ISO format by default. If you need to format a date to string, use the format as: "yyyy-MM-dd'T'HH:mm" (Please notice extra single quotes!)


Jira Issue's date field example:


  • issue.created.format("yyyy-MM-dd'T'HH:mm")


import java.time.*  
  LocalDateTime t = LocalDateTime.now();
  return (t as String) 
DropdownListReturn a valid option value.
return "ada"
EncryptedThe value must be text.
return "password123"
InventoryListReturn reference asset ID
return "3"
InventoryListByFormReturn reference asset ID
return "10"
IPAny text is possible, there is no format control.
return  "10.0.0.2"
IPv6Any text is possible, there is no format control.
return "2001:0db8:85a3:0000:0000:8a2e:0370:7334"
Jira Organizations

The value must be one of Jira Organization ids. If there are multiple values, you must add "@@@@@@" between them.

(Here is how to get Jira Organization Ids)

return "1"
return "1@@@@@@2"
Jira Project

The value must be one of Jira Project Ids. If there are multiple values, you must add "@@@@@@" between them.

(Here is how to get Jira Project Ids)

return "10000"
return "10000@@@@@@10001"
Jira Project Components

The value must be one of "Jira Project Id - Jira Project Component Id". If there are multiple values, you must add "@@@@@@" between them.

(Here is how to get Jira Project Components Ids)

return "10000-1"
return "10000-1@@@@@@100001-2"
Jira Project Versions

The value must be one of "Jira Project Id - Jira Project Version Id". If there are multiple values, you must add "@@@@@@" between them.

(Here is how to get Jira Project Components Ids)

return "10000-1"
return "10000-1@@@@@@100001-2"
Jira User PickerThe value must be a username. It doesn't need to be a Jira User.
return "tyler-durden"
TextAreaAny text is possible.
return issue.description
URLAny text is possible, there is no format control.
return "http://www.snapbytes.com/"
UserPickerAny text is possible, there is no control. You may use issue.reporter.username or issue.assignee.username
return issue.reporter.username



Another post function example for All Attribute types

You can update all fields of an attribute in one post function.