It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

Script listener does not update field with correct decimal separator (transforms point to comma)

Hello community !

My script that should be replacing commas with '.' in a custom field, but it is not doing this in tickets.

Custom field named "global cost" is a "number field" type which is calculated in a script listener.

I need to know why my script listener is returning a value containing a comma, although in the script I use a comma to point function : .replace(",", ".")
Also I have tested it in a script console, and it gives me the correct result (decimal value with a point and not a comma).
On a ticket, listener does not fill correctly decimal value since point is replaced by a comma. Therefore, an error message "invalid number" blocks ticket creation. (see below).

invalid_number.PNG

Here is the update value line :


globalcostsField.updateValue(null, parent, new ModifiedValue(parent.getCustomFieldValue(globalcostsField), Double.parseDouble(String.format("%.2f", count * 0.65).replace(",", "."))), new DefaultIssueChangeHolder());

 ----------------------------------------------------------------

Whole script : 

//Custom listener 
//Projects: DE,FR,IT,UK,TEST
//Events: Issue Updated
//Update global cost of a user request according to the sum of subtasks effort

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 com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder

String tshirtField = "customfield_10207";
String costsField = "customfield_10208";

CustomFieldManager cfm = ComponentAccessor.getCustomFieldManager();
CustomField serviceType = cfm.getCustomFieldObject("customfield_10706");
CustomField globalcostsField = cfm.getCustomFieldObject("customfield_15402");
IssueManager im = ComponentAccessor.getIssueManager();

if (event.getIssue().getIssueTypeId() == "22") {
Issue parent = event.getIssue().getParentObject();

Double count = 0;
Double cost = 0;
for (Issue i : parent.getSubTaskObjects()) {
cost = i.getCustomFieldValue(cfm.getCustomFieldObject(costsField)) != null ? (Double) i.getCustomFieldValue(cfm.getCustomFieldObject(costsField)) : 0;
if (i.getCustomFieldValue(cfm.getCustomFieldObject(tshirtField)) != null)
{
switch (i.getCustomFieldValue(cfm.getCustomFieldObject(tshirtField)).toString().substring(0,2).replace(" ", "")) {
case "XS":
count += Math.max(2, cost);
break;
case "S":
count += Math.max(4, cost);
break;
case "M":
count += Math.max(9, cost);
break;
case "L":
count += Math.max(19, cost);
break;
case "XL":
count += Math.max(30, cost);
break;
}
}
else
{
count += cost;
}
}
if ((count * 0.65) > (Double)(parent.getCustomFieldValue(globalcostsField).toString() != "null" ? parent.getCustomFieldValue(globalcostsField) : 0)) {
globalcostsField.updateValue(null, parent, new ModifiedValue(parent.getCustomFieldValue(globalcostsField), Double.parseDouble(String.format("%.2f", count * 0.65).replace(",", "."))), new DefaultIssueChangeHolder());
}
}

 ------------------------------------------------------

Steps To Reproduce
- create custom field ("number field" type) named "global cost"
- create script listener updating "global cost" value (cf script attached)
- verify return result via script console add-on
- create a ticket containing "global cost" value automatically filled
- check value contains a comma and thus blocks ticket editing.

-----------------------------------------------------------------

Jira version : JIRA 7.1.7

ScriptRunner version : 4.3.1

-------------------------------------------------------------------

Thanks a lot !!!

 

1 answer

0 votes
nic Community Leader Feb 07, 2018

You cannot put formatting into a numeric field.  The field accepts whatever you have for a decimal point and stores it as a number, not a string.

It is your language settings that determine what you see when Jira renders a number.

Thanks for your quick feedback.

In fact, when I open this issue in "view" mode, I can see global cost is : "5.85". But if I open it in "editing" mode, I can see "5,85".

So when I try to edit another field ih the ticket (I do not modify this one since it is automatically filled), I am blocked with this error message.

Language settings is "english", I don't see why it is an issue.

I have found a Workaround by hiding this field in "edit" view. So I hope my user will be satisfied with that !

nic Community Leader Feb 07, 2018

You're missing the point - your code is trying to use a separator that is invalid.  You simply don't need to try to set it, just pass the number into the field.

Can you be more specific ?

I have tried not to replace "," to ".", by replacing former update line by this line : 

globalcostsField.updateValue(null, parent, new ModifiedValue(parent.getCustomFieldValue(globalcostsField), Double.parseDouble(String.format("%.2f", count * 0.65))), new DefaultIssueChangeHolder());

I have same error.

Please let me know your code suggestion.

Suggest an answer

Log in or Sign up to answer
Community showcase
Asked in Opsgenie

CLOSED: AMA with Serhat Can on everything Opsgenie

Update: Our AMA with @Serhat Can is complete! Thank you everyone for taking the time to write your questions and participate, you are the folks that make the community enriching and fun! If...

624 views 15 7
View question

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you