Forums

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

Added field in script field is breaking script for old tickets - Script Fields

bSte October 5, 2018

I added a new field and updated the code but it breaks the code on all the old tickets, how do I fix this? The above script does not work.

1 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.
October 5, 2018

Do all five of your variables you are calculating from always have a value?

bSte October 9, 2018

Yes, except in the case where the field does not exist on old tickets, then there are only 4 values.

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.
October 9, 2018

You'll need to cope with the absence of the data in the script, so the script knows to not try to do anything with it.

Imagine you're trying to add 3 fields in the new script.  Instead of

total += fieldValue3

use

if (fieldValue3) { total += fieldValue3 }

bSte October 9, 2018

I'm not sure I'm following how to go about that. I have tried to different ways of going about it: 

 

def total = ((bvInt + tcInt + rrInt + svInt) / sizeInt) * 100;
def totalNull = ((bvInt + tcInt + rrInt) / sizeInt) * 100;
def result;

if(svInt==1 || 5 || 8 || 13){
return total
}else {
return totalNull
}

OR 

 def total = ((bvInt + tcInt + rrInt + svInt) / sizeInt) * 100
def totalNull = ((bvInt + tcInt + rrInt) / sizeInt) * 100
def result

if(svInt!=null){
return total
} else {
return totalNull
}
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.
October 9, 2018

You need to check if a field value is valid before you try to use it in a calculation.

bSte October 10, 2018
import com.atlassian.jira.ComponentAccessor
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.ComponentManager
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


CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
CustomField bvField = customFieldManager.getCustomFieldObject("customfield_17937")
CustomField tcField = customFieldManager.getCustomFieldObject("customfield_17944")
CustomField rrField = customFieldManager.getCustomFieldObject("customfield_17941")
CustomField sizeField = customFieldManager.getCustomFieldObject("customfield_17942")
CustomField svField = customFieldManager.getCustomFieldObject("customfield_19311")

String bvValue = issue.getCustomFieldValue(bvField).toString()
String tcValue = issue.getCustomFieldValue(tcField).toString()
String rrValue = issue.getCustomFieldValue(rrField).toString()
String sizeValue = issue.getCustomFieldValue(sizeField).toString()
String svValue = issue.getCustomFieldValue(svField).toString()

double sizeInt = Integer.parseInt(sizeValue)
double rrInt = Integer.parseInt(rrValue)
double tcInt = Integer.parseInt(tcValue)
double bvInt = Integer.parseInt(bvValue)
double svInt = Integer.parseInt(svValue)

def svTotal = ((bvInt + tcInt + rrInt + svInt) / sizeInt) * 100
def totalNull = ((bvInt + tcInt + rrInt) / sizeInt) * 100
def result

if(svInt==1 || 5 || 8 || 13){
result = svTotal
}else {
result = totalNull
}
return result
bSte October 10, 2018

Do I also need to validate within the if/else statement itself?

bSte October 10, 2018
import com.atlassian.jira.ComponentAccessor
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.ComponentManager
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


CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
CustomField bvField = customFieldManager.getCustomFieldObject("customfield_17937")
CustomField tcField = customFieldManager.getCustomFieldObject("customfield_17944")
CustomField rrField = customFieldManager.getCustomFieldObject("customfield_17941")
CustomField sizeField = customFieldManager.getCustomFieldObject("customfield_17942")
CustomField svField = customFieldManager.getCustomFieldObject("customfield_19311")

String bvValue = issue.getCustomFieldValue(bvField).toString()
String tcValue = issue.getCustomFieldValue(tcField).toString()
String rrValue = issue.getCustomFieldValue(rrField).toString()
String sizeValue = issue.getCustomFieldValue(sizeField).toString()
String svValue = issue.getCustomFieldValue(svField).toString()

double sizeInt = Integer.parseInt(sizeValue)
double rrInt = Integer.parseInt(rrValue)
double tcInt = Integer.parseInt(tcValue)
double bvInt = Integer.parseInt(bvValue)
double svInt = Integer.parseInt(svValue)

def svTotal = ((bvInt + tcInt + rrInt + svInt) / sizeInt) * 100
def totalNull = ((bvInt + tcInt + rrInt) / sizeInt) * 100
def result

if(svInt==1 || 5 || 8 || 13){
result = svTotal
}else {
result = totalNull
}
return result
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.
October 10, 2018

No.  The if statement is the validation.  The code I gave before

if (fieldValue3) { total += fieldValue3 }

in English is:

If you have anything at all for fieldValue3, then add it to total

(This will still fail if fieldValue3 is not a number, but the main thing here is that the if is checking to see if there's anything there at all)

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.
October 10, 2018

Sorry, our comments crossed over.  In your code, you're still not checking that you have a value before you try to work with it.

The "if" I've given needs to check a value is there before any conversions or sums.

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events