How to copy User Picker fields from Issues to Assets Objects in Jira Data Center

Platform Notice: Data Center - This article applies to Atlassian products on the Data Center platform.

Note that this knowledge base article was created for the Data Center version of the product. Data Center knowledge base articles for non-Data Center-specific features may also work for Server versions of the product, however they have not been tested. Support for Server* products ended on February 15th 2024. If you are running a Server product, you can visit the Atlassian Server end of support announcement to review your migration options.

*Except Fisheye and Crucible

Summary

This article expands on some examples from the Post functions Assets documentation — specifically how to copy User Picker field values from Jira Issues to Assets Objects.

These examples use Groovy Post-functions you can configure on Jira Issue Worfklows.


Environment

Any version of Jira Service Management Data Center.


Solution

On the below examples we use these 3 Ids you should replace when testing on your own instances. Please mind the Ids may change from one instance to another (Staging to Prod) if the data hasn't been recently refreshed.

77777: The Custom Field Id of the User Picker in Jira

88888: The Custom Field Id of the Assets Object field that holds the Asset Object we're updating.

99999: The Attribute Id in Assets that will have the Users copied over from "Field 77777" in Jira


Copying Single User Picker fields to Assets Object

This Groovy Script example works for a Single Picker User Field.

You'll find this code very similar to examples on the Post functions article — the trick is the "newValue.getName()" to pass the User's username instead of the whole object "toString()" value.

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

/* 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);

/* 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);

/* Get the factory that creates Insight Attributes */
Class objectAttributeBeanFactoryClass = ComponentAccessor.getPluginAccessor().getClassLoader().findClass("com.riadalabs.jira.plugins.insight.services.model.factory.ObjectAttributeBeanFactory");
def objectAttributeBeanFactory = ComponentAccessor.getOSGiComponentInstanceOfType(objectAttributeBeanFactoryClass);

/* This is the custom field with the value you want to add to an object attribute */
CustomField jiraCustomField = ComponentAccessor.getCustomFieldManager().getCustomFieldObject(77777);   // SOURCE JIRA FIELD

/* This is the custom field where the object/s you want to set the value */
CustomField insightCustomField = ComponentAccessor.getCustomFieldManager().getCustomFieldObject(88888);   // INSIGHT FIELD WITH OBJECT TO UPDATE
def insightObjects = issue.getCustomFieldValue(insightCustomField);   // "issue" variable is always accessible in post function scripts.

/* This is the priority object type attribute and the one we want to modify */
def objectTypeAttributeBean = objectTypeAttributeFacade.loadObjectTypeAttributeBean(99999); // INSIGHT ATTRIBUTE ID

if (insightObjects != null) {
    insightObjects.each{insightObject ->
        /* Create the new attribute bean based on the value */
        def newValue = issue.getCustomFieldValue(jiraCustomField);
        def newObjectAttributeBean = objectAttributeBeanFactory.createObjectAttributeBeanForObject(insightObject, objectTypeAttributeBean, newValue.getName());
        /* Load the attribute bean */
        def objectAttributeBean = objectFacade.loadObjectAttributeBean(insightObject.getId(), objectTypeAttributeBean.getId());
        if (objectAttributeBean != null) {
           /* If attribute exist reuse the old id for the new attribute */
           newObjectAttributeBean.setId(objectAttributeBean.getId());
        }
        /* Store the object attribute into Insight. */
        try {
            objectAttributeBean = objectFacade.storeObjectAttributeBean(newObjectAttributeBean);
        } catch (Exception vie) {
            log.warn("Could not update object attribute due to validation exception:" + vie.getMessage());
        }
    }
}

return true;


Copying Multiple User Picker fields to Assets Object

This Groovy Script example works for a Multiple Picker User Field.

This is very similar to the above, except we needed an additional "newUsernames" Array object to accumulate the usernames of each value present in the "jiraCustomField 77777".

import com.atlassian.jira.user.ApplicationUser;
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.fields.CustomField;

/* 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);

/* 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);

/* Get the factory that creates Insight Attributes */
Class objectAttributeBeanFactoryClass = ComponentAccessor.getPluginAccessor().getClassLoader().findClass("com.riadalabs.jira.plugins.insight.services.model.factory.ObjectAttributeBeanFactory");
def objectAttributeBeanFactory = ComponentAccessor.getOSGiComponentInstanceOfType(objectAttributeBeanFactoryClass);

/* This is the custom field with the value you want to add to an object attribute */
CustomField jiraCustomField = ComponentAccessor.getCustomFieldManager().getCustomFieldObject(77777);   // SOURCE JIRA FIELD

/* This is the custom field where the object/s you want to set the value */
CustomField insightCustomField = ComponentAccessor.getCustomFieldManager().getCustomFieldObject(88888);   // INSIGHT FIELD WITH OBJECT TO UPDATE
def insightObjects = issue.getCustomFieldValue(insightCustomField);   // "issue" variable is always accessible in post function scripts.

/* This is the priority object type attribute and the one we want to modify */
def objectTypeAttributeBean = objectTypeAttributeFacade.loadObjectTypeAttributeBean(99999); // INSIGHT ATTRIBUTE ID

def newUsernames = new ArrayList < String > ();
issue.getCustomFieldValue(jiraCustomField).each { ApplicationUser userToAdd ->
  newUsernames.add(userToAdd.getUsername().toString());
}

if (insightObjects != null) {
    insightObjects.each{insightObject ->
        /* Create the new attribute bean based on the value */
        def newValue = issue.getCustomFieldValue(jiraCustomField);
        def newObjectAttributeBean = objectAttributeBeanFactory.createObjectAttributeBeanForObject(insightObject, objectTypeAttributeBean, newUsernames.toArray(new String[newUsernames.size()]));
        /* Load the attribute bean */
        def objectAttributeBean = objectFacade.loadObjectAttributeBean(insightObject.getId(), objectTypeAttributeBean.getId());
        if (objectAttributeBean != null) {
           /* If attribute exist reuse the old id for the new attribute */
           newObjectAttributeBean.setId(objectAttributeBean.getId());
        }
        /* Store the object attribute into Insight. */
        try {
            objectAttributeBean = objectFacade.storeObjectAttributeBean(newObjectAttributeBean);
        } catch (Exception vie) {
            log.warn("Could not update object attribute due to validation exception:" + vie.getMessage());
        }
    }
}

return true;



Last modified on Sep 25, 2024

Was this helpful?

Yes
No
Provide feedback about this article
Powered by Confluence and Scroll Viewport.