Increase asset inventory conditionally
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: |
---|
Functionality
The post function performs the following operations:
Checks if it is already increased by checking whether the
Stock is reflected
custom field value isYes
orNo
.ÂChecks if
In Stock
isYes
.IncreasesÂ
Number of items in stock
withOrder 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
Number of items in stock - Number - This will hold inventory of assets.
Add the post function
Add [AIP] - Update asset workflow to a specific transition.Â
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
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 script
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();
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
to32
.Stock is reflected is set to
Yes
. If the same transition passes, the post function will not update the asset.