Managing Inventories on asset custom field value change - Fixed amount - Example 2

It is possible to track quantity of an asset while it is added to the issue custom field or removed from it. Please follow the steps of the following example.

In this example, we'll define a global transition with a custom screen (Update Asset) to increase or decrease quantity value of an asset when it is added or removed from asset custom field. Asset won't be updated when asset won't added to the issue or removed from it.

1- Define a Quantity attribute

2- Add it to the desired form

3- Create multiple assets and set Quantity values (i.e 50, 40, 30)

4- Define a screen to Update Asset -

Remove asset custom field from edit and create screens. Only this screen will update assets on transition. View Issue screen will have Asset custom field as read-only.

5- Define a new transition with Update Asset screen

In this example it is defined as All-to-self. You can define for a specific status as well. 


6- Define the post function to update quantity for the Update Asset transition

Select Add "[AIP] - Update Asset workflow postfunction" to the workflows to update an asset object on transition. And select Quantity field. Leave form parameter as blank to set any matching form.

Post function Groovy Script

import inventoryplugin.entity.JipInventoryItem;
import org.apache.commons.lang3.StringUtils;

// as field is a Text type we need to convert value to int and return as String
String getNewQuantityValueOfAsset() {
    def stringValue = StringUtils.trim(aipUtils.getAttributeValueAsStringByName(asset, 'Quantity'));
  
    if (stringValue != null && stringValue.isInteger()) {
        int intValue = stringValue as Integer
        if (assetStatus == 'added') intValue--
        else if (assetStatus == 'removed') intValue++
        return intValue as String
    } else {
        return stringValue
    }
}
// If you want to do nothing for assets removed from issue uncomment code.
// if (assetStatus == 'removed') return null; 
return getNewQuantityValueOfAsset();

aipUtils.getAttributeValueAsStringByName returns the Quantity attribute value of the asset.

"return null" result won't update asset. If you want to clear attribute value return empty String "return ''"

This script will be executed for each assets of all custom fields that matches the configuration. Assets also includes removed assets to let you control inventory. If you want to do nothing for assets removed from issue you can use if (assetStatus == 'removed') return null

7- Test it on Post Function Create Screen and publish workflow

8- Update assets with Update Asset transition


First: Issue has no assets 

3 Phones have 50, 40, 30 Quantity values


Issue updated with transition



Issue updated and 2 phones are updated with have 49 and 39 quantity values


Now 3rd asset is added


Quantity values after update is 49, 39, 29


Now 2 assets are removed


Quantity values after update is 49, 40, 30