Forums

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

Insight groovy get attribute value from issue's customfield into multi user field

Nico Hoffmann October 25, 2018

Hello Community,

I do have an issue where I want to get an Insight object's attribute value (Approver, Jira User Object) of a customfield of an issue to put it into a multi user picker. I do have played around with code, but if you might be able to point me to the right direction it would be highly appreciated.

Here is my current code, which does not return the attribute value of the object:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.UpdateIssueRequest
import com.atlassian.jira.issue.fields.CustomField
import org.apache.log4j.Logger
import org.apache.log4j.Level

CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()

def approverCf = customFieldManager.getCustomFieldObject(10203)
def currentUser = ComponentAccessor.jiraAuthenticationContext.getLoggedInUser()

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

/* Get Insight IQL Facade from plugin accessor */
Class iqlFacadeClass = ComponentAccessor.getPluginAccessor().getClassLoader().findClass("com.riadalabs.jira.plugins.insight.channel.external.api.facade.IQLFacade");
def iqlFacade = ComponentAccessor.getOSGiComponentInstanceOfType(iqlFacadeClass);

/* Get Insight Object Attribute Facade from plugin accessor */
Class objectTypeAttributeFacadeClass = ComponentAccessor.getPluginAccessor().getClassLoader().findClass("com.riadalabs.jira.plugins.insight.channel.external.api.facade.ObjectTypeAttributeFacade");
def objectTypeAttributeFacade = ComponentAccessor.getOSGiComponentInstanceOfType(objectTypeAttributeFacadeClass);

/* Specify the schema id as well as the IQL that will fetch objects. In this case all objects with Name matching the valueCF, be sure to include " around value */
def insightobjects = iqlFacade.findObjectsByIQLAndSchema(3,"objectType = User AND object HAVING inR(\"\",referenceType in (\"approves\"))"); // See the complete list of possible IQL on the Insight Query Language documentation page
/* If this is a mandatory field you should be able to do this: */
log.debug("Assignee: " + issue.assignee)
log.debug("Insight objects: " + insightobjects.each {
println "Item: $it" // `it` is an implicit parameter corresponding to the current element
});

def getInsightAttributeValue(object_id, attribute_id) {

/* Get Insight Object Facade from plugin accessor */
Class objectFacadeClass = ComponentAccessor.getPluginAccessor().getClassLoader().findClass("com.riadalabs.jira.plugins.insight.channel.external.api.facade.ObjectFacade");
def objectFacade = ComponentAccessor.getOSGiComponentInstanceOfType(objectFacadeClass);

CustomField myCF = ComponentAccessor.getCustomFieldManager().getCustomFieldObject(object_id) // id of custom field
def objectBean = myCF.getValue(issue)[0]
//return objectBean
return objectFacade.loadObjectAttributeBean(objectBean.getId(), attribute_id).getObjectAttributeValueBeans()[0].getValue() //attribute id

}

if (getInsightAttributeValue(10300,56) != null) {
def Approvers = getInsightAttributeValue(10300,50)
Approvers.each {
println "Item: $it"
}
}

It could be an arbitrary number of objects in the source field (10300). Do you have any idea how to proceed?

Thank you and best regards,

Nico

1 answer

1 accepted

1 vote
Answer accepted
Gezim Shehu [Communardo]
Community Champion
October 25, 2018

Below is a script I use to get attribute value from Insight Object.

Specifically JIRA User type. I get the username out of it and then build a comment mentioning those users. But in your case you could adjust it to add the usernames to a (multi) user picker.

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import org.apache.log4j.Level
log.setLevel(Level.INFO)

Issue issue = issue

def bsField = issue.getCustomFieldValue(ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName('Business Service'))

if(bsField){
    def commentManager = ComponentAccessor.getCommentManager()
    def sdUser = ComponentAccessor.getUserManager().getUserByName('robotuser');
    
    //insight classes & components
    Class objectFacadeClass = ComponentAccessor.getPluginAccessor().getClassLoader().findClass("com.riadalabs.jira.plugins.insight.channel.external.api.facade.ObjectFacade");  
    Class objectTypeAttributeFacadeClass = ComponentAccessor.getPluginAccessor().getClassLoader().findClass("com.riadalabs.jira.plugins.insight.channel.external.api.facade.ObjectTypeAttributeFacade");
    def objectTypeAttributeFacade = ComponentAccessor.getOSGiComponentInstanceOfType(objectTypeAttributeFacadeClass);
    def objectFacade = ComponentAccessor.getOSGiComponentInstanceOfType(objectFacadeClass);

    ArrayList <String> userList = new ArrayList<String>();
    
    for(businessService in bsField){
        //extract insight key ang get object
        def bs=businessService.toString()
        def bsKey = bs.substring(bs.indexOf('(')+1,bs.indexOf(')'))
        def bsObject = objectFacade.loadObjectBean(bsKey)
        //get IT Responsibles for that Business Service
        def itResponsibles = objectFacade.loadObjectAttributeBean(bsObject.getId(), "IT Responsible").getObjectAttributeValueBeans()
           
        if(itResponsibles){
            for(String responsible in itResponsibles){
                responsible = new StringBuilder(responsible).insert(1,"~") //build mention format [~username]
                if(!userList.contains(responsible)){
                    userList.add(responsible)  
                }                     
            }
        }    
     }
    def users = userList.toString()
    def commentUsers = users.substring(1,users.length()-1)
    def message = "Dear All, \n " + "Unassigned Issue with " + issue.getPriority().getName() +" Priority, requires your attention! \n----\n "  + commentUsers
    log.info("SLA Comment Posted!")
    commentManager.create(issue,sdUser,,message,null,10301,true) //10301 is ID of Project Role IT
}
//x
Nico Hoffmann November 6, 2018

Hello Gezim,

thank you for your answer and sorry for the late reply. I tried your suggestion, but I only get the referenced insight object id with your code and no user name I may add to another multi user picker custom field. It looks like this:

Dear All,
Unassigned Issue with Medium Priority, requires your attention!


[~4838121(78390)]

Do you have any idea how to retrieve the "Jira User" Attribute of the referenced object instead?

Best regards,

Nico

Gezim Shehu [Communardo]
Community Champion
November 6, 2018

Hi @Nico Hoffmann,

 

We upgraded our instance and also insight version lately.

Had to do some changes in some script regarding the return format now is

[~4838121(78390)] where it was prior only [~78390].

 

Either way this is still working for me. Can you check again with this script

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import org.apache.log4j.Level
log.setLevel(Level.INFO)

Issue issue = issue

def bsField = issue.getCustomFieldValue(ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName('Business Service'))

if(bsField){
    def commentManager = ComponentAccessor.getCommentManager()
    def sdUser = ComponentAccessor.getUserManager().getUserByName('robotuser');
    
    //insight classes & components
    Class objectFacadeClass = ComponentAccessor.getPluginAccessor().getClassLoader().findClass("com.riadalabs.jira.plugins.insight.channel.external.api.facade.ObjectFacade");  
    Class objectTypeAttributeFacadeClass = ComponentAccessor.getPluginAccessor().getClassLoader().findClass("com.riadalabs.jira.plugins.insight.channel.external.api.facade.ObjectTypeAttributeFacade");
    def objectTypeAttributeFacade = ComponentAccessor.getOSGiComponentInstanceOfType(objectTypeAttributeFacadeClass);
    def objectFacade = ComponentAccessor.getOSGiComponentInstanceOfType(objectFacadeClass);

    ArrayList <String> userList = new ArrayList<String>();
    
    for(businessService in bsField){
        //extract insight key ang get object
        def bs=businessService.toString()
        def bsKey = bs.substring(bs.indexOf('(')+1,bs.indexOf(')'))
        def bsObject = objectFacade.loadObjectBean(bsKey)
        //get IT Responsibles for that Business Service
        def itResponsibles = objectFacade.loadObjectAttributeBean(bsObject.getId(), "IT Responsible").getObjectAttributeValueBeans()
           
        if(itResponsibles){
            for(String responsibleLong in itResponsibles){
                def String responsible = responsibleLong.replaceAll("[\\d\\(\\)]","")
                responsible = new StringBuilder(responsible).insert(1,"~") //build mention format [~username]
                if(!userList.contains(responsible)){
                    userList.add(responsible)  
                }                     
            }
        }    
     }
    def users = userList.toString()
    def commentUsers = users.substring(1,users.length()-1)
    def message = "Dear All, \n " + "Unassigned Issue with " + issue.getPriority().getName() +" Priority, requires your attention! \n----\n "  + commentUsers
    log.info("SLA Comment Posted!")
    commentManager.create(issue,sdUser,,message,null,10301,true) //10301 is ID of Project Role IT
}
//x
Like David Harkins likes this

Suggest an answer

Log in or Sign up to answer