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

Next challenges

Recent achievements

  • Global
  • Personal

Recognition

  • Give kudos
  • Received
  • Given

Leaderboard

  • Global

Trophy case

Kudos (beta program)

Kudos logo

You've been invited into the Kudos (beta program) private group. Chat with others in the program, or give feedback to Atlassian.

View group

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

ScriptRunner Jira Cloud - Convert String to Integrer

I am attempting to create a scripted field that will calculate Risk based on Impact and Likelihood.

Both the Impact and Likelihood fields are a single select dropdown field. To do the calculation, I need to convert the String to an Integer. In Jira Datacenter, I know I can create an If/else statement that looks something like this:

def likelihood = issue.getCustomFieldValue(customFieldManager.getCustomFieldObjectByName("Risk Likelihood"))

if ( "Rare".equals (likelihood.getValue()) ) {

    likelihood = 1

} else if ( "Unlikely".equals (likelihood.getValue()) ) {

    likelihood = 2

} else if ( "Moderate".equals (likelihood.getValue()) ) {

    likelihood = 3

} else if ( "Likely".equals (likelihood.getValue()) ) {

    likelihood = 4

} else if ( "Almost Certain".equals (likelihood.getValue()) ) {

    likelihood = 5

} else {

    return null

}

 

On Jira Cloud, I can not use .equals

Does anyone know the Cloud equivalent?

3 answers

Hi Jeanne Howe and welcome,

 

Do you mind to share with us how your actual code looks like the whole stacktrace of the error to see if anyone here can help you with your problem? You can use https://pastebin.com/ site to share those files with us.

Depending of the language used on your Cloud script you will be able to use .equals() method or not, Groovy / Java based language have this method available for each existing variable on the code since this method is provided by Object.class, the one every variable on those languages extend from.

Best Regards

Hi Jeanne,

I can confirm if the value returned in your script is a string you can use the parseInt() method that groovy provides to return it as an integer as documented in the groovy docs located here.

I hope this helps.

Regards,

Kristian

@Kristian Walker _Adaptavist_ @Jack Nolddor _Sweet Bananas_ 

Thank you both for the responses. I have "cheated" a little bit here and add a numeric value to each of my fields, so I can parse the field instead of trying to convert the string to integer.

The scripted field is working, but I am trying to change the calculation that is being performed. The current calculation adds the two field values:

def output = impact[0..0]+likelihood[0..0] as Integer

I would like to multiply the two field values:

def output = impact[0..0]*likelihood[0..0] as Integer

 

To do this I believe I need to declare a method - but have no idea what that method is.

This is a snippet of the error I see:

org.codehaus.groovy.runtime.InvokerInvocationException: groovy.lang.MissingMethodException: No signature of method: java.lang.String.multiply() is applicable for argument types: (String) values: [1] Possible solutions: multiply(java.lang.Number) at TestScriptedFieldExecution2_groovyProxy.run(Unknown Source) Caused by: groovy.lang.MissingMethodException: No signature of method: java.lang.String.multiply() is applicable for argument types: (String) values: [1] Possible solutions: multiply(java.lang.Number)

 

Here is the code that I have working so far:

def input1CfId = 'customfield_12025'//Risk Likelihood string
def input2CfId = 'customfield_12024' //Risk Impact string
def outputCfId1 = 'customfield_12026' //Rick Score Integer

def impact = issue.fields[input1CfId]['value'] as String
def likelihood = issue.fields[input2CfId]['value'] as String

def output = impact[0..0]+likelihood[0..0] as Integer

put("/rest/api/2/issue/${issue.key}")
.header("Content-Type", "application/json")
.body([
fields:[
(outputCfId1): output
]
])
.asString()

 

Any thoughts on how to modify this to multiply the two values instead of adding?

(would also appreciate any thoughts you had on the script formatting. I am very new to this)

Jeanne

I believe I have the Scripted field working. Would appreciate any feed back on the script formatting, I am still very new to this. Here is my scripted field:

 

def input1CfId = 'customfield_12025'//Risk Likelihood string
def input2CfId = 'customfield_12024' //Risk Impact string
def outputCfId1 = 'customfield_12026' //Rick Score Integer

def impact = issue.fields[input1CfId]['value'] as String
def likelihood = issue.fields[input2CfId]['value'] as String

def impactint = impact[0] as Integer
def likelihoodint = likelihood[0] as Integer

def output = impactint * likelihoodint as Integer

return output

put("/rest/api/2/issue/${issue.key}")
.header("Content-Type", "application/json")
.body([
fields:[
(outputCfId1): output
]
])
.asInteger

Hi Jeane,

The error message indicates the values are still stored as strings and you cannot perform mathematical operations on Strings.

This means you will need to parse the strings to convert them to integer values which are stored as integer values and then you can multiply these integer values in your script.

Regards,

Kristian

Thanks Kristian,

I have modified my comment above. I think I have it working.

Seems I still do not have a "number". When I attempt to PUT the Risk Score back to the issue, I get this message:

 

Invalid type

The scripted field ran successfully, but returned an invalid type. Please return a number.

Hi Jeane,

In you script above you just have the return statement of return output and you need to change this to convert the value returned as an integer value by changing the line to be similar to return output as Integer.

Also, I can confirm that scripted fields just calculate values on an issue when it is loaded and you cannot make a put request on a scripted field.

If you wish to make a put request to update another field then you need to use a Script Listener script which is configured to fire on the issue updated event.

Regards,

Kristian

@Kristian Walker _Adaptavist_ 

I have modified the script to 

return output as Integer

 

but I am still getting the same message:

Invalid type

The scripted field ran successfully, but returned an invalid type. Please return a number.

 

As before, if I run the script manually (using the Test option when creating the field) it  runs successfully and will update the ticket, but if I edit a ticket and modify the Risk Impact or Risk Likelihood the scripted field, Risk Score, is not updated.

Hi Jeanne,

As mentioned above, the reason is that you cannot run this code as a Scripted Field as I have explained as Scripted Fields cannot run put requests and only update when an issue is reloaded in the browser and not on issue update.

Also, I can confirm in your script that you are attempting to make a rest call after the return statement and the way that groovy works is, that once the return keyword is reached then the script will terminate, so this means you should have the return statement as the last line in your script so that the rest calls are made before the return statement is reached.

As mentioned previously if you wish to have the script update field values after the issue is edited, then you will need to rewrite this script as a script listener to run on the issue updated event so that it updates the field values when the issue is updated as Scripted Field values are only calculated when an issue is loaded.

Regards,

Kristian

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PERMISSIONS LEVEL
Site Admin
TAGS

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