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

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

Arach Idjadi February 7, 2018

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

Suggest an answer

Log in or Sign up to answer
0 votes
Nic Brough -Adaptavist-
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.
February 7, 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.

Arach Idjadi February 7, 2018

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 Brough -Adaptavist-
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.
February 7, 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.

Arach Idjadi February 7, 2018

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.

TAGS
AUG Leaders

Atlassian Community Events