Page tree
Skip to end of metadata
Go to start of metadata

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.

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 


 

  • No labels