In this example, we will define a post function to increase inventory of an asset.
Post function performs following operations:
- Checks if it is already increased by checking "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
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 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
Condition:
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 issue edit screen and located 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();
Variables to Set
Inspect issue edit screen and located 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.
Also Stock is reflected to Yes. If the same transition passes, post function will not update asset.