Hello,
I am completely new to Groovy scripting and I don't know much javascript either so any help with this would be appreciated.
I am trying to get the built in script: Calculated custom field to to work in the context of 1 of my projects. I have replaced the values in the default script in order to match the context of my project:
def input1CfId = 'customfield_11493'
def input2CfId ='customfield_11494'
def outputCfId = 'customfield_11485'
def projectKey = "ITSD"
if (issue == null || ((Map)issue.fields.project).key != projectKey) {
logger.info("Wrong Project \${issue.fields.project.key}")
return
}
def input1 = issue.fields[input1CfId] as Integer
def input2 = issue.fields[input2CfId] as Integer
if (input1 == null || input2 == null) {
logger.info("Calculation using \${input1} and \${input2} was not possible")
return
}
def output = input1 + input2
if (output == (issue.fields[outputCfId] as Integer)) {
logger.info("already been updated")
return
}
put("/rest/api/2/issue/\${issue.key}")
.header("Content-Type", "application/json")
.body([
fields:[
(outputCfId): output
]
])
.asString()
Both of my input fields are number fields (created through administration > add new field. The output field is just a text field created the same way.
However I keep getting either:
CorrelationId: 38faeca5-8626-4696-a55e-d635d564188f RUN Script identifier: 48b631c9-49e0-4658-afde-4c034cbb50f5 com.adaptavist.sr.cloud.events.WebhookExecution (jira:issue_updated webhook fired for issue ITSD-1496) Script name: calc Took: 400ms Logs: 2019-09-03 14:25:05.044 INFO - PUT /rest/api/2/issue/${issue.key} asString Request Duration: 0ms 2019-09-03 14:25:05.060 ERROR - java.net.URISyntaxException: Illegal character in path at index 90: https://avst.connect.adaptavist.com/proxy-service/rest/api/1/jira/proxy/rest/api/2/issue/${issue.key} 2019-09-03 14:25:05.080 ERROR - Class: com.adaptavist.sr.cloud.events.WebhookExecution, Config: null
or
2019-09-03 14:22:51.223 ERROR - Cannot execute null+null on line 25 /
"Calculation using \${input1} and \${input2} was not possible")
I did look up scriptrunner's documentation and I ran some code to check the value of the fields I defined in this script, they did return the correct values in the console so I can confirm they are not null:
package com.adaptavist.sr.cloud.samples.events
def issueKey = 'ITSM-1496' def customFieldName = 'customfield_11493'
def result = get("/rest/api/2/issue/${issueKey}?fields=${customFieldName}") .header('Content-Type', 'application/json') .asObject(Map)
if (result.status == 200) { return result.body.fields[customFieldName] }
else { return "Error retrieving issue ${result}" }
Any help is greatly appreciated, I'm sure its just something small I am missing but its just not coming to me.
Thanks,
El
Hi EL,
Thank you for your question.
I can confirm that the Calculated Field script listener example provided in the ScriptRunner for Jira Cloud documentation here requires each of the custom fields to be a number type field.
Can I please ask you to navigate to the custom fields page in your instance and to check the fields with the ID's of customfield_11493, customfield_11494 and customfield_11485 and to confirm that these are all number type fields and if not to update the script to use number type fields.
Also I notice that the script above contains lots of \ characters which are invalid so I would ask you to remove these characters in order to ensure that your script is as per the example in the documentation.
If this response has answered your question can you please mark it as accepted so that other users can see it is correct when searching for similar answers.
Regards,
Kristian
Thank you so much ! Sum is appearing now.
-El
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.