Hi,
I need some help with the groovy script. First of all, I honestly don't know much about the groovy script. I play around with it by looking online. One of my coworkers helped me with this script below but he left the company. I need some help in updating this to fix the error.
I have two fields:
The goal of this script is to calculate a value based on = Priority Field/Story Points.
The script works when there are values entered for both the story points and priority fields but it gives null with an error when no value is entered for either of those fields. Following is the error message. I will paste the script below it as well. Any help would be appreciated. I have a similar problem with a few other scripts that are giving error with NULL when no value is entered. I really don't know how to fix this. Please help!
Thank you so much!
Error:
2020-04-15 12:53:44,914 ERROR [runner.ScriptFieldPreviewRunner]: ************************************************************************************* 2020-04-15 12:53:44,916 ERROR [runner.ScriptFieldPreviewRunner]: Script field preview failed for field that has not yet been created java.lang.NumberFormatException: For input string: "null" at java_lang_Float$parseFloat.call(Unknown Source) at Script324.run(Script324.groovy:11)
Script:
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.IssueManager;
import com.atlassian.jira.issue.Issue;
import java.text.SimpleDateFormat;
def issueManager = ComponentAccessor.getIssueManager()
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def storyPointsField = customFieldManager.getCustomFieldObject((Long)10018)
def storyPoints = Float.parseFloat(issue.getCustomFieldValue(storyPointsField).toString())
def priorityField = customFieldManager.getCustomFieldObject((Long)14103)
def priority = issue.getCustomFieldValue(priorityField).toString()
def priorityNumber = Integer.parseInt(priority.split("-")[0].trim())
def effort = Double.parseDouble(((6-priorityNumber)/storyPoints).toString())
return effort
Hi @Shahriar
please, which value should be returned, if one of the fields or both are empty?
I will try to fix it for you, but this is not clear. Thank you.
Hi Hana, thank you for the quick response.
Well, I get the error when one or both the fields are empty.
For e.g.
Story Point: EMPTY
Priority Field: 3 - Needed
or
Story Point: 4
Priority Field: EMPTY
or
Story Point: EMPTY
Priority Field: EMPTY
I would like a value of "O" or NULL to be returned if either of the fields is empty. Currently, it does return NULL but along with that error.
I am sorry for the confusion. Please let me know if you need more information. Thank you for your help.
- Shahriar
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi @Shahriar ,
thank you, please try this:
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
final String storyPointsFieldId = "customfield_10018"
final String priorityFieldId = "customfield_14103"
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
CustomField storyPointsField = customFieldManager.getCustomFieldObject(storyPointsFieldId)
CustomField priorityField = customFieldManager.getCustomFieldObject(priorityFieldId)
Double storyPoints = issue.getCustomFieldValue(storyPointsField) as Double
String priorityString = issue.getCustomFieldValue(priorityField) as String
Integer priorityNumber = priorityString ? Integer.parseInt(priorityString.split("-")[0]?.trim()) : null
Number effort = priorityNumber > 0 && storyPoints > 0 ? (6 - priorityNumber)/storyPoints : 0
return effort
Select template "Number field" in definition of your scripted field.
Please let me know, if it works for you. Thank you.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Hana,
You are a freaking GENIUS!!! It works!!
It works but for some reason, it is also giving a syntax error. But it still works and lets me save it without error. Let me provide a screenshot of the syntax error below. Thank you again so much! This is so helpful!
- Shahriar
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi @Shahriar ,
thank you, I'm happy to help.
Try to fix it with replacing
Number effort = priorityNumber > 0 && storyPoints > 0 ? (6 - priorityNumber)/storyPoints : 0
with:
Double effort = priorityNumber > 0 && storyPoints > 0 ? (6 - priorityNumber)/storyPoints as Double : 0
But the error doesn't mind much.
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.
@Shahriar I can see you know well groovy script and I am stuck from a busines case I need to solve, could you help ?
From a Service Request submit by a user, I need to read the Email field and store it in a groovy script variable in order to be used to an REST API call.
Do you know how to get this Email value from script ?
thanks for help
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.