Hi all,
We're using JSM Cloud and I'm exploring ScriptRunner to assign value to a custom field based on the values (parent and child) of a cascading field.
My custom cascading field is Issue category / sub-category.
How do I get the values of parent and child cascading field in ScriptRunner Listener when it is changed?
I'm really desperate to get the script working as soon as possible.
Regards,
Hi @Zaldy Parian ,
There are a couple of steps that you need to go through to do this. First, as you have mentioned you want to set up a listener that executes when the issue is updated.
Then, we need to check to see if the cascading field was updated. When you create a listener, you will see in the script context that we have the changelog variable defined. This gives us access to what was changed in this update, you can see the structure of this data here: https://developer.atlassian.com/server/jira/platform/webhooks/#executing-a-webhook
So, using the follow if statement, we filter out any change that doesn't involve the cascading field:
// Change 'Custom Cascading Fields' to your field name
def item = changelog.items.find{ item -> item.field == "Custom Cascading Fields"}
if(item){
// code here will execute if the cascading fields have been updated
}
Then, we can access the issue data to get the field values:
// Change 'customfield_10123' to your custom field name
def parentValue = issue.fields.customfield_10123.value
def childValue = issue.fields.customfield_10123.child.value
So, total code would look like this:
def item = changelog.items.find{ item -> item.field == "Custom Cascading Fields"}
if(item){
def parentValue = issue.fields.customfield_10123.value
def childValue = issue.fields.customfield_10123.child.value
// Add any logic you need from the field values here
} else {
logger.error('Wrong update!')
}
Does this help?
Kind regards,
Bobby
Thanks for your guidance, @Bobby Bailey
I couldn't get pass the def item code.
Then similarly with def parentValue and childValue code lines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi @Zaldy Parian ,
Those warnings are because the editor doesn't have knowledge of the data type we are processing, but they are just warnings. The script will still work because, despite the editor not recognising it, the data is there. You can add checks by adding '?' before all '.'s.
For example, issue.fields.customfield_xxxxx.value
Becomes
issue?.fields?.customfield_xxxxx?.value.
Regardless, despite the warnings, the script should still still work.
Kind regards,
Bobby
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi @Bobby Bailey ,
You're absolutely right. Those are just warning and most of the times can be ignored.
In the Script Console, I tried the snippet and it did not error out. I also tried this, having 4 warnings, and it worked....
def result = get("/rest/api/2/issue/${"TEST-37"}")
.header('Content-Type', 'application/json')
.asObject(Map)
.body
.fields
Category_parent = result.customfield_10220.value
Category_child = result.customfield_10220.child.value
logger.info ("Parent value: ${Category_parent}")
logger.info ("Child value: ${Category_child}")
Thanks a lot for your help and guidance.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Atlassian Government Cloud has achieved FedRAMP Authorization at the Moderate level! Join our webinar to learn how you can accelerate mission success and move work forward faster in cloud, all while ensuring your critical data is secure.
Register NowOnline 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.