Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

How to synchronize Insight object attribute with Jira Custom Field

Boyan Angelov _Nemetschek Bulgaria_
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.
June 23, 2022

Hi All,

Using Jira Service Management Data Center here. I am struggling to find a way to synchronize Insight object attribute(s) with Jira Custom Fields in connected tickets. Here is the case:

- I have Insight Object Schema with 1 Object type with several attributes (for example Phone).

- For each object I have a ticket in Jira (created automatically when the object is created by Insight automation).

- For several of the attributes in the object (for example Phone) I have matching custom fields in the Jira ticket which are copied over when the ticket is created.

Now I need to be able to update the Jira custom field in the connected ticket when someone changes the value of an attribute in the object. I was thinking Insight automation can help, as it can trigger on object update, but I cannot figure out how this can happen. The Jira Automation (for Data Center) does not seem to have any trigger for Insight object updates, so it does not look like useful option too.

Has anyone made such update working before?

Thanks,

Boyan

2 answers

0 votes
Boyan Angelov _Nemetschek Bulgaria_
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.
October 3, 2024

Edited

0 votes
Boyan Angelov _Nemetschek Bulgaria_
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 31, 2023

OK, almost a year later let me answer to myself. The answer to my question is in the Assets Automation rules, where you can setup a rule to run when an asset is updated and run a Groovy script. In the Groovy script you can describe which attributes you want to copy over to the connected ticket and do it, but pay attention to the type of the attribute/custom field, as the data might need some massaging before you can set the issue field correctly, for example if you want to copy a Date field.

Christian Frazee
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
October 2, 2024

Boyan,

Would you be able to share the groovy script you used to accomplish this task? I would like to create an automation that updates Jira issue Date fields based off of the Date attribute within an object tied that Jira issue in an Assets Object Field.

Boyan Angelov _Nemetschek Bulgaria_
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.
October 3, 2024

This is the script:

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.event.type.EventDispatchOption
def objectFacade = ComponentAccessor.getOSGiComponentInstanceOfType(ComponentAccessor.getPluginAccessor().getClassLoader().findClass("com.riadalabs.jira.plugins.insight.channel.external.api.facade.ObjectFacade"));
def objectTypeAttributeFacade = ComponentAccessor.getOSGiComponentInstanceOfType(ComponentAccessor.getPluginAccessor().getClassLoader().findClass("com.riadalabs.jira.plugins.insight.channel.external.api.facade.ObjectTypeAttributeFacade"));
def objectAttributeBeanFactory = ComponentAccessor.getOSGiComponentInstanceOfType(ComponentAccessor.getPluginAccessor().getClassLoader().findClass("com.riadalabs.jira.plugins.insight.services.model.factory.ObjectAttributeBeanFactory"));
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def issueManager = ComponentAccessor.getIssueManager()
def userManager = ComponentAccessor.getUserManager()
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def optionsManager = ComponentAccessor.getOptionsManager()
def CF_DATE_TIME_PATTERN = "E MMM d HH:mm:ss zzz yyyy"
def CF_DATE_TIME_PATTERN_SIMPLE = "yyyy-MM-dd HH:mm:ss.SSS"
def DATE_TIME_SCHEMA_TYPE = "com.atlassian.jira.plugin.system.customfieldtypes:datetime";
def DATE_SCHEMA_TYPE = "com.atlassian.jira.plugin.system.customfieldtypes:datepicker";
def USER_TYPE = "com.atlassian.jira.plugin.system.customfieldtypes:userpicker";
def SINGLE_OPTION_LIST = "com.atlassian.jira.plugin.system.customfieldtypes:select";
//[object attribute name : custom field name, ...]
def CF_MAP = ["Init Date":"Init Date", "Warranty start": "Warranty Start", "Warranty end": "Warranty End"];
log.warn("========================================");
log.warn("object:" + object.toString());
if (object != null) {
def connectedTickets = objectFacade.findObjectTicketConnections(object.id.toInteger());
def firstConnectedTicketId = connectedTickets[0].ticketId;
def issue = issueManager.getIssueObject(firstConnectedTicketId);
log.warn("first connected issue:" + issue.key);
//iterate each attribute and set its value to a related custom field
def attributes = object.getObjectAttributeBeans();
attributes.each { insightObject ->
def values = insightObject.getObjectAttributeValueBeans();
def objectTypeAttributeId = insightObject.objectTypeAttributeId;
def objectTypeAttributeBean = objectTypeAttributeFacade.loadObjectTypeAttributeBean(objectTypeAttributeId).createMutable();
log.warn('attribute object = ' + insightObject.toString());
log.warn('values = ' + values.value); //array
log.warn('bean name = ' + objectTypeAttributeBean.name.toString()); //array
if(CF_MAP[objectTypeAttributeBean.name.toString()]){
def cf = customFieldManager.getCustomFieldObjectByName(CF_MAP[objectTypeAttributeBean.name.toString()]);
def resultValue;
def userByKey;
def userById;
def usedIdLong;
def userIdString;
log.warn('TO SET = ' + values.value[0]); //array
log.warn('type of CF is = ' + cf.getCustomFieldType().getKey());
if(cf.getCustomFieldType().getKey().equals(DATE_TIME_SCHEMA_TYPE) || cf.getCustomFieldType().getKey().equals(DATE_SCHEMA_TYPE)){
def dateString = values.value[0].toString();
if (dateString.substring(0, 1).isNumber()) {
log.warn('date starts with a number');
def parsedDate = Date.parse(CF_DATE_TIME_PATTERN_SIMPLE, dateString);
resultValue = parsedDate.toTimestamp();
} else {
log.warn('date starts with a letter');
def parsedDate = Date.parse(CF_DATE_TIME_PATTERN, dateString);
resultValue = parsedDate.toTimestamp();
}
} else if (cf.getCustomFieldType().getKey().equals(USER_TYPE)){
resultValue = userManager.getUserByKey(values.value[0].toString());
} else if (cf.getCustomFieldType().getKey().equals(SINGLE_OPTION_LIST)){
resultValue = optionsManager.findByOptionValue(values.value[0].toString()).get(0);
} else {
resultValue = values.value[0].toString();
}
log.warn('resultValue before setting = ' + resultValue);
issue.setCustomFieldValue(cf, resultValue);
issueManager.updateIssue(user, issue, EventDispatchOption.DO_NOT_DISPATCH, false)
}
}
}
log.warn("========================================");
You can see there is mapping between object attribute and custom field names, so that it knows what to to transfer where.
Like Christian Frazee likes this
Christian Frazee
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
October 10, 2024

Thank you, Boyan! That worked like a charm. The only issue I ran into was when selecting the date attribute within the object itself it appears that it updates the date field inside the Jira issue to the date prior to the one selected.

 

I thought that this could have been due to the date time format

 

def CF_DATE_TIME_PATTERN = "E MMM d HH:mm:ss zzz yyyy"

def CF_DATE_TIME_PATTERN_SIMPLE = "yyyy-MM-dd HH:mm:ss.SSS"

Whereas the date format for the attribute and field dates is dd/MMM/yy (10/Oct/24)

 

Oddly enough the new and old dates in the assets Activity --> History changelog shows the date prior to the one selected as well... Not sure what is going on there.

 Will have to look into that further
Boyan Angelov _Nemetschek Bulgaria_
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.
October 10, 2024

Good to hear that worked. For some reason I do not have this problem with the dates, maybe it is the date format...

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events