Forums

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

How to get attribut for multi-select-user-field with groovy

Sokratis I_
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
April 19, 2023

I want to use Groovy in a post-function to get the approver from a custom field of the type Multi-User-Select and then read a specific attribute from Insight, where the approver is stored as an object.

The script I have does not work. It only works if the user is read from an Assets-Objects field.

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField

CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
Class objectFacadeClass = ComponentAccessor.getPluginAccessor().getClassLoader().findClass("com.riadalabs.jira.plugins.insight.channel.external.api.facade.ObjectFacade")
def objectFacade = ComponentAccessor.getOSGiComponentInstanceOfType(objectFacadeClass)

CustomField u = customFieldManager.getCustomFieldObject(10406)
ArrayList myValue = issue.getCustomFieldValue(u) as ArrayList
ArrayList myAttributes = []
return myValue
myValue.each {
def myAttrValue = objectFacade.loadObjectAttributeBean(it.getId(), 7201).getObjectAttributeValueBeans()[0].getValue()
myAttributes.add(myAttrValue)
}

 

1 answer

1 accepted

1 vote
Answer accepted
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.
April 19, 2023

Presumably, your user object type in Insight has an attribute of type Jira User. Or at least a user name or something else that we can use to find the user based on the selected users in the user picker.

So you should be able to use the iqlFacade to search for the user object and then retrieve the attributes that way.

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.user.ApplicationUser
import com.onresolve.scriptrunner.runner.customisers.PluginModule
import com.onresolve.scriptrunner.runner.customisers.WithPlugin
import com.riadalabs.jira.plugins.insight.channel.external.api.facade.IQLFacade
import com.riadalabs.jira.plugins.insight.channel.external.api.facade.ObjectFacade

CustomFieldManager customFieldManager = ComponentAccessor.customFieldManager
@WithPlugin('com.riadalabs.jira.plugins.insight') insightPlugin
@PluginModule ObjectFacade objectFacade
@PluginModule IQLFacade iqlFacade
issue = issue as Issue
CustomField approversCf = customFieldManager.getCustomFieldObject(10406)
List<ApplicationUser> selectedApprovers = issue.getCustomFieldValue(approversCf) as List<ApplicationUser>

def assetAttributes = selectedApprovers.findResults {user->
def userObjectList = iqlFacade.findObjects("""objectType = "Your user Object" and "Jira Account" = $user.key """)
if(!userObjectList) {
log.warn "Unable to find user object using $user"
return null
}
def userObject = userObjectList[0]
objectFacade.loadObjectAttributeBean(userObject.id, 7201).objectAttributeValueBeans*.value[0]
}

return assetAttributes

I also added a couple of groovy/scriptrunner shortcuts.

Sokratis I_
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
April 20, 2023

I don't use/have ScriptRunner installed. Are there any options without scriptrunner shortcuts?

The user(s) is stored in my custom field 10406 in the following format:

[fisrtname.lastname@company.com(firstname.lastname@company.com)]
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.
April 21, 2023

Sorry, I missed that fact (about not having scriptrunner).

Here is a version without scriptrunner shortcuts:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.user.ApplicationUser
import com.riadalabs.jira.plugins.insight.channel.external.api.facade.IQLFacade
import com.riadalabs.jira.plugins.insight.channel.external.api.facade.ObjectFacade

CustomFieldManager customFieldManager = ComponentAccessor.customFieldManager
ObjectFacade objectFacade = ComponentAccessor.getOSGiComponentInstanceOfType(ObjectFacade)
IQLFacade iqlFacade = ComponentAccessor.getOSGiComponentInstanceOfType(IQLFacade)
issue = issue as Issue
CustomField approversCf = customFieldManager.getCustomFieldObject(10406)
List<ApplicationUser> selectedApprovers = issue.getCustomFieldValue(approversCf) as List<ApplicationUser>

def assetAttributes = selectedApprovers.findResults { user ->
def userObjectList = iqlFacade.findObjects("""objectType = "Your user Object" and "Jira Account" = $user.key """)
if (!userObjectList) {
log.warn "Unable to find user object using $user"
return null
}
def userObject = userObjectList[0]
objectFacade.loadObjectAttributeBean(userObject.id, 7201).objectAttributeValueBeans*.value[0]
}

return assetAttributes

Be sure to update the IQL to the correct attribute name from your insight object that matches with either the user's name or user's key. In my example, I used "Jira Account" yours might be different.

Like Sokratis I_ likes this

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events