create nested object by script post function Edited

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

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

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

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;

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...

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

thanks, my misstake :)

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...

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
Community showcase
Posted Oct 11, 2018 in Marketplace Apps

You + one app + a desert island...

Hi all! My name is Miles and I work on the Marketplace team. We’re looking for better ways to recommend and suggest apps that are truly crowd favorites, so of course we wanted to poll the Community. ...

3,662 views 6 6
Join discussion

Atlassian User Groups

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

Find a group

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

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you