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).
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());
}
}
------------------------------------------------------
-----------------------------------------------------------------
Jira version : JIRA 7.1.7
ScriptRunner version : 4.3.1
-------------------------------------------------------------------
Thanks a lot !!!
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 !
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
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.