Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Panel
panelIconIdatlassian-info
panelIcon:info:
panelIconText:info:
bgColor#F4F5F7

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

This document explains how to create a Jira post function that conditionally increases the inventory of an asset when a specific transition occurs.

On this page:

Table of Contents
minLevel1
maxLevel6
outlinefalse
styledefault
typelist
printabletrue

Functionality

The post function performs the following operations:

  • Checks if it is already increased by checking whether the Stock is reflected custom field value is Yes or No

  • Checks if In Stock is Yes.

  • Increases  Number of items in stock with Order Amount field value.

Configuration

Fields of screen and asset

  • Assets - Asset custom field.

    • maps to assetCustomFieldId in Groovy script.

Other fields

  • Order Amount - Number Field - number of items to be purchased.

    • maps to orderAmountCustomFieldId in Groovy script.

  • In Stock - Radio (Yes / No) - This must be Yes for this operation as we will create new asset.

    • maps to inStockCustomFieldId in Groovy script.

  • Stock is reflected - Radio (Yes / No) - This must be No for this operation as we will create new asset. This flag prevents to reflect inventory multiple times by mistake.

    • maps to stockIsReflectedCustomFieldId in Groovy script.

Asset

Attribute

attribute

  • Number of items in stock - Number - This will hold inventory of assets.

Add the post function

  1. Add [AIP] - Update asset workflow to a specific transition. 

  2. Put the post function after Issue Updates and before Update change history for an issue and store the issue in the database, GenerateChangeHistoryFunction, and Re-index post functions.

Post function parameters

Expand
titleCondition script:
Code Block
languagegroovy
import com.atlassian.jira.issue.customfields.option.Option;

/*** variables to configure **************************/
def inStockCustomFieldId = 'customfield_10317'; // In stock Custom field: set the id according to your environment
def inStockYesValue = 'Yes'
def stockIsReflectedCustomFieldId = 'customfield_10319'; // In stock Custom field: set the id according to your environment
def stockIsReflectedYesValue = 'Yes'
/*****************************************************/

def isRadioValueYes(originalIssue, cfId, optionValue) {
    def customFieldManager = ComponentAccessor.getCustomFieldManager()
    def cField = customFieldManager.getCustomFieldObject(cfId)
    def option = (Option) originalIssue.getCustomFieldValue(cField);
    return option != null && option.getValue() != null && option.getValue().equals(optionValue)
}

if (isRadioValueYes(issue, stockIsReflectedCustomFieldId, stockIsReflectedYesValue)) return false;
return isRadioValueYes(issue, inStockCustomFieldId, inStockYesValue)

Variables to set

Inspect the Issue Edit screen, locate the IDs of the fields, and then update the script.

def inStockCustomFieldId = 'customfield_10317'; // In stock Custom field: set the id according to your environment def inStockYesValue = 'Yes' def stockIsReflectedCustomFieldId = 'customfield_10319'; // In stock Custom field: set the id according to your environment def stockIsReflectedYesValue = 'Yes'

Target Asset custom fields: Select asset custom field.

Attributes to be updated: Any Form - Number Of Items in stock - custom groovy Groovy script

Code Block
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.customfields.manager.OptionsManager
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import org.apache.commons.lang3.StringUtils

def stockIsReflectedCustomFieldId = 'customfield_10319'; // In stock Custom field: set the id according to your environment
def stockIsReflectedYesValue = 'Yes'

int getOrderAmount() {
    def orderAmountCustomFieldId = 'customfield_10316'; // set the id according to your environment
    def customFieldManager = ComponentAccessor.getCustomFieldManager()
    def cField = customFieldManager.getCustomFieldObject(orderAmountCustomFieldId)
    def orderAmount = issue.getCustomFieldValue(cField);
    return (orderAmount == null) ? 0 : orderAmount as Integer;
}

// returns "current stock amount" plus "order amount"
String getNewQuantityValueOfAsset() {
    def attributeName = 'Number of items in stock'; // set the actual attribute name that hold inventory
    def stringValue = StringUtils.trim(aipUtils.getAttributeValueAsStringByName(asset, attributeName));
    if (stringValue != null && stringValue.isInteger()) {
        int currentValue = stringValue as Integer;
        return (currentValue + getOrderAmount()) as String;
    } else {
        return getOrderAmount();
    }
}

def updateStockIsReflected(issue, stockIsReflectedCustomFieldId, stockIsReflectedYesValue) {
    def customFieldManager = ComponentAccessor.getCustomFieldManager()
    def optionsManager = ComponentAccessor.getComponent(OptionsManager)

    def customField = customFieldManager.getCustomFieldObject(stockIsReflectedCustomFieldId)
    def fieldConfig = customField.getRelevantConfig(issue)
    def option = optionsManager.getOptions(fieldConfig).getOptionForValue(stockIsReflectedYesValue, null);

    // execute update
    customField.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(customField), option), new DefaultIssueChangeHolder())
}

updateStockIsReflected(issue, stockIsReflectedCustomFieldId, stockIsReflectedYesValue);
return getNewQuantityValueOfAsset();



Variables to Set

Inspect the Issue Edit screen, locate the IDs of the fields, and then update the script.

def stockIsReflectedCustomFieldId = 'customfield_10319'; // In stock Custom field: set the id according to your environment
def stockIsReflectedYesValue = 'Yes'

def orderAmountCustomFieldId = 'customfield_10316'; // set the id according to your environment

Test

  •  Number of items in stock is increased from 20 to 32.

  • Stock is reflected is set to Yes. If the same transition passes, the post function will not update the asset.