Setting user name field of an asset with asset custom field value change - Example 4
It is possible to set a user field of an asset while it is added to the issue custom field or removed from it. Please follow the steps of the following example.
In this example, we'll define a global transition with a custom screen (Update Asset) to set or unset "JIRA User" field of an asset when it is added or removed from asset custom field. Asset won't be updated when asset won't added to the issue or removed from it.
1- Define a "JIRA User" attribute
2- Add it to the desired form
3- Create multiple Mouse assets
4- Define a screen to Update Asset -
Remove asset custom field from edit and create screens. Only this screen will update assets on transition. View Issue screen will have Asset custom field as read-only.
5- Define a new transition with Update Asset screen
In this example it is defined as All-to-self. You can define for a specific status as well. Select "Update Asset" screen for the transition.
6- Define the post function to update user name on the Update Asset transition
Select Add "[AIP] - Update Asset workflow postfunction" to the workflows to update an asset object on transition. And select "JIRA User" field. Leave form parameter as blank to set any matching form.
Post function Groovy Script
if (assetStatus == 'added') return issue.reporter.username else if (assetStatus == 'removed') return '' else return null
"return null" result won't update asset. If you want to clear attribute value return empty String "return ''"
This script will be executed for each assets of all custom fields that matches the configuration. Assets also includes removed assets to let you control inventory. If you want to do nothing for assets removed from issue you can use if (assetStatus == 'removed') return null
Order of Post Function
7- Test it on Post Function Create Screen and publish workflow
8- See it in action: Update assets with Update Asset transition
Before: Issue has no assets and Asset #2093 has no JIRA User value
After "Update Asset" transition, reporter name is set to "JIRA User" asset attribute.
Now remove asset
Jira User is cleared
Now add 2 assets
2 assets have JIRA User values set
Remove 2 assets with "Update Asset" and see JIRA Users fields are cleared