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 vote

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
Atlassian Community Anniversary

Happy Anniversary, Atlassian Community!

This community is celebrating its one-year anniversary and Atlassian co-founder Mike Cannon-Brookes has all the feels.

Read more
Community showcase
Bridget Sauer
Published yesterday in Marketplace Apps

Calling all developers––You're invited to Atlas Camp 2018

 Atlas Camp   is our developer event which will take place in Barcelona, Spain  from the 6th -7th of   September . This is a great opportunity to meet other developers and get n...

72 views 0 5
Read article

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