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

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

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

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
Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
Feb 08, 2022 • edited

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.

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

Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
Feb 08, 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())
}
}
}

@Peter-Dave 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

Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
Feb 09, 2022

Do you have the Scriptrunner App?

Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
Feb 09, 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")
Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
Feb 09, 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")

@Peter-Dave 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

Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
Feb 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())
}
}
}

@Peter-Dave 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

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

 

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

@Peter-Dave 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())
}
}
}

Hello @Peter-Dave 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());
}

}
}

Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
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/

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

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?

@Peter-Dave 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