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

Need help with the script to populate the data in insight objects field

Chethan GR
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 20, 2022

We have a requirement to populate the value in an insight field on the create screen on the portal when the users open the form.

 

We have an insight objects field which shows the computer objects that are owned by the logged in user and currently the user has to select the value from the dropdown but we want to auto populate the value instead of user having to select it. I'm using the below script but the value is not getting populated.

 

import com.atlassian.jira.component.ComponentAccessor

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.atlassian.jira.component.ComponentAccessor

import com.atlassian.jira.issue.Issue

import com.atlassian.jira.component.ComponentAccessor

import com.atlassian.jira.issue.fields.CustomField

@WithPlugin("com.riadalabs.jira.plugins.insight")

@PluginModule IQLFacade iqlFacade

def issue = Issue

def groupManager = ComponentAccessor.getGroupManager()

def formField = getFieldById(getFieldChanged())

// def Reporter = issue.getReporter()

/* Specify the schema id as well as the IQL that will fetch objects. In this case all objects with Name matching the valueCF, be sure to include " around value */

def objects = iqlFacade.findObjects(8, /objectType = "Computer" and "Assigned User" = "chethan.gr"/)[0];

if(objects){

    /* Insight custom field to set */

    def insightCF = ComponentAccessor.customFieldManager.getCustomFieldObject(20001)

    issue.setCustomFieldValue(objects)

    return objects;

}


1 answer

0 votes
Hieu
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.
November 13, 2022

Hi @Chethan GR you can try with IssueService :

IssueService issueService = ComponentAccessor.getIssueService();
IssueInputParameters inputParameters = issueService.newIssueInputParameters();
inputParameters.setSkipLicenceCheck(true);
inputParameters.setSkipScreenCheck(true);

inputParameters.addCustomFieldValue(insightFieldId, "Key of insight object you want to populate");
UpdateValidationResult updateValidationResult = issueService.validateUpdate(ItsmUtils.getItsmUser(), targetedIssue.getId(), inputParameters);
if (updateValidationResult.isValid()) {
issueService.update(user, updateValidationResult, EventDispatchOption.ISSUE_UPDATED, true);
} else {
log("Error when update custom field value in " + targetedIssue.getKey() + " " + updateValidationResult.getErrorCollection().toString());
}

 Hope it's can you, note "User" must have permission to perform an update on the issue and the value to update is the key of insight object

Chethan GR
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.
January 9, 2023

Hi @Hieu ,

 

Thanks for the response.

 

As the computer objects will have unique keys and providing the key would become a hardcoded value!

 

The need is to automatically populate the user's computer as they open the form. And we need to populate this when the user has only one computer and if there are many then we can ignore.

 

Regards,

Chethan

Hieu
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.
January 9, 2023
def objects = iqlFacade.findObjects(8/objectType = "Computer" and "Assigned User" = "chethan.gr"/)[0]

https://insight-javadoc.riada.io/insight-javadoc-5.0/com/riadalabs/jira/plugins/insight/services/model/ObjectBean.html#getObjectKey--


From your code above, the key of insight object already dynamic 

def keyOfInsight = objects.getObjectKey()

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events