Insight customField update in Scriptrunner

Damian Wodzinski
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.
August 19, 2020

Hi, I am not able to update Insight CustomField in Jira using Script listener

 

On their site, you can find this link:
https://documentation.mindville.com/display/INSSERV/Update+Insight+Custom+Field

When I am running it in console, everything works, but when I try to use the same code as the Listener, there is problem with this part:

def objects = iqlFacade.findObjectsByIQLAndSchema(1, "Name = \"" + workstationName + "\"");

 

When working as Listener, above code return 0 results. The same IQL works in Jira and in console

3 answers

1 vote
Damian Wodzinski
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.
August 20, 2020

Hi, thanks for suggestion. I will try to change user to a person who do have access. Meantime I am pasting my code (this is a bit messy). 

In console, instead of trying to use captured issue upon "New issue creation event" I am just using some example issue.

import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.MutableIssue;
import com.atlassian.jira.event.type.EventDispatchOption;
import com.atlassian.jira.issue.fields.CustomField;;
import java.util.regex.*;
import com.riadalabs.jira.plugins.insight.channel.external.api.facade.*;
import com.atlassian.jira.issue.util.*;
import com.atlassian.jira.issue.ModifiedValue;
import com.riadalabs.jira.plugins.insight.channel.external.api.facade.IQLFacade.*;
import com.atlassian.jira.bc.issue.*;
import com.atlassian.jira.issue.*;
import com.atlassian.jira.component.*;
import com.atlassian.jira.issue.IssueImpl.*;
import com.atlassian.jira.event.type.EventDispatchOption;

def issueManager = ComponentAccessor.getIssueManager();
Issue issue = issue;
if(issue.getIssueTypeId() == '10600'){
log.error(issue.getIssueTypeId())
String Description = issue.getDescription().toString();
String description;
String workstation;
def Label = issueManager.getIssueObject("IT-390").getLabels();
log.error(Label)
MutableIssue iss = issue;
iss.setLabels(Label)
Pattern pattern = Pattern.compile("\\w{3}\\d{5}\\w");
Matcher matcher = pattern.matcher(Description);
while (matcher.find()){
workstation = matcher.group(0);
}
pattern = Pattern.compile("What happened: (.*)");
matcher = pattern.matcher(Description);
while (matcher.find()){
description = matcher.group(1);
}
log.error(workstation)
def changeHolder = new DefaultIssueChangeHolder()
def customFieldManager = ComponentAccessor.getCustomFieldManager();
def issueDescription = customFieldManager.getCustomFieldObject("customfield_11101");
issueDescription.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(issueDescription), description), changeHolder);
def pc = customFieldManager.getCustomFieldObject("customfield_11100");
Class iqlFacadeClass = ComponentAccessor.getPluginAccessor().getClassLoader().findClass("com.riadalabs.jira.plugins.insight.channel.external.api.facade.IQLFacade");
def iqlFacade = ComponentAccessor.getOSGiComponentInstanceOfType(iqlFacadeClass);
String iql = "Name = \"" + workstation + "\""
log.error(iql)
try{
iqlFacade.validateIQL(iql)
def objects = iqlFacade.findObjectsByIQLAndSchema(3, "Name = \"" + workstation + "\"");
log.error(objects)
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser();
if (objects) {
MutableIssue mi = (MutableIssue) issue;
mi.setCustomFieldValue(pc, objects);
ComponentAccessor.getIssueManager().updateIssue(user, mi, EventDispatchOption.DO_NOT_DISPATCH, false);
} else {
log.error("There is no such an object")
}
} catch (Exception e){
log.error("Wrong iql")
}
}

Matcher is catching the name of the workstation from Description, and I can confirm it is working correctly. After I run IQL search, objects variable is empty.

 

Example log than I have from code above:
 

2020-08-19 09:32:46,828 ERROR [runner.ScriptBindingsManager]: 10600
2020-08-19 09:32:46,844 ERROR [runner.ScriptBindingsManager]: [Sophos]
2020-08-19 09:32:46,857 ERROR [runner.ScriptBindingsManager]: PPL20032L
2020-08-19 09:32:46,875 ERROR [runner.ScriptBindingsManager]: Name = "PPL20032L"
2020-08-19 09:32:46,954 ERROR [runner.ScriptBindingsManager]: []
2020-08-19 09:32:46,954 ERROR [runner.ScriptBindingsManager]: There is no such an object
Damian Wodzinski
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.
August 20, 2020

So, user is catched after iql method, so that didn't help. I cannot perform update on empty object.

Hana Kučerová
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
August 20, 2020

Hi Damian,

the problem is definitely in the search part. I've tested the code below ("validate IQL and find object with name "Test" in object schema 1 in Insight").

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.IssueImpl.*
import com.riadalabs.jira.plugins.insight.channel.external.api.facade.IQLFacade.*

Class iqlFacadeClass = ComponentAccessor.getPluginAccessor().getClassLoader().findClass("com.riadalabs.jira.plugins.insight.channel.external.api.facade.IQLFacade")
def iqlFacade = ComponentAccessor.getOSGiComponentInstanceOfType(iqlFacadeClass)

final String objectName = "Test"
final int objectSchemaId = 1

String iql = "Name = \"" + objectName + "\""
log.error(iql)
try {
iqlFacade.validateIQL(iql)
def objects = iqlFacade.findObjectsByIQLAndSchema(objectSchemaId, iql)
if (objects) {
log.error(objects)
} else {
log.error("There is no such an object")
}
} catch (Exception e){
log.error("Wrong iql")
}

It works for me in console and also as a listener code.

I'm not able to find the objects, when I limit the access to Insight scheme for the user, who creates the issue (then the Issue created event is fired and the listener code is executed).

In your code, the user object is used for the issue update operation, but this is not important. I think the problem is with the user, who performs the search in Insight - I believe the user, who creates the issue, is the one who is used for the search operation. When you use this code in console, search is performed by your user, so it works.

Is it possible explanation? Who does create the issues in your project?

Damian Wodzinski
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.
August 25, 2020

Yeah I understand what you suggest. I have mail handler to create tickets, I added the reporter to the Insight but it does not work. I am not sure if reporter is the one who is creating it, or maybe there is some generic user that performs those actions.

 

For now it is not working, even when I added the reporter to Insight, I will try to check which user is used to create issues.

Steve Letch February 17, 2021

hi

 

any chance someone could modify the script from the Insight documentation to copy the values in a labels field to an insight field?

 

I'm currently doing it by sending tickets through an insight post function on a global transition, but that triggers the issue updated event which spews old tickets back onto peoples boards :( As far as I'm aware when you do these edits via scriptrunner it doesnt trigger the issue updated event.

1 vote
Hana Kučerová
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
August 20, 2020

Hi @Damian Wodzinski ,

could it be, that the script is somehow affected by the user, who executes the script?

Are there any users, who are not able to view the objects in Insight?

Would you please provide me the whole code? I'll try to test it using my instance.

Which versions of Jira/ScriptRunner/Insight do you have?

Thank you.

Damian Wodzinski
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.
August 20, 2020

I posted code below, 

Jira version: 8.4.1
ScriptRunner version:  6.6.0 
Insight version:  8.6.4 

0 votes
Deleted user July 8, 2021

Run the script as an user with insight access by:

ApplicationUser superuser = userManager.getUserByName("superuser")
ComponentAccessor.getJiraAuthenticationContext().setLoggedInUser(superuser)

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events