Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

How to use a numerical field to transition a ticket based on a number range

Eric Smith January 9, 2017

I am working on using script runner to create a Purchasing workflow for our Finance department to use in order to streamline our purchasing. In order to accomplish this, I need the Price (numerical) field to transition based on a numerical range:

 

If the Price is below $999, it needs to transition to manager A

If the Price is between $1000 and $9999, it needs to transition to Manager B

If the price is $10000 or above, it needs to transition to manager C

 

I searched the internet as well as the forum, but I dont see anything regarding numerical field ranges. Is this possible with script runner?

1 answer

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

1 vote
Vasiliy Zverev
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 9, 2017

Here is a draft for your case:

import com.atlassian.jira.component.ComponentAccessor

double price = issue.getCustomFieldValue(ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Price"))
String managerName;

if(price < 1_000) {
    managerName = "manager A"
}else if(price < 10_000) {
    managerName = "manager B "
} else
    managerName = "manager 3"


issue.setAssigneeId(managerName)
Eric Smith January 9, 2017

I actually lackadaisically used Manager to represent a transition, what I plan on doing is more like this:

 

If price is less than $1000, transition to transition1, manager A

If price is between $1000 and $9999, transition to transition1 manager A, after manager A approves, transition to transition2 manager B

If price is above $10000, transition to status1 ManagerA, after manager A approves, transition to status2 manager B, after manager B approves, transition to status3 manager C

 

I have created a workflow that has a transition and a status for each manager. In my current iteration I created a post-function that checks a custom field with three options (below $1000, between $1000 and $999, above $10000) that transitions the ticket depending on what the customer selects. However, im hoping to be able to swap out each post function at their respective transitions to make the price field more intelligent. 

 

Im guessing that I just need one post-function for each of the transitions, using the price to drive the ticket higher up the ladder. I just need to know what each of them need to be. Sorry for the confusion, I should have just explained it properly to begin with. Thanks for your help.

 

EDIT: I also accidentally made this an answer instead of a comment and cant change because im under 3 points.

Vasiliy Zverev
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 9, 2017

Since I would reccomned to create a script condition for each transition. Like that:

import com.atlassian.jira.component.ComponentAccessor

issue.getCustomFieldValue(ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Price")) < 1_000

So, on current state user will be able to make the only transition.

Eric Smith January 17, 2017

Its returning an error in the variable (the variable is undeclared), as well as saying cannot find matching method com.atlassian.jira.issue.Issue#GetCustomFieldValue(java.lang.object).

 

I have simplified this a bit and decided to instead use the fast-track transition option. So when Price is less than $1000, it needs to fast track to transition 971. Im trying to define price something like this:

cfValues['Price'] < 999

Vasiliy Zverev
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 17, 2017

Which namely variable is undeclared?

Eric Smith January 17, 2017

I fixed the first error, now I get this:

 

cannot find matching method java.lang.Object#compareTo(int) Line 3 column 1

Eric Smith January 18, 2017
We have been working on it and I think we are close:

import com.atlassian.jira.ComponentManager
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.Issue
ComponentManager componentManager = ComponentManager.getInstance()
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
CustomField customField = customFieldManager.getCustomFieldObjectByName("Price")
String value = customField.getValue(issue)
value.toInteger() &lt; 999.9

 

 

Everything looks like its working, but its not. This is in the log:

 

2017-01-18 15:27:52,880 ERROR [utils.ConditionUtils]: *************************************************************************************
2017-01-18 15:27:52,893 ERROR [utils.ConditionUtils]: Condition failed
on issue: HDSD-3889, built-in script:com.onresolve.scriptrunner.canned
.jira.workflow.postfunctions.FasttrackTransition java.lang.NumberFormatException: For input string: "800.0" at Script56.run(Script56.groovy:10) 2017-01-18 15:27:52,894 ERROR [utils.ConditionUtils]: Script follows: import com.atlassian.jira.ComponentManager import com.atlassian.jira.component.ComponentAccessor import com.atlassian.jira.issue.CustomFieldManager import com.atlassian.jira.issue.fields.CustomField import com.atlassian.jira.issue.Issue ComponentManager componentManager = ComponentManager.getInstance() CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager() CustomField customField = customFieldManager.getCustomFieldObjectByName("Price") String value = customField.getValue(issue) value.toInteger() > 999

 

Any ideas?

Vasiliy Zverev
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 18, 2017

What type for custom field "Price": is a a text or number field?

For number field you could use:

import com.atlassian.jira.component.ComponentAccessor

return ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Price").getValue(issue) &lt; 999.9
Eric Smith January 18, 2017

Its a number field

Vasiliy Zverev
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 18, 2017

Does provided code work?

Eric Smith January 19, 2017

When I put that into fast-track a transition, I get:

 

cannot find matching method java.lang.object#compareTo(java.math.BigDecimal). Please check if the declared type is right and if the method exists @ line 3 column 102

Vasiliy Zverev
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 19, 2017

Hm, try this one:

import com.atlassian.jira.component.ComponentAccessor

return ((double) ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Price").getValue(issue)) &lt; 999.9
Eric Smith January 19, 2017

It appears that it worked! However, I am trying to have it change to a transition that is hidden. It works unhidden but I would like for them to not make that transition unless they are over $999, which is then done automatically. I tried to skip the Condition of the transition that im trying to land on, but im getting these errors:

in atlassian-jira.log:

Unhandled object type for storing in workflow XML: param: FIELD_TRANSITION_OPTIONS, value: [FIELD_SKIP_CONDITIONS], type: java.util.ArrayList

.

In script runner:

2017-01-19 23:33:45,994 ERROR [utils.WorkflowUtils]: In-depth checking of each condition follows to determine why the action was not allowed. 
2017-01-19 23:33:46,007 ERROR [utils.WorkflowUtils]: 
It seems that you have tried to perform a workflow operation (Requires Higher Approval) that is not valid for the current state of this issue (HDSD-3893). The likely cause is that somebody has changed the issue recently, please look at the issue history for details.
2017-01-19 23:33:46,008 ERROR [utils.WorkflowUtils]: Not attempting transition because of above errors.

 

Is there any way to make the transition ignore the condition so it can transition to the hidden status?

Vasiliy Zverev
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 19, 2017

Why do you need to hide transitions?

You have 3 visible transitions. If conditions are correct then user can see only one of it. Is it really requered to hide transitions?

Eric Smith January 22, 2017

From the status "Waiting for Manager", there are three transitions: Approve and Deny are visible, Send to Upper Management is hidden. When the manager clicks approve, the postfunction you gave me that reads the Price field cycles through approve and clones the ticket to the Finance department. Immediately after that, it uses the Price field to calculate if it needs to carry on to "Pending Purchase" (If the price is <999), or if it needs to instead transition to transition to Send to Upper Management (If the price is >999). This way, the manager does not have to decide which button to push, they merely have to decide if they want to approve or deny the purchase.

Vasiliy Zverev
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 22, 2017

I see. You could create 3 transitions for approve and the only transition manager will see.

TAGS
AUG Leaders

Atlassian Community Events