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
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)
I would use HAPI from Scriptrunner for that
https://docs.adaptavist.com/sr4js/latest/hapi/work-with-assets-insight
You can also have a look to this community post.
Regards
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online 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.