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)
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());
}
}
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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())
}
}
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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 ;(
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Do you have the Scriptrunner App?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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")
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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")
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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())
}
}
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I find out! Jist deleted withPlugin and imports, and add extra classes as Class.
Thank you so much, you are God of the scripting!
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())
}
}
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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());
}
}
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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/
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thanks for providing the link to API that helped me a lot in fixing the issues in my script.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.