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:
Id | Name | Type | Type Value |
563 | Key | Default | Text |
564 | Name | Default | Text |
703 | Reporting Name | Default | Text |
721 | Site Status | Status | |
922 | IP Allocation Region | Default | Text |
705 | LAN IP Range | Default | Text |
1987 | VLAN 1 ID | Default | Text |
1988 | VLAN 1 IP | Default | Text |
1989 | VLAN 2 ID | Default | Text |
1990 | VLAN 2 IP | Default | Text |
1991 | VLAN 3 ID | Default | Text |
1992 | VLAN 3 IP | Default | Text |
1993 | VLAN 4 ID | Default | Text |
1994 | VLAN 4 IP | Default | Text |
1995 | VLAN 5 ID | Default | Text |
1996 | VLAN 5 IP | Default | Text |
1997 | VLAN 6 ID | Default | Text |
1998 | VLAN 6 IP | Default | Text |
1999 | VLAN 7 ID | Default | Text |
2000 | VLAN 7 IP | Default | Text |
2001 | VLAN 8 ID | Default | Text |
2002 | VLAN 8 IP | Default | Text |
706 | Line Type | Default | Text |
707 | Line Speed | Default | Text |
722 | Not Required in Active Directory | Default | Boolean |
709 | Circuit Reference | Default | Text |
710 | Primary Site Contact | User | |
711 | Primary Contact Number | Default | Text |
712 | Secondary Site Contact | User | |
713 | Secondary Contact Number | Default | Text |
714 | Customer Service Manager | User | |
715 | Street Address | Default | Text |
716 | Suburb | Default | Text |
717 | City | Default | Text |
718 | Province | Default | Text |
719 | Country | Default | Text |
720 | GPS Co-ordinates | Default | Text |
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.
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
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thanks Yinon! This is just what I needed and it is not clear in the documentation!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Antoine,
Thank you. Sorry things have been a bit hectic at the office. Will let you know.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.