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?
Community moderators have prevented the ability to post new answers.
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)
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Which namely variable is undeclared?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I fixed the first error, now I get this:
cannot find matching method java.lang.Object#compareTo(int) Line 3 column 1
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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() < 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?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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) < 999.9
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Does provided code work?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hm, try this one:
import com.atlassian.jira.component.ComponentAccessor return ((double) ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Price").getValue(issue)) < 999.9
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I see. You could create 3 transitions for approve and the only transition manager will see.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.