Forums

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

How to I get the values of cascading field using ScriptRunner

Zaldy Parian
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.
November 24, 2023

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,

1 answer

1 accepted

0 votes
Answer accepted
Bobby Bailey
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.
November 25, 2023

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

Zaldy Parian
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.
November 26, 2023

Thanks for your guidance, @Bobby Bailey 

 

I couldn't get pass the def item code.

Then similarly with def parentValue and childValue code lines.

cascading3.png cascading1.png cascading2.png

Bobby Bailey
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.
November 26, 2023

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

Like Zaldy Parian likes this
Zaldy Parian
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.
November 30, 2023

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.

Like Bobby Bailey likes this

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
STANDARD
PERMISSIONS LEVEL
Product Admin
TAGS
atlassian, atlassian government cloud, fedramp, webinar, register for webinar, atlassian cloud webinar, fedramp moderate offering, work faster with cloud

Unlocking the future with Atlassian Government Cloud ☁️

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 Now
AUG Leaders

Atlassian Community Events