Sample Groovy Scripts for Asset generic groovy script post function

Please note that; 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 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>'


In the example screen has 2 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, 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 

 





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