Page tree
Skip to end of metadata
Go to start of metadata

This is an example how you can create an object with values from fields in an JIRA Issue. This is typically executed in a transition somewhere in your workflow. The object we will create is of type "Customer" and we will create an object where the name is issue summary, the object priority is the issue priority and the object description is the issue description.

This is how the object type is setup:

 

This is how this could look like:

import com.atlassian.jira.component.ComponentAccessor;
import java.util.ArrayList;
 
/* 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 Type Facade from plugin accessor */
Class objectTypeFacadeClass = ComponentAccessor.getPluginAccessor().getClassLoader().findClass("com.riadalabs.jira.plugins.insight.channel.external.api.facade.ObjectTypeFacade");   
def objectTypeFacade = ComponentAccessor.getOSGiComponentInstanceOfType(objectTypeFacadeClass);

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

/* The ID of the object type "Customer" is 1 in this example. The easiest way of finding the ID of an object type is to click at the object type in the object type tree and open up the web browser console to get the id that Insight uses to fetch objects. This will be easier to do in upcoming releases. */
def objectTypeCustomer = objectTypeFacade.loadObjectTypeBean(1);
 
/* Create a new unsaved object bean */
def newObjectBean = objectTypeCustomer.createObjectBean();
 
/* Set up the attribute list */
def objectAttributeBeans = new ArrayList();
 
/* Set the name of the customer */
def nameObjectTypeAttributeBean = objectTypeAttributeFacade.loadObjectTypeAttributeBean(1);   // 1 is the ID of the object type attribute "Name"
def nameObjectAttributeBean = newObjectBean.createObjectAttributeBean(nameObjectTypeAttributeBean); // This is the name object attribute for the new object
def nameObjectAttributeValueBean = nameObjectAttributeBean.createObjectAttributeValueBean(); // This is the actual value of the attribute
nameObjectAttributeValueBean.setTextValueShort(issue.getSummary()); 									// Set the name


def values = nameObjectAttributeBean.getObjectAttributeValueBeans();							
values.add(nameObjectAttributeValueBean); 													// Add the value to the object attribute
nameObjectAttributeBean.setObjectAttributeValueBeans(values);									
 
objectAttributeBeans.add(nameObjectAttributeBean);										   // Add to the list of object attributes
 
/* Set the priority of the customer */
if (issue.getPriorityObject() != null) {
 	def priorityObjectTypeAttributeBean = objectTypeAttributeFacade.loadObjectTypeAttributeBean(2);   // 2 is the ID of the object type attribute "Priority"
	def priorityObjectAttributeBean = newObjectBean.createObjectAttributeBean(priorityObjectTypeAttributeBean); // This is the object attribute for the new object
	def priorityObjectAttributeValueBean = priorityObjectAttributeBean.createObjectAttributeValueBean(); // This is the actual value of the attribute 
	priorityObjectAttributeValueBean.setTextValueShort(issue.getPriorityObject().getName()); 				// Set the priority

	values = priorityObjectAttributeBean.getObjectAttributeValueBeans();
	values.add(priorityObjectAttributeValueBean); 														// Add the value to the object attribute
	priorityObjectAttributeBean.setObjectAttributeValueBeans(values);									

	objectAttributeBeans.add(priorityObjectAttributeBean);											// Add to the list of object attributes
}

 /* Set the description of the customer */
if (issue.getDescription() != null && !issue.getDescription().equals("")) {
 	def descriptionObjectTypeAttributeBean = objectTypeAttributeFacade.loadObjectTypeAttributeBean(3);   // 3 is the ID of the object type attribute "Priority"
	def descriptionObjectAttributeBean = newObjectBean.createObjectAttributeBean(descriptionObjectTypeAttributeBean); // This is the object attribute for the new object
	def descriptionObjectAttributeValueBean = descriptionObjectAttributeBean.createObjectAttributeValueBean(); // This is the actual value of the attribute 
	descriptionObjectAttributeValueBean.setTextShortValue(issue.getDescription()); 								// Set the description

	values = descriptionObjectAttributeBean.getObjectAttributeValueBeans();
	values.add(descriptionObjectAttributeValueBean); 														// Add the value to the object attribute
	descriptionObjectAttributeBean.setObjectAttributeValueBeans(values);									

	objectAttributeBeans.add(descriptionObjectAttributeBean);											// Add to the list of object attributes
}

 /* Set all object attributes to the object */
newObjectBean.setObjectAttributeBeans(objectAttributeBeans);
 
/* Store the object into Insight. The new ObjectBean will be updated with an unique ID */
try {
	newObjectBean = objectFacade.storeObjectBean(newObjectBean);
    log.warn("newObjectBean: " + newObjectBean);
} catch (Exception vie) {
	log.warn("Could not create issue due to validation exception:" + vie.getMessage());
}

/* Done! :) */
return true;
 
  • No labels