Update Insight Object using Groovy Post Function

Andrew Downs
Contributor
March 11, 2019

Good day all,

 

I am trying to update a field in Jira Insight based on a text field on an issue.

 

Basically my Insight schema is as follows:

IdNameTypeType Value
563KeyDefaultText
564NameDefaultText
703Reporting NameDefaultText
721Site StatusStatus 
922IP Allocation RegionDefaultText
705LAN IP RangeDefaultText
1987VLAN 1 IDDefaultText
1988VLAN 1 IPDefaultText
1989VLAN 2 IDDefaultText
1990VLAN 2 IPDefaultText
1991VLAN 3 IDDefaultText
1992VLAN 3 IPDefaultText
1993VLAN 4 IDDefaultText
1994VLAN 4 IPDefaultText
1995VLAN 5 IDDefaultText
1996VLAN 5 IPDefaultText
1997VLAN 6 IDDefaultText
1998VLAN 6 IPDefaultText
1999VLAN 7 IDDefaultText
2000VLAN 7 IPDefaultText
2001VLAN 8 IDDefaultText
2002VLAN 8 IPDefaultText
706Line TypeDefaultText
707Line SpeedDefaultText
722Not Required in Active DirectoryDefaultBoolean
709Circuit ReferenceDefaultText
710Primary Site ContactUser 
711Primary Contact NumberDefaultText
712Secondary Site ContactUser 
713Secondary Contact NumberDefaultText
714Customer Service ManagerUser 
715Street AddressDefaultText
716SuburbDefaultText
717CityDefaultText
718ProvinceDefaultText
719CountryDefaultText
720GPS Co-ordinatesDefaultText

 

There is a field called Site Name on the issue that I would like to use to update the Reporting Name field in the Insight Object Schema.

 

I am trying to use code referenced here:

https://documentation.riada.io/display/ICV51/Update+object+attribute

 

 

But seem to be hitting an issue. My code I have so far is as follows:

 

// Get Custom Field
siteNameField = customFieldManager.getCustomFieldObjectByName("Site Name")
siteNameFieldValue = issue.getCustomFieldValue(siteNameField)

//return siteNameFieldValue




/* 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().getCustomFieldObjectByName("Site Name"); // Change 12345 to the correct value

//return jiraCustomField
/* This is the custom field where the object/s you want to set the value */
CustomField insightCustomField = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Site Name"); // Change 23456 to the correct value
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(703);

if (insightObjects != null) {
insightObjects.each{insightObject ->
/* Create the new attribute bean based on the value */
//def newValue = issue.getCustomFieldValue(insightCustomField);

def newObjectAttributeBean = objectAttributeBeanFactory.createObjectAttributeBeanForObject(insightObject, objectTypeAttributeBean, jiraCustomField[0].getName());
// def newObjectAttributeBean = objectAttributeBeanFactory.createObjectAttributeBeanForObject(insightObject, objectTypeAttributeBean, jiraCustomField[0].getName());

// return newObjectAttributeBean

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



/* Done! :) */
return true;

 

I get the jiraCustomField value as Site Name, and the insightObjects returns the actual text value as an example: Test Site

 

The problem I am running into is at this point:

 

 /* Create the new attribute bean based on the value */
def newValue = issue.getCustomFieldValue(insightCustomField);

def newObjectAttributeBean = objectAttributeBeanFactory.createObjectAttributeBeanForObject(insightObject, objectTypeAttributeBean, newValue[0].getName());

 

I keep getting the below error:

 

No signature of method: java.lang.String.getName() is applicable for argument types: () values: [] Possible solutions: getAt(java.util.Collection), getAt(groovy.lang.Range), getAt(groovy.lang.IntRange), getAt(int), getAt(java.lang.String), getAt(groovy.lang.Range)groovy.lang.MissingMethodException: No signature of method: java.lang.String.getName() is applicable for argument types: () values: [] Possible solutions: getAt(java.util.Collection), getAt(groovy.lang.Range), getAt(groovy.lang.IntRange), getAt(int), getAt(java.lang.String), getAt(groovy.lang.Range) at Script304$_run_closure1.doCall(Script304.groovy:66) at Script304.run(Script304.groovy:62)

 

I am sure there is something small. I am also not 100% sure if this is necessary or if I can use the default post functions for Insight to achieve this. Documentation is, to say the list a little bit scarce on actual examples.

2 answers

2 accepted

1 vote
Answer accepted
Yinon Negev
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
March 19, 2019

Hi Andrew.

     Just FYI - you can achive the same using the Insight Postfunction: Set the value of an object attribute with a predefined value; where if the Object is selected in an Insight CF, the Attribute "Reporting Name" will be populated with the Jira CF, noted as a Placeholder: ${Site Name}

Kind regards,

Yinon

Team Riada

Andrew Downs
Contributor
March 19, 2019

Hi Yinon,

 

That only seems to update a field on the form, not the other way around.

 

What I am wanting to do is update the actual insight object.

Lara Lopez
Contributor
May 30, 2022

Thanks Yinon! This is just what I needed and it is not clear in the documentation!

Like Antoine Berry likes this
0 votes
Answer accepted
Antoine Berry
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
March 11, 2019

Hi,

I am not sure to understand what you want to achieve. I guess your custom field "Site Name" is a Text Field ? And you want to copy its value into the attribute "Reporting Name" ? But of which object ?

In your code variables jiraCustomField and insightCustomField are the same so it is confusing.

Andrew Downs March 13, 2019

Hi Antoine,

 

Thank you the for the response. 

 

So Site Name is a free text field on the Jira issue, Reporting Name is a field in Jira Insight, what I am wanting to do is update the Reporting Name within Jira Insight with the value on Site Name. 

 

Reporting Name is not a field only any form, so I would need to firstly pick up the Insight Object and then update the Reporting Name property. 

 

I also have a insight field called Site Name which is a link to the Sites within Insight. Hope that makes a bit more sense. 

 

Will try do more of an example when back at the office. 

Antoine Berry
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
March 14, 2019

Hi Andrew,

I am not sure I fully understood, but if I got it correctly : 

On your form, you fill "Site name" text custom field and "Site Name" insight custom field. You want to update the insight object you selected in the second field with the value you filled the first one with. I would advise not having two custom fields with the same name by the way. 

If I got it right, this code should work (I just tried it), just replace the IDs : 

import com.atlassian.jira.component.ComponentAccessor

def customFieldManager = ComponentAccessor.getCustomFieldManager()

//Initialize insight classes
Class objectFacadeClass = ComponentAccessor.getPluginAccessor().getClassLoader().findClass("com.riadalabs.jira.plugins.insight.channel.external.api.facade.ObjectFacade");
def objectFacade = ComponentAccessor.getOSGiComponentInstanceOfType(objectFacadeClass);

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

//Jira Site Name Text Custom field
int jiraSiteCfId = 12700
def cfJiraSite = customFieldManager.getCustomFieldObject(jiraSiteCfId)
def cfJiraSiteValue = issue.getCustomFieldValue(cfJiraSite)

//Jira Site Name Insight Custom field
int jiraSiteInsightCfId = 12406
def cfJiraSiteInsight = customFieldManager.getCustomFieldObject(jiraSiteInsightCfId)
def cfJiraSiteInsightValue = issue.getCustomFieldValue(cfJiraSiteInsight)

if (cfJiraSiteInsightValue != null && cfJiraSiteInsightValue.size() > 0){
//ID of the attribute in insight
int reportingNameId = 5

def insightObject = cfJiraSiteInsightValue[0]
def reportingNameTypeAttributeBean = objectTypeAttributeFacade.loadObjectTypeAttributeBean(reportingNameId);

def reportingNameAttributeBean = objectAttributeBeanFactory.createObjectAttributeBeanForObject(insightObject, reportingNameTypeAttributeBean, cfJiraSiteValue);
def insightObjectAttributeBean = objectFacade.loadObjectAttributeBean(insightObject.getId(), reportingNameTypeAttributeBean.getId());
if (insightObjectAttributeBean != null) {
/* If attribute exist reuse the old id for the new attribute */
reportingNameAttributeBean.setId(insightObjectAttributeBean.getId());
}

/* Store the object attribute into Insight. */
try {
insightObjectAttributeBean = objectFacade.storeObjectAttributeBean(reportingNameAttributeBean);
} catch (Exception vie) {
log.warn("Could not update object attribute due to validation exception:" + vie.getMessage());
}
}

This is assuming you only selected one insight object in the custom field. If you selected multiple objects, you a loop.

Let me know if that helped

Antoine

Like Vitali likes this
Andrew Downs
Contributor
March 19, 2019

Hi Antoine,

 

Thank you. Sorry things have been a bit hectic at the office. Will let you know.

Andrew Downs
Contributor
March 19, 2019

I am now getting the below error:

 

No signature of method: com.riadalabs.jira.plugins.insight.services.model.factory.ObjectAttributeBeanFactoryImpl.createObjectAttributeBeanForObject() is applicable for argument types: (java.lang.String, com.riadalabs.jira.plugins.insight.services.model.ObjectTypeAttributeBean, java.lang.String) values: [S, ObjectTypeAttributeBean [id=703, name=Reporting Name, type=DEFAULT], ...] Possible solutions: createObjectAttributeBeanForObject(com.riadalabs.jira.plugins.insight.services.model.ObjectBean, com.riadalabs.jira.plugins.insight.services.model.ObjectTypeAttributeBean, [Ljava.lang.String;), createObjectAttributeBeanForObject(com.riadalabs.jira.plugins.insight.services.model.ObjectBean, com.riadalabs.jira.plugins.insight.services.model.ObjectTypeAttributeBean, java.text.DateFormat, java.text.DateFormat, [Ljava.lang.String;)

I have tried importing the following:

 

import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.fields.CustomField
import com.onresolve.scriptrunner.runner.customisers.WithPlugin
@WithPlugin("com.riadalabs.jira.plugins.insight")

 

Not sure what I would be missing, but I am sure it is something small.

Antoine Berry
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
March 20, 2019

Hi Andrew, no worries. 

It seems that your "insightObject" is of type String, when it should be of type ObjectBean. This object should be the insight object that is selected in your form and you want to update.

Can you check you are using the right ID in the code ? Maybe provide a screenshot of the form to make sure I understood correctly. 

You can use some logs in the code for more information as well.

Antoine

Andrew Downs
Contributor
March 20, 2019

Hi Antoine,

 

I have reverted to the original code, I was trying to find the object by name as opposed to the ID.

 

The script is working as expected. Thank you so much for the assistance.

Like Antoine Berry likes this

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events