Forums

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

Add user field value into user attribute of insight object. Post function

it-lics@opends.tech February 8, 2022

Im trying to add user field value into user attribute of insight object. I can SET new value, but its destroy previous users in insight attribute, i need to add extra users in this list. I tried to write script but it doesnt work(

It should updates vie transition in new status.

i need to add value of customfield_10510  to insight customfield_11703 (attribute id 855)

Screenshot 2022-02-08 at 13.24.45.png

3 answers

1 accepted

0 votes
Answer accepted
PD Sheehan
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.
February 8, 2022

You have to first read the attribute values of the object into an array.

Then you can add your user to that array and re-apply that array back to the object.

It's probably a good idea to run .unique() on the array before adding it back in case the user you want to add is already in the list.

it-lics@opends.tech February 8, 2022

hmmm, started with next script, but errors:

 

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.CustomFieldManager;
import java.sql.Timestamp;
import java.text.DateFormat
import java.util.Date
import com.onresolve.scriptrunner.runner.customisers.PluginModule
import com.onresolve.scriptrunner.runner.customisers.WithPlugin
import com.riadalabs.jira.plugins.insight.services.model.factory.ObjectAttributeBeanFactory

@WithPlugin("com.riadalabs.jira.plugins.insight")
@PluginModule ObjectAttributeBeanFactory objectAttributeBeanFactory


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);

CustomField jiraCustomField = ComponentAccessor.getCustomFieldManager().getCustomFieldObject(10510);

CustomField insightCustomField = ComponentAccessor.getCustomFieldManager().getCustomFieldObject(11703);
def insightObjects = issue.getCustomFieldValue(insightCustomField);
def objectTypeAttributeBean = objectTypeAttributeFacade.loadObjectTypeAttributeBean(855);

if (insightObjects != null) {
insightObjects.each{insightObject ->

def newValue = issue.getCustomFieldValue(jiraCustomField);

my = DateFormat.getDateInstance().format(newValue);
def newObjectAttributeBean = objectAttributeBeanFactory.createObjectAttributeBeanForObject(insightObject, objectTypeAttributeBean, my);

def objectAttributeBean = objectFacade.loadObjectAttributeBean(insightObject.getId(), objectTypeAttributeBean.getId());
if (objectAttributeBean != null) {

newObjectAttributeBean.setId(objectAttributeBean.getId());
}

try {
objectAttributeBean = objectFacade.storeObjectAttributeBean(newObjectAttributeBean);
} catch (Exception vie) {
log.warn("Could not update object attribute due to validation exception:" + vie.getMessage());
}
}
}

PD Sheehan
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.
February 8, 2022

I don't understand what this has to do with anything in your initial question:

def newValue = issue.getCustomFieldValue(jiraCustomField);
my = DateFormat.getDateInstance().format(newValue);

I thought you were trying to add an ApplicationUser value from a UserPicker custom field to a User insight attribute.

Here is a new version of your script:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.user.ApplicationUser
import com.riadalabs.jira.plugins.insight.channel.external.api.facade.ObjectFacade
import com.riadalabs.jira.plugins.insight.channel.external.api.facade.ObjectTypeAttributeFacade
import com.riadalabs.jira.plugins.insight.services.events.EventDispatchOption
import com.riadalabs.jira.plugins.insight.services.model.ObjectAttributeBean
import com.riadalabs.jira.plugins.insight.services.model.ObjectBean
import com.riadalabs.jira.plugins.insight.services.model.ObjectTypeAttributeBean

import com.onresolve.scriptrunner.runner.customisers.PluginModule
import com.onresolve.scriptrunner.runner.customisers.WithPlugin
import com.riadalabs.jira.plugins.insight.services.model.factory.ObjectAttributeBeanFactory

@WithPlugin("com.riadalabs.jira.plugins.insight")
@PluginModule ObjectTypeAttributeFacade objectTypeAttributeFacade
@PluginModule ObjectFacade objectFacade

def userManager = ComponentAccessor.userManager
def cfm = ComponentAccessor.customFieldManager
CustomField jiraCustomField = cfm.getCustomFieldObject(10510) //user picker custom field
CustomField insightCustomField = cfm.getCustomFieldObject(11703) //insight object custom field
def objectTypeAttributeBean = objectTypeAttributeFacade.loadObjectTypeAttribute(855) //insight "User" attribute

def insightObjects = issue.getCustomFieldValue(insightCustomField) as List<ObjectBean>
issue = issue as Issue
if (insightObjects != null) {
insightObjects.each { insightObject ->
def newUser = issue.getCustomFieldValue(jiraCustomField) as ApplicationUser
List<ApplicationUser> existingUsers = []
def objectAttributeBean = insightObject.objectAttributeBeans.find { it.objectTypeAttributeId == objectTypeAttributeBean.id }
def mutableObjectAttributeBean
if (objectAttributeBean) {
existingUsers = objectAttributeBean.objectAttributeValueBeans.collect { userManager.getUserByKey(it.value as String) }
mutableObjectAttributeBean = objectAttributeBean.createMutable()
} else {
mutableObjectAttributeBean = insightObject.createObjectAttributeBean(objectTypeAttributeBean)
}
existingUsers << newUser
def valueBeans = existingUsers.unique().collect { user ->
def objectAttributeValueBean = objectAttributeBean.createObjectAttributeValueBean()
objectAttributeValueBean.setValue(objectTypeAttributeBean, user.key)
objectAttributeValueBean
}
mutableObjectAttributeBean.setObjectAttributeValueBeans(valueBeans)
try {
objectFacade.storeObjectAttributeBean(mutableObjectAttributeBean, EventDispatchOption.DO_NOT_DISPATCH)
} catch (Exception vie) {
log.warn("Could not update object attribute due to validation exception:" + vie.getMessage())
}
}
}

it-lics@opends.tech February 8, 2022

@PD Sheehan 

 

I thought you were trying to add an ApplicationUser value from a UserPicker custom field to a User insight attribute. - Yes, u re right, i just lost in code and didn't delete date-part

 

Just test your code, still nothing ;(Screenshot 2022-02-09 at 09.58.57.pngScreenshot 2022-02-09 at 10.04.14.png

PD Sheehan
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.
February 9, 2022

Do you have the Scriptrunner App?

it-lics@opends.tech February 9, 2022

@PD Sheehan  Sure

PD Sheehan
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.
February 9, 2022

Then try my script in the scriptrunner console instead of the Insight Groovy Console.

Just add a line at the top to get an issue:

def issue=ComponentAccessor.issueManager.getCustomFieldObject("YOURKEY-123")
it-lics@opends.tech February 9, 2022

@PD Sheehan 

Result ( and issue hasn't changed):

Screenshot 2022-02-09 at 22.01.55.pngScreenshot 2022-02-09 at 22.03.50.png

PD Sheehan
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.
February 9, 2022

Oops, I must have been distracted when I wrote this line:

def issue=ComponentAccessor.issueManager.getCustomFieldObject("YOURKEY-123")

It should be

def issue=ComponentAccessor.issueManager.getIssueObject("YOURKEY-123")
it-lics@opends.tech February 9, 2022

@PD Sheehan 

No errors, but still no changes.

Logs:

2022-02-10 09:37:57,097 WARN [runner.ScriptBindingsManager]: Could not update object attribute due to validation exception:No signature of method: com.riadalabs.jira.plugins.insight.channel.external.api.facade.impl.ObjectFacadeImpl.storeObjectAttributeBean() is applicable for argument types: (com.riadalabs.jira.plugins.insight.services.model.MutableObjectAttributeBean...) values: [ObjectAttributeBean [objectTypeAttributeId=855, objectId=199], ...] Possible solutions: storeObjectAttributeBean(com.riadalabs.jira.plugins.insight.services.model.MutableObjectAttributeBean), storeObjectAttributeBean(com.riadalabs.jira.plugins.insight.services.model.MutableObjectAttributeBean, com.riadalabs.jira.plugins.insight.services.events.EventDispatchOption), storeObjectAttributeBean(com.riadalabs.jira.plugins.insight.services.model.ObjectAttributeBean)

 

Screenshot 2022-02-10 at 09.43.31.png

PD Sheehan
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.
February 10, 2022

Try this version...  There was an error on line 55 of your screenshot.

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.user.ApplicationUser
import com.riadalabs.jira.plugins.insight.channel.external.api.facade.ObjectFacade
import com.riadalabs.jira.plugins.insight.channel.external.api.facade.ObjectTypeAttributeFacade
import com.riadalabs.jira.plugins.insight.services.events.EventDispatchOption
import com.riadalabs.jira.plugins.insight.services.model.ObjectAttributeBean
import com.riadalabs.jira.plugins.insight.services.model.ObjectBean
import com.riadalabs.jira.plugins.insight.services.model.ObjectTypeAttributeBean

import com.onresolve.scriptrunner.runner.customisers.PluginModule
import com.onresolve.scriptrunner.runner.customisers.WithPlugin
import com.riadalabs.jira.plugins.insight.services.model.factory.ObjectAttributeBeanFactory

@WithPlugin("com.riadalabs.jira.plugins.insight")
@PluginModule ObjectTypeAttributeFacade objectTypeAttributeFacade
@PluginModule ObjectFacade objectFacade

def issue=ComponentAccessor.issueManager.getIssueObject("TEST-23")
def userManager = ComponentAccessor.userManager
def cfm = ComponentAccessor.customFieldManager
CustomField jiraCustomField = cfm.getCustomFieldObject(10510) //user picker custom field
CustomField insightCustomField = cfm.getCustomFieldObject(11703) //insight object custom field
def objectTypeAttributeBean = objectTypeAttributeFacade.loadObjectTypeAttribute(855) //insight "User" attribute

def insightObjects = issue.getCustomFieldValue(insightCustomField) as List<ObjectBean>
issue = issue as Issue
if (insightObjects != null) {
insightObjects.each { insightObject ->
def newUser = issue.getCustomFieldValue(jiraCustomField) as ApplicationUser
List<ApplicationUser> existingUsers = []
def objectAttributeBean = insightObject.objectAttributeBeans.find { it.objectTypeAttributeId == objectTypeAttributeBean.id }
def mutableObjectAttributeBean
if (objectAttributeBean) {
existingUsers = objectAttributeBean.objectAttributeValueBeans.collect { userManager.getUserByKey(it.value as String) }
mutableObjectAttributeBean = objectAttributeBean.createMutable()
} else {
mutableObjectAttributeBean = insightObject.createObjectAttributeBean(objectTypeAttributeBean)
}
existingUsers << newUser
def valueBeans = existingUsers.unique().collect { user ->
def objectAttributeValueBean = mutableObjectAttributeBean.createObjectAttributeValueBean()
objectAttributeValueBean.setValue(objectTypeAttributeBean, user.key)
objectAttributeValueBean
}
mutableObjectAttributeBean.setObjectAttributeValueBeans(valueBeans)
try {
objectFacade.storeObjectAttributeBean(mutableObjectAttributeBean, EventDispatchOption.DO_NOT_DISPATCH)
} catch (Exception vie) {
log.warn("Could not update object attribute due to validation exception:" + vie.getMessage())
}
}
}
it-lics@opends.tech February 10, 2022

@PD Sheehan Good news -  it works in script console, but it doesn't work in insight post-functionScreenshot 2022-02-11 at 09.44.40.png

it-lics@opends.tech February 10, 2022

I find out! Jist deleted withPlugin and imports, and add extra classes as Class. 

 

Thank you so much, you are God of the scripting!

@PD Sheehan 

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.user.ApplicationUser
import com.riadalabs.jira.plugins.insight.channel.external.api.facade.ObjectFacade
import com.riadalabs.jira.plugins.insight.channel.external.api.facade.ObjectTypeAttributeFacade
import com.riadalabs.jira.plugins.insight.services.events.EventDispatchOption
import com.riadalabs.jira.plugins.insight.services.model.ObjectAttributeBean
import com.riadalabs.jira.plugins.insight.services.model.ObjectBean


/* 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);

 

def userManager = ComponentAccessor.userManager
def cfm = ComponentAccessor.customFieldManager
CustomField jiraCustomField = cfm.getCustomFieldObject(10510) //user picker custom field
CustomField insightCustomField = cfm.getCustomFieldObject(11703) //insight object custom field
def objectTypeAttributeBean = objectTypeAttributeFacade.loadObjectTypeAttribute(855) //insight "User" attribute

def insightObjects = issue.getCustomFieldValue(insightCustomField) as List<ObjectBean>
issue = issue as Issue
if (insightObjects != null) {
insightObjects.each { insightObject ->
def newUser = issue.getCustomFieldValue(jiraCustomField) as ApplicationUser
List<ApplicationUser> existingUsers = []
def objectAttributeBean = insightObject.objectAttributeBeans.find { it.objectTypeAttributeId == objectTypeAttributeBean.id }
def mutableObjectAttributeBean
if (objectAttributeBean) {
existingUsers = objectAttributeBean.objectAttributeValueBeans.collect { userManager.getUserByKey(it.value as String) }
mutableObjectAttributeBean = objectAttributeBean.createMutable()
} else {
mutableObjectAttributeBean = insightObject.createObjectAttributeBean(objectTypeAttributeBean)
}
existingUsers << newUser
def valueBeans = existingUsers.unique().collect { user ->
def objectAttributeValueBean = mutableObjectAttributeBean.createObjectAttributeValueBean()
objectAttributeValueBean.setValue(objectTypeAttributeBean, user.key)
objectAttributeValueBean
}
mutableObjectAttributeBean.setObjectAttributeValueBeans(valueBeans)
try {
objectFacade.storeObjectAttributeBean(mutableObjectAttributeBean, EventDispatchOption.DO_NOT_DISPATCH)
} catch (Exception vie) {
log.warn("Could not update object attribute due to validation exception:" + vie.getMessage())
}
}
}

Niharika Sarabu May 16, 2023

Hello @PD Sheehan ,

I have been using you script like this insight post function to update the values in a referenced attribute of objects, but I see an error as in below : 

Also can you share the link to details of Assets/Insights API documentation from Atlassian for future reference like for Jira https://docs.atlassian.com/software/jira/docs/api/9.4.5/overview-summary.html

Thanks in advance



Error - 


Could not update object attribute due to validation exception:ValidationInsightException: Validation errors were found: rlabs-insight-attribute-591: ErrorMessage{i18nKey='rlabs.insight.i18n.constraint.violation.ObjectAttributeValueBean.Null.value', parameters=[], additionalMessage=null};

Script-


import com.atlassian.jira.component.ComponentAccessor

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);

/* This is the custom field with the value you want to add to an object attribute */
def departmentObj = ComponentAccessor.getCustomFieldManager().getCustomFieldObject("customfield_15719")
def department = issue.getCustomFieldValue(departmentObj)[0]

/* This is the custom field where the object/s you want to set the value */
def groupsObj = ComponentAccessor.getCustomFieldManager().getCustomFieldObject("customfield_16801")
def listOfGroups = issue.getCustomFieldValue(groupsObj)

/* This is the object type attribute and the one we want to modify */
def objectTypeAttributeBean = objectTypeAttributeFacade.loadObjectTypeAttributeBean(591);

if (listOfGroups != null) {
listOfGroups.each{insightObject ->
/* Create the new attribute bean based on the value */
def newValue = insightObject.name;
log.warn(insightObject.name)
//def newObjectAttributeBean = objectAttributeBeanFactory.createObjectAttributeBeanForObject(department, objectTypeAttributeBean, newValue);
/* Load the attribute bean */
def objectAttributeBean = objectFacade.loadObjectAttributeBean(department.getId(), objectTypeAttributeBean.getId());
//log.warn(objectAttributeBean)

if (objectAttributeBean) {
existingUsers = objectAttributeBean.objectAttributeValueBeans.collect { it.value as String }
mutableObjectAttributeBean = objectAttributeBean.createMutable()
} else {
mutableObjectAttributeBean = insightObject.createObjectAttributeBean(objectTypeAttributeBean)
}
existingUsers << newValue
def valueBeans = existingUsers.unique().collect { user ->
def objectAttributeValueBean = objectAttributeBean.createObjectAttributeValueBean()
objectAttributeValueBean.setValue(objectTypeAttributeBean, user)
objectAttributeValueBean
}
mutableObjectAttributeBean.setObjectAttributeValueBeans(valueBeans)


/* Store the object attribute into Insight. */
try {
objectAttributeBean = objectFacade.storeObjectAttributeBean(mutableObjectAttributeBean) //newObjectAttributeBean);
} catch (Exception vie) {
log.warn("Could not update object attribute due to validation exception:" + vie.getMessage());
}

}
}

PD Sheehan
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.
May 16, 2023

The error you posted includes all the details you need to debug this yourself

Could not update object attribute due to validation exception:ValidationInsightException: Validation errors were found: rlabs-insight-attribute-591: ErrorMessage{i18nKey='rlabs.insight.i18n.constraint.violation.ObjectAttributeValueBean.Null.value', parameters=[], additionalMessage=null};

 

rlabs-insight-attribute-591 - this means the error is on your attribute with id 591

rlabs.insight.i18n.constraint.violation.ObjectAttributeValueBean.Null.value - the means that attribute 591 doesn't accept a null value

Go look in your attribute configuration and you will see that it is set to contain a minimum of 1 value. 

So either set it to the default of min=0 or make sure that attribute is populated for the object you are trying to modify (even if that's not the attribute you are trying to modify).

 

I found the most recent version of the assets API here: https://docs.atlassian.com/assets/10.4.4/

Niharika Sarabu May 17, 2023

Thanks for providing the link to API that helped me a lot in fixing the issues in my script.

0 votes
Yuna
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!
February 3, 2023

Hi, do you have a script to edit an user attribute on insight object from user field value in jira? I need set the new value and destroy previous user on insight, but using your script doesn´t work for me, because I don´t need a list, I need only one user. Can u help me please?

0 votes
it-lics@opends.tech February 8, 2022

@PD Sheehan Hello! You helped me one time (one month ago), mb do you know the answer? -)

Suggest an answer

Log in or Sign up to answer