Forums

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

Update custom field via groovy script

Dominik Hufnagel January 4, 2024

Hi together,

in our Jira-Project we have a custom field of type "asset object". This custom field allows multiple value selection.

I wrote a groovy script, which set a new value and then update the custom field. The specific part of my code is shown below:

//Define the specific customfield of Task
def cfID_12500 = "customfield_12500"

//Get the customfield object
def cfObject_12500 = ComponentAccessor.getCustomFieldManager().getCustomFieldObject(cfID_12500)

// Get the custom field value for the specific issue
def cfValue_12500 = issue_parent.getCustomFieldValue(cfObject_12500)

//The list contain duplicate values (e.g. ABC, XYZ, ABC, HIZ)
//First remove all duplicates by converting it to a Set and then back to List
def uniqueSet_of_OEs = listOEs_of_Tasks as Set
def uniqueListOEs_of_Tasks = uniqueSet_of_OEs.toList()

//Set new values for the asset object field
def cfnewValue_12500 = uniqueListOEs_of_Tasks
issue.setCustomFieldValue(cfObject_12500, cfnewValue_12500)

//Update the issue
ComponentAccessor.issueManager.updateIssue(ComponentAccessor.jiraAuthenticationContext.getLoggedInUser(), issue, EventDispatchOption.DO_NOT_DISPATCH, false)

 Somehow my code is not working. When I execute it, it throw an error in the last line.

Error

2024-01-04 08:42:38,891 ERROR [common.UserScriptEndpoint]: Script console script failed:
java.lang.ClassCastException: java.util.ArrayList cannot be cast to com.riadalabs.jira.plugins.insight.services.model.ObjectBean

I understand the error, but I don't know how to solve it.

Can I use the "update issue" only for variables with a single value?

Would be great to get some help

Best Regards Dominik

 

2 answers

0 votes
PD Sheehan
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.
January 24, 2024

It's true that this would be much easier with HAPI as mentioned by Florian

import com.riadalabs.jira.plugins.insight.services.model.ObjectBean
def fieldId = 12500
issue.update{
def parentValue = issue.parentObject.getCustomFieldValue(fieldId)
setCustomFieldValue(fieldId, parentValue.unique() as ObjectBean[])
}

But you should also be able to work with pure jira api. I'm not quite sure why your code is not working. There are clearly some lines missing in what you shared with us.

But I think we can simplify it. Did you know that is a .unique() method in all groovy collections?

Something along those lines might work for you

//Define the specific customfield of Task
def cfID_12500 = "customfield_12500"

//Get the customfield object
def cfObject_12500 = ComponentAccessor.customFieldManager.getCustomFieldObject(cfID_12500)

// Get the custom field value for the specific issue
def cfValue_12500_parent = issue_parent.getCustomFieldValue(cfObject_12500) as List<ObjectBean>

//Set new values for the asset object field
issue.setCustomFieldValue(cfObject_12500, cfValue_12500_parent.unique())

//Update the issue
ComponentAccessor.issueManager.updateIssue(ComponentAccessor.jiraAuthenticationContext.getLoggedInUser(), issue, EventDispatchOption.DO_NOT_DISPATCH, false)
0 votes
Florian Bonniec
Community Champion
January 4, 2024

Suggest an answer

Log in or Sign up to answer