Sample Groovy scripts for asset generic Groovy script post function

This page is about Assets & Inventory Plugin for Jira DC. Using Cloud? Click here.

This document provides sample Groovy scripts for the asset generic Groovy script post function. These scripts can be used to update issue details (summary, description, reporter, custom fields) based on information from the associated assets.

Use issue context parameter to update the issue. You can directly use issue or originalIssue of the transition, or fetch an issue by key or ID with IssueManager (like anotherIssue in examples) to fetch data.

return statements are optional but it makes the script more readable.

Get asset field value by custom field ID and field name

Script:

import inventoryplugin.entity.JipInventory import inventoryplugin.workflow.function.genericscript.dto.AssetCustomFieldAndValue List<String> getMultiFieldValueOfAssetCustomField(customFieldIdAsLong, attributeName) { for (AssetCustomFieldAndValue assetCustomFieldAndValue : assetCustomFieldAndValueList) { if (customFieldIdAsLong == assetCustomFieldAndValue.assetCustomField.id) { for (JipInventory inventory : assetCustomFieldAndValue.getAssetList()) { // This script assumes custom field is a single select. // if you have asset id and the Custom Field is multi-select, you can filter with asset id here by using inventory.ID return aipUtils.getMultiAttributeValueAsListByName(inventory, attributeName) } } } } String getAssetAttributeValueAsStringByName(customFieldIdAsLong, attributeName) { for (AssetCustomFieldAndValue assetCustomFieldAndValue : assetCustomFieldAndValueList) { if (customFieldIdAsLong == assetCustomFieldAndValue.assetCustomField.id) { for (JipInventory inventory : assetCustomFieldAndValue.getAssetList()) { return aipUtils.getAttributeValueAsStringByName(inventory, attributeName) } } } } String getAssetSystemFieldValueAsStringByName(customFieldIdAsLong, attributeName) { for (AssetCustomFieldAndValue assetCustomFieldAndValue : assetCustomFieldAndValueList) { if (customFieldIdAsLong == assetCustomFieldAndValue.assetCustomField.id) { for (JipInventory inventory : assetCustomFieldAndValue.getAssetList()) { return aipUtils.getAssetSystemField(inventory, attributeName) } } } } // this field returns userKey list (not userName) def multiUserFieldValue = getMultiFieldValueOfAssetCustomField(10700, 'Object Approvers'); def textFieldValue = getAssetAttributeValueAsStringByName(10208, 'Clarity ID'); def statusValue = getAssetSystemFieldValueAsStringByName(10208, 'status'); return '<div> JST Asset\'s Clarity ID: ' + textFieldValue + '</div>' + '<div> CARDS Asset\'s Object Approvers user key list: ' + multiUserFieldValue + '</div>' + '<div> CARDS Asset\'s status : ' + statusValue + '</div>'

Example:

In the example, the screen has two asset custom fields:

  • Assets, cf id=10208

    •  JST asset is selected, has following attributes  (and some other that we do not use here)

      • Clarity ID: 00006866

      • Object Approvers: Alana Grant and Tyler Durden

      • status: In Stock

  • Assets2, cf id=  10700

    • Asset CARDS is selected, has following attribute (and some other that we do not use here)

      • Object Approvers: Marla Singer and Tyler Durden

      • status: In Use

Result:

JST Asset's Clarity ID: 00006866
CARDS Asset's Object Approvers user key list: [JIRAUSER18101, JIRAUSER18100]
CARDS Asset's status : In use

Update issue's summary, description, and reporter with multiple assets' values

Script:

import inventoryplugin.entity.JipInventory import inventoryplugin.entity.JipInventoryItem import inventoryplugin.workflow.function.genericscript.dto.AssetCustomFieldAndValue import java.time.LocalDateTime String getFirstAttributeValueOfAnAsset(attributeName, appendFieldName) { for (AssetCustomFieldAndValue assetCustomFieldAndValue : assetCustomFieldAndValueList) { for (JipInventory inventory : assetCustomFieldAndValue.getAssetList()) { for (JipInventoryItem inventoryItem : inventory.getInventoryItems()) { if (inventoryItem.getFormAttribute().getAttribute().getAttributeName() == attributeName) { def result = inventoryItem.getValue() if (appendFieldName) result += ' [' + assetCustomFieldAndValue.assetCustomField.fieldName + ']' return result } } } } } String getAllAssetIdAndNames() { def result = '' for (AssetCustomFieldAndValue assetCustomFieldAndValues : assetCustomFieldAndValueList) { for (JipInventory inventory : assetCustomFieldAndValues.getAssetList()) { result += '\n' + ' #' + inventory.ID + ' - ' + inventory.name } } return result } def assetUserName = getFirstAttributeValueOfAnAsset('JIRA User', false) def assetQuantity = getFirstAttributeValueOfAnAsset('Quantity', true) def applicationUserToSet = ComponentAccessor.getUserManager().getUserByName(assetUserName) def nowAsString = LocalDateTime.now().toString() def allAssetIdAndNames = getAllAssetIdAndNames() issue.setSummary('This is an updated summary - ' + nowAsString) issue.setDescription('This is an updated description. \n'+ '*Updated at* ' + nowAsString + '\n' + '*Asset quantity:* ' + assetQuantity + '\n' + '*All asset names and ids:* ' + allAssetIdAndNames) issue.setReporter(applicationUserToSet) issue.store()

Result 

image-20240522-120433.png

Update Text custom field value with asset's attribute value (single or multi-line is applicable)

Script:

import com.atlassian.jira.issue.CustomFieldManager import com.atlassian.jira.issue.ModifiedValue import com.atlassian.jira.issue.fields.CustomField import com.atlassian.jira.issue.util.DefaultIssueChangeHolder import inventoryplugin.entity.JipInventory import inventoryplugin.entity.JipInventoryItem import inventoryplugin.workflow.function.genericscript.dto.AssetCustomFieldAndValue String getFirstAttributeValueOfAnAsset(attributeName) { for (AssetCustomFieldAndValue assetCustomFieldAndValue : assetCustomFieldAndValueList) { for (JipInventory inventory : assetCustomFieldAndValue.getAssetList()) { for (JipInventoryItem inventoryItem : inventory.getInventoryItems()) { if (inventoryItem.getFormAttribute().getAttribute().getAttributeName() == attributeName) { return inventoryItem.getValue() } } } } return 'Nothing found' } def valueToUpdate = getFirstAttributeValueOfAnAsset('ABC') CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager() CustomField textCustomField = customFieldManager.getCustomFieldObject("customfield_11100") textCustomField.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(textCustomField), valueToUpdate), new DefaultIssueChangeHolder())

Result 

Â