How to automatically increment Assets Integer attribute using Automation for Assets
Platform Notice: Cloud - This article applies to Atlassian products on the cloud platform.
Summary
Automatically incrementing a numeric attribute among objects from the same object type is useful in scenarios where you need an ID that is exclusive to that particular object type and it needs to be sequential.
It is possible to implement this by making use of automation for Jira. This article will detail steps on how to create a rule that will be triggered every time a new object from a particular object type is created. It will then detect the value from an integer attribute from the second newest created object (because the first would be the object that was just created). It will then increment this value by 1, and add this value to the newly created object.
Environment
Jira Service Management Cloud and Assets.
Solution
Step 1 - Add an integer attribute to your object type
Navigate to Assets and open the object scheme where you have the object type that needs the auto-increment. Select the object type and add a new integer attribute. In this example, I've named it "Increment".
Step 2 - Create the automation rule
Go to System (Cog Icon) > Global Automation (on the panel to the left) and start a new rule with the following components:
Object Trigger: On the Created tab, select the object schema where the object resides (this trigger is only available in Global Automations).
IQL condition: This will be used so that this rule only applies to objects from the specified object type. In this example, I'm using the "Laptops" object type, so replace this part of the AQL with the name of your object type:
Lookup Objects: This will be used to collect the increment value from the last created object. The AQL used here should be the same as the AQL condition, but we need to add an ORDER BY clause so the objects are returned in the newest to oldest order.
Create Variable: This will be used to store the value from the object returned from the Lookup Objects action with the following smart value:
{{lookupObjects.get(1).Increment}}
Edit Object Attribute: Now that we have the data from the last created object stored in the variable, we'll use the Edit Object Attribute action to edit the integer attribute of the newly created object and increment it with some math expressions. On the dropdown, select the integer attribute that you're using for the auto-increment, and use the following smart value (number being the name of the variable we created earlier):
{{#=}}{{number}} + 1{{/}}
- Replace "Increment" with the name of your own attribute.
After that, name the rule and save it. New objects created from that object type will have the integer attribute automatically incremented.