It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

create nested object by script post function Edited

meysam Sep 12, 2017

Hi

i need insight script for create object to my object scheme as following attachment file.

i'm very tired  reading this document :(

https://documentation.riada.se/display/ICV50/Insight+Post+Functions

how can i fix this?

screenshot.jpg

screenshot.jpg

3 answers

0 votes
Ola Melin Sep 20, 2017 • edited

Hi, 

You can use this Groovy script as a template and see how it can be done. 

It only contains 2 custom fields but this will show you the principle.

 

import com.atlassian.jira.component.ComponentAccessor;
import java.util.ArrayList;
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 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);

Class objectAttributeBeanFactoryClass = ComponentAccessor.getPluginAccessor().getClassLoader().findClass("com.riadalabs.jira.plugins.insight.services.model.factory.ObjectAttributeBeanFactory");
def objectAttributeBeanFactory = ComponentAccessor.getOSGiComponentInstanceOfType(objectAttributeBeanFactoryClass);

CustomField jiraCustomField = ComponentAccessor.getCustomFieldManager().getCustomFieldObject(10201);
CustomField modelNameCF = ComponentAccessor.getCustomFieldManager().getCustomFieldObject(10200);

/* The object schema to use */
def objectSchemaId = 61;
def cpuType;
/* get value from cascading field */
def cpuTypeValue = jiraCustomField.getValue(issue);
if (cpuTypeValue != null) {

/* Find the object type based on input value */
for (objectType in objectTypeFacade.findObjectTypeBeansFlat(objectSchemaId)) {

if (objectType.name.equalsIgnoreCase(cpuTypeValue.values()[1].toString())) {
cpuType = objectType;
break;
}
}
/* Create a new unsaved object bean */
def newObjectBean = cpuType.createMutableObjectBean();
def objectAttributeBeans = new ArrayList();
/* Load the attribute from the cpu object type */
def nameObjectTypeAttributeBean = objectTypeAttributeFacade.loadObjectTypeAttributeBean(cpuType.id, "Name");
objectAttributeBeans.add(objectAttributeBeanFactory.createObjectAttributeBeanForObject(newObjectBean, nameObjectTypeAttributeBean, modelNameCF.getValue(issue)));
/* Set all object attributes to the object */
newObjectBean.setObjectAttributeBeans(objectAttributeBeans);
newObjectBean = objectFacade.storeObjectBean(newObjectBean);

log.warn("Created object bean " + newObjectBean.objectKey);
}



/* Done! :) */
return true;

 

Cheers

0 votes
Ola Melin Sep 20, 2017 • edited Nov 02, 2017

Removed double post

0 votes
Alexander Sundström Sep 22, 2017

Hi Meysam,

Perhaps this Groovy Script example for creating an object based on custom fields can be helpful. It's based in Insight 5, https://documentation.riada.se/display/ICV50/Create+object

There is more examples available that can be used as an inspiration at https://documentation.riada.se/display/ICV50/Groovy+script+examples

Also, We have some Partners at Riada (Developer of Insight), that can help you with your configuration if needed and creating such scripts. Let us know if that is the case.

Best Regards
Alexander

Ola Melin Sep 22, 2017

Here is an template that can be used to see the principle of the 

import com.atlassian.jira.component.ComponentAccessor;
import java.util.ArrayList;
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 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);

Class objectAttributeBeanFactoryClass = ComponentAccessor.getPluginAccessor().getClassLoader().findClass("com.riadalabs.jira.plugins.insight.services.model.factory.ObjectAttributeBeanFactory");
def objectAttributeBeanFactory = ComponentAccessor.getOSGiComponentInstanceOfType(objectAttributeBeanFactoryClass);

CustomField jiraCustomField = ComponentAccessor.getCustomFieldManager().getCustomFieldObject(10201);
CustomField modelNameCF = ComponentAccessor.getCustomFieldManager().getCustomFieldObject(10200);

/* The object schema to use */
def objectSchemaId = 61;
def cpuType;
/* get value from cascading field */
def cpuTypeValue = jiraCustomField.getValue(issue);
if (cpuTypeValue != null) {

/* Find the object type based on input value */
for (objectType in objectTypeFacade.findObjectTypeBeansFlat(objectSchemaId)) {

if (objectType.name.equalsIgnoreCase(cpuTypeValue.values()[1].toString())) {
cpuType = objectType;
break;
}
}
/* Create a new unsaved object bean */
def newObjectBean = cpuType.createMutableObjectBean();
def objectAttributeBeans = new ArrayList();
/* Load the attribute from the cpu object type */
def nameObjectTypeAttributeBean = objectTypeAttributeFacade.loadObjectTypeAttributeBean(cpuType.id, "Name");
objectAttributeBeans.add(objectAttributeBeanFactory.createObjectAttributeBeanForObject(newObjectBean, nameObjectTypeAttributeBean, modelNameCF.getValue(issue)));
/* Set all object attributes to the object */
newObjectBean.setObjectAttributeBeans(objectAttributeBeans);
newObjectBean = objectFacade.storeObjectBean(newObjectBean);

log.warn("Created object bean " + newObjectBean.objectKey);
}



/* Done! :) */
return true;
Filip Håkansson Community Leader Nov 02, 2017 • edited

Does "com.riadalabs.jira.plugins.insight.services.model.factory.ObjectAttributeBeanFactory" still exist?


Class objectAttributeBeanFactoryClass = ComponentAccessor.getPluginAccessor().getClassLoader().findClass("com.riadalabs.jira.plugins.insight.services.model.factory.ObjectAttributeBeanFactory");
def objectAttributeBeanFactory = ComponentAccessor.getOSGiComponentInstanceOfType(objectAttributeBeanFactoryClass); 

 It gives  ClassNotFoundException...

Ola Melin Nov 02, 2017

Yes it should be available in all versions of Insight > 5.0

Filip Håkansson Community Leader Nov 02, 2017

thanks, my misstake :)

Volker Albrich Jan 06, 2018

Hi, where do I get the correct values for 10201 and 10200??

CustomField jiraCustomField = ComponentAccessor.getCustomFieldManager().getCustomFieldObject(10201);
CustomField modelNameCF = ComponentAccessor.getCustomFieldManager().getCustomFieldObject(10200);

 Thanks in advance and sorry for the dumb question, but I'm completely new...

Volker Albrich Jan 06, 2018

I get error:

class com.riadalabs.jira.plugins.insight.common.exception.GroovyInsightException

GroovyInsightException: No signature of method: java.util.ArrayList.values() is applicable for argument types: () values: [] Possible solutions: plus(java.util.Collection), plus(java.lang.Object), plus(java.lang.Object), plus(java.lang.Object), plus(java.util.Collection), plus(java.lang.Iterable)'

 

What am I doing wrong?

Suggest an answer

Log in or Sign up to answer
This widget could not be displayed.
This widget could not be displayed.
Community showcase
Published in Marketplace Apps & Integrations

Join our webinar: “Asset Management: The Role of Context in ITSM” with Atlassian & Riada

  Hi Atlassian Community! This is Teresa from the Jira Service Desk team. On June 19th, I’ll be hosting a webinar “Asset Management: The Role of Context in ITSM” with Atlassian Partne...

125 views 1 3
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you