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

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,559,031
Community Members
 
Community Events
184
Community Groups

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 Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
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 Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
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
TAGS
AUG Leaders

Atlassian Community Events