Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Groovy script giving error with NULL

Shahriar April 15, 2020

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:

  • Story points (number field)
  • Priority Field (single select dropdown field with options)
    • 1 - Roadmap/Critical
    • 2 - Urgent
    • 3 - Needed
    • 4 - Nice to have
    • 5 - Want

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

1 answer

1 accepted

0 votes
Answer accepted
Hana Kučerová
Community Champion
April 15, 2020

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.

Shahriar April 15, 2020

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

Hana Kučerová
Community Champion
April 15, 2020

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.

Shahriar April 15, 2020

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! 

Screen Shot 2020-04-15 at 10.57.28 PM.png

- Shahriar

Hana Kučerová
Community Champion
April 15, 2020

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. 

Shahriar April 16, 2020

Hana, 

Thank you again so much! 

Stay safe during this crazy time and have a good day. 

 

Thanks, 

Shahriar

Like Hana Kučerová likes this
Hana Kučerová
Community Champion
April 16, 2020

Hi @Shahriar 

thank you, and same to you!

serge calderara
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.
May 1, 2020

@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

Suggest an answer

Log in or Sign up to answer