Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

How can I set an Insight custom field from a ScriptRunner behaviour

Lucía Ruz Sáez December 3, 2021

Hello.
We recently started to use Insight in our Jira Software. We have created the object schema. Inside this scheme we have objects with 2 insight custom field with multiple selection: 'Country' and 'Month'. 

Goal: on the creation of an issue, filter the objects depending the 'Country' and the 'Month' and set all the objects in a new insight custom field 'Camp'. It would be similar to the IQL: Country in ({values of the custom field 'Country'}) AND Month in ({values of the custom field 'Month'})

Problem: in a first instance, when you set the IQL filter to this new insight custom field, due to the multiple selection, when you select more than 1 value it change the option 'IN' to 'AND'. If for example you select 2 countries and 1 month it make the following: Country in (value1) AND Country in (value2) AND Month in (value1) instead of Country in (value1, value2) and Month in (value1).

Solution: as solution I created a behaviour with the add-on "ScriptRunner" to parse the value of the custom fields, make the IQL search correctly and set the obtained values to the custom field 'Camp'.

Problem of the solution: after search and obtain the correct objects from insight, I can't find the way to set the obtained objects in the new custom field 'Camp' from this behaviour on ScriptRunner. I have the id of the objects.

In summary, in my script I can't set the objects on a insight custom field once I have find this objects. Please, help me to find the solution. Thanks in advance.

2 answers

0 votes
Lucía Ruz Sáez December 7, 2021

Hello @Dirk Ronsmans 

This is the code:

package com.onresolve.scriptrunner.canned.jira.admin

import com.onresolve.scriptrunner.canned.CannedScript
import com.onresolve.scriptrunner.canned.util.BuiltinScriptErrors
import com.onresolve.scriptrunner.canned.util.SimpleBuiltinScriptErrors
import com.onresolve.scriptrunner.runner.customisers.PluginModule
import com.onresolve.scriptrunner.runner.customisers.WithPlugin
import com.riadalabs.jira.plugins.insight.channel.external.api.facade.IQLFacade
import com.riadalabs.jira.plugins.insight.services.model.ObjectBean
import org.apache.log4j.Level
import org.apache.log4j.Logger


import com.atlassian.jira.component.ComponentAccessor

Logger LOG = Logger.getLogger("GetGroups")


@WithPlugin("com.riadalabs.jira.plugins.insight")
@PluginModule IQLFacade iqlFacade


//MAP with the countries
def countries = [
"MAR-191560": "AT",
"MAR-191562": "BE",
"MAR-191561": "BG",
"MAR-191563": "CH",
"MAR-191564": "CZ",
"MAR-190876": "DE",
"MAR-191565": "DK",
"MAR-190817": "ES",
"MAR-191566": "FI",
"MAR-190819": "FR",
"MAR-191567": "GB",
"MAR-191568": "GR",
"MAR-191570": "HR",
"MAR-191569": "HU",
"MAR-191571": "IE",
"MAR-190818": "IT",
"MAR-191572": "NL",
"MAR-191573": "NO",
"MAR-191574": "PL",
"MAR-191575": "PT",
"MAR-191576": "RO",
"MAR-191577": "RU",
"MAR-191578": "SE",
"MAR-191579": "SI",
"MAR-191580": "SK"
]

def months = [
"MAR-190862": "01-Jan",
"MAR-190863": "02-Feb",
"MAR-190864": "03-Mrz",
"MAR-190865": "04-Apr",
"MAR-190866": "05-Mai",
"MAR-190867": "06-Jun",
"MAR-190868": "07-Jul",
"MAR-190869": "08-Aug",
"MAR-190870": "09-Sep",
"MAR-190871": "10-Okt",
"MAR-190872": "11-Nov",
"MAR-190873": "12-Dez"
]


//get countries selectid on the custom field Country
def country = getFieldById(getFieldChanged()).getValue();

//get month in "month"
def month = getFieldById("customfield_19441").getValue();

//Set IQL
def IQL = "\"Status\" = Active"


//If is selected only one country
if(country in countries){
country = country as String
IQL += " AND \"Country - Shop\" IN ("+countries[country]+")"
}

//More than one country
else if (country != "null"){

// Create the IQL to the search of the objects
country = country as List
int size_list = country.size()
int iterator = 0

for (String item : country){
if (iterator == 0){
IQL += " AND \"Country - Shop\" IN ("+countries[item]
}else if(iterator == (size_list - 1)){
IQL += ", "+ countries[item] + ")"
} else {
IQL += ", "+countries[item]
}
iterator += 1
}
}

if (month in months){
month = month as String
IQL += " AND Month in (" + months[month] + ")"
} else if (month != "null"){
month = month as List
int size_list = month.size()
int iterator = 0

for (String item : month){
if (iterator == 0){
IQL += "AND Month IN ("+months[item]
}else if(iterator == (size_list - 1)){
IQL += ", "+ months[item] + ")"
} else {
IQL += ", "+months[item]
}
iterator += 1
}
}

List<ObjectBean> objects = iqlFacade.findObjects(8,IQL)


//Set field Camp
getFieldById("customfield_17873").setFormValue(objects)

 If I make the IQL search, it works fine, but when I set this list on the insight object it does not show the results on the custom field. I tried to make an iterator on the list and then print the result and nothing...

Dirk Ronsmans
Community Champion
December 7, 2021

well I believe your issue is just with the last 2 lines:

List<ObjectBean> objects = iqlFacade.findObjects(8,IQL)


//Set field Camp
getFieldById("customfield_17873").setFormValue(objects)

 

you are setting objects through the setFormValue but the parameters is just expecting a list of Strings and not objectBean's.

I believe I had to do a similar thing in the past where I iterate over the beans and just create a String of objectKey's and set those on the setFormValue()

Lucía Ruz Sáez December 14, 2021

I tried to set the options with "setFieldOptions" but with the Insight object does not change anything. I also tried to set it manually instead use the List with the IQL, introducing the key as String of the objects, but neither... 

 

def camp = getFieldByName("Campaign")

def camp2 = customFieldManager.getCustomFieldObject(17873)
def config2 = camp2.getRelevantConfig(issueContext)


def options2 = ComponentAccessor.optionsManager.getOptions(config2)

camp.setFieldOptions(options2.findAll {it.value in ["MAR-190822"]})
Martin Hohenberg
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!
June 9, 2022

Have you ever found a solution for this? Looking at a similar problem. 

https://xkcd.com/979/ applies here, painfully :)

0 votes
Dirk Ronsmans
Community Champion
December 4, 2021

Hi @Lucía Ruz Sáez and welcome to the community!

Could you post the code you use to set the value?

It should normally not be any more difficult than to use the 

setFormValue("[objectKey]") function.

So if you have the objects already retrieved, you should be able to get the key of the insight object and set that as a string on the yourfield.setFormValue function.

Suggest an answer

Log in or Sign up to answer