I am using the Scriptrunner listener capability of Jira cloud version to update a field called 'Urgency' based on the value of the field 'Severity'. This is for a servicedesk project.
I checked the screens (actually there is only 1 screen for create, view, edit) and the screen has both of these fields. Yet, I am not able to update the field value and it keeps giving me the above error. I am running the script as a "ScriptRunner Add-On User"
Could someone please help me to point out where the problem is and how can I address this?
Roughly, this is how my script looks:
def projectKey = "PS"
def issueKey = issue.key
def issueType = ((Map)issue.fields.issuetype).name as String
if (issueType == null || issueType != "Bug") {
logger.info("Wrong Issue Type ${issueType}")
return
}
def fields = issue.fields as Map
def severityField = get("/rest/api/2/field")
.asObject(List)
.body
.find {
(it as Map).name == 'Severity'
} as Map
def severityValue = (fields[severityField.id] as Map).value as String
def urgencyValue = "" as String
if (severityValue == "A" || severityValue == "B") {
urgencyValue = "Critical"
} else if (severityValue == "C") {
urgencyValue = "High"
} else if (severityValue == "D") {
urgencyValue = "Medium"
} else {
urgencyValue = "Low"
}
def urgencyField = get("/rest/api/2/field")
.asObject(List)
.body
.find {
(it as Map).name == 'Urgency'
} as Map
def result = put("/rest/api/2/issue/${issueKey}")
.queryString("overrideScreenSecurity", Boolean.TRUE)
.header('Content-Type', 'application/json')
.body([
fields: [
"{$urgencyField.key}": [
value: "${urgencyValue}"
]
]
])
.asString()
if (result.status == 204) {
return 'Success'
} else {
return "${result.status}: ${result.body}"
}
Hi Asha,
Thank you for confirming this.
I can confirm that to set the value for a field which is not on screen that you will need to override the overrideScreenSecurity query paramater in the Rest call where you are setting the field and this will require the Script Listiner to be ran as the ScriptRunner Add On user.
We have an example of how to set this paramter inside of our documentation example which is located here and you can use this as a reference for how to set this paramater.
If this does not resolve the issue could you please try setting the select list value using the syntax shown in the example code snippet located here.
This example can be ran on the Script Console and shows how you can update an issue and set a select list value on an issue.
You will be able to use this example in order to modify your script to set the select list field that you require.
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
Kristian,
Thank you very much for sharing the code snippet. While my code was almost same as the one recommended by the piece of code block you shared, comparing the two blocks of code side by side, helped me find one logical error I had in my code.
This is all the problem was:
fields: [
"{$urgencyField.key}": [
value: "${urgencyValue}"
]
]
had to be changed to:
fields: [
"${urgencyField.key}": [
value: "${urgencyValue}"
]
]
Now it is working absolutely fine. Thank you for all your help to get me here :)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Asha.
Is there any particular reason why you are sending REST request instead of using the class methods? I haven't setup this particular example but this:
def severityField = get("/rest/api/2/field")
.asObject(List)
.body
.find {
(it as Map).name == 'Severity'
} as Map
Can be done like this:
import com.atlassian.jira.component.ComponentAccessor
def severityField = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Severity")
----
This:
def urgencyField = get("/rest/api/2/field")
.asObject(List)
.body
.find {
(it as Map).name == 'Urgency'
} as Map
Can be done like this:
import com.atlassian.jira.component.ComponentAccessor
def urgencyField = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Urgency")
----
And this, which I suppose is a customField setter of an issue value:
def result = put("/rest/api/2/issue/${issueKey}")
.queryString("overrideScreenSecurity", Boolean.TRUE)
.header('Content-Type', 'application/json')
.body([
fields: [
"{$urgencyField.key}": [
value: "${urgencyValue}"
]
]
])
.asString()
Can be done like this:
issue.setCustomFieldValue(urgencyField, urgencyValue)
def issueManager = ComponentAccessor.getIssueManager()
def user = ComponentAccessor.jiraAuthenticationContext.getLoggedInUser()
issueManager.updateIssue(user, issue, EventDispatchOption.ISSUE_UPDATED, false)
----
Have you tried any of these?
Cheers!
DY
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
For whatever reason, I am not able to import the libraries like this in my script:
import com.atlassian.jira.component.ComponentAccessor
It keeps giving me error no matter which library I am trying to import. I tried a lot and then decided to keep it simple using APIs that probably allows me to do everything required. Haven't been successful either way though.
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.
Hi Asha.
Where it is that you are calling this script?
Cheers!
DY
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I have tried this in 2 modes and both have same problem:
Screenshot for the reference
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Asha.
I would like to extend my apologies. I didn't see the jira-cloud tag in the post, you don't mention that this is for Scriptrunner Cloud in the description of your question. That led me to believe this was for Jira Server. my previous comments were misleading and sadly, I have no experience with Jira Cloud, so I can't help you there.
I'm so very sorry, your original script is possibly right.
Maybe someone that knows Cloud can help you out.
Once again, apologies.
DYelamos
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
No problem Daniel. Thank you for your attempt. I also updated the issue description to call out that I am using cloud version. Hope someone helps me out with this.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Asha,
I can confirm that to set the value for a field which is not on screen that you will need to override the overrideScreenSecurity query paramater in the Rest call where you are setting the field and this will require the Script Listiner to be ran as the ScriptRunner Add On user.
We have an example of how to set this paramter inside of our documentation example which is located here and you can use this as a reference for how to set this paramater.
Also can I please ask what type of field is customfield_10406 so that we can verify that you are specifying the correct syntax to set the field as this error could also be caused if the script cannot recognise the field and how to set it.
Regards,
Kristian
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Kristian,
Thanks for trying to help on this. customfield_10406 is of select list type. The field from which I am deriving the value is also of the same type.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Asha,
Thank you for confirming this.
Can you please confirm if overriding the screen security query paramater resolves the issue and allows the field to be set.
If this does not resolve the issue could you please try setting the select list value using the syntax shown in the example code snippet located here.
This example can be ran on the Script Console and shows how you can update an issue and set a select list value on an issue.
You will be able to use this example in order to modify your script to set the select list field that you require.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Kristian,
If you noticed the code block I have shared, override screen security parameter is already set but that hasn't helped the situation. Let me try the other code snippet you have shared and get back to you on how things go.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Kristian,
Thank you very much for sharing the code snippet. While my code was almost same as the one recommended by the piece of code block you shared, comparing the two blocks of code side by side, helped me find one logical error I had in my code.
This is all the problem was:
fields: [
"{$urgencyField.key}": [
value: "${urgencyValue}"
]
]
had to be changed to:
fields: [
"${urgencyField.key}": [
value: "${urgencyValue}"
]
]
Now it is working absolutely fine. Thank you for all your help to get me here :)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Asha,
Thank you for confirming that the issue has been resolved.
Could you please accept this answer to mark it as correct and help others find it who are searching for a similar question in future.
Regards,
Kristian
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Kristian,
I am not able to do so for the specific comment that you have made. I am guessing the reason is because this is a comment under another comment and it doesn't make sense to mark that as accepted answer. Would you mind adding a separate comment with the same information provided earlier, so that I can flag that as the answer?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I have re added this as an answer below.
Regards,
Kristian
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Can someone please help on this? I am stuck with this and not been able to make progress. Tried a lot of suggestions on this forum and none has helped so far.
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.