Hey, I have the following setup: I have a multi-user field which is supposed to contain users. I want to use a groovy script which finds the list of those users, but I cannot get it to work (new to groovy...)
The error I am getting is that:
Below is the part of the script:
You can't call "getID" on an list, it doesn't have one that is of any use.
To break that down a bit, line 141 sets objUsers to be the value of a field. That field is a list of users, not a single user. It would work for a single user picker because that would return a user, but a multi-select is a list.
I think before you reach line 143, you need to think about iterating over the list to find the user you want to work with.
Hm, I see. I did some modifications but I am still in the middle of it.
Below is my whole script. Currently I am stuck at the point where I can find:
1. a user who is an attribute 'subscribed users' of the insight object 'business service'
2. a user who is entered in the service portal and then resides in the issue
What I still am unable to do is add the user from the issue to the user in the insight object, so that the attribute would contain both users.
Another thing is, that I would like this to be a multi-user option, meaning that more than one users could be both in the jira issue and the insight attribute.
int insightSchemaID = 1;
int jiraCustomFieldIDForCPEInternalService = 13002;
int jiraCustomFieldIDForIssueSubscribedUsers = 14012;
int insightServiceSubscribedUsersID = 1056
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.MutableIssue;
import com.atlassian.jira.event.type.EventDispatchOption;
import com.atlassian.jira.issue.context.IssueContextImpl;
import com.atlassian.jira.issue.fields.config.FieldConfig;
import java.util.ArrayList;
import java.util.stream.Collectors;
/* 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 Type Facade from plugin accessor */
Class objectTypeFacadeClass = ComponentAccessor.getPluginAccessor().getClassLoader().findClass("com.riadalabs.jira.plugins.insight.channel.external.api.facade.ObjectTypeFacade");
def objectTypeFacade = ComponentAccessor.getOSGiComponentInstanceOfType(objectTypeFacadeClass);
/* 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);
/* ------------------------------------------------------------- */
/* Step 1: retrieve the CPE Internal Services from the issue */
/* ------------------------------------------------------------- */
CustomField issueCPEInternalServicesField = ComponentAccessor.getCustomFieldManager().getCustomFieldObject(jiraCustomFieldIDForCPEInternalService);
if (issueCPEInternalServicesField == null) {
log.warn("No subscribed users found in this issue. Custom field identifier was " + issueCPEInternalServicesField + ". Exiting.");
return false;
}
else {
log.info("CPE Internal Services: " + issueCPEInternalServicesField);
}
def CPEInternalServiceID = issueCPEInternalServicesField.getValue(issue);
//Although this field is defined in Jira as Insight Object (multi select), it returns an array so we need to pull the first item from the value returned
if (issueCPEInternalServicesField == null) {
log.warn("CPE Internal Service value not found. Exiting");
return false;
}
else {
log.info("CPE Internal Service ID from Jira issue: " + CPEInternalServiceID[0]);
}
/* ------------------------------------------------------------- */
/* Step 2: find the referenced CPE Internal Services in Insight */
/* ------------------------------------------------------------- */
/* Find CPE Internal Service object in Insight */
def objService = objectFacade.loadObjectBean(CPEInternalServiceID[0].id);
if (objService == null) {
log.warn("Could not find CPE Internal Services in Insight. Exiting.");
return false;
}
else {
log.info("CPE Internal Services in Insight: " + objService);
}
/* ------------------------------------------------------------- */
/* Step 3: find the Insight attribute */
/* ------------------------------------------------------------- */
def objectTypeAttributeBean = objectTypeAttributeFacade.loadObjectTypeAttributeBean(insightServiceSubscribedUsersID); //the ID of the object type attribute "Assets added to bin"
if (objectTypeAttributeBean == null) {
log.warn("Could not find ‘Subscribed Users’ field in Insight. Exiting.");
return false;
}
else
{
log.info("Insight attribute: " + objectTypeAttributeBean);
}
/* ------------------------------------------------------------------- */
/* Step 4: prep */
/* ------------------------------------------------------------------- */
//create an empty attribute that will store all the values and will ultimately replace the attribute associated with the current bin
def newObjectAttributeBean = objService.createObjectAttributeBean(objectTypeAttributeBean);
//create a list to populate with values old and new
def values = newObjectAttributeBean.getObjectAttributeValueBeans();
/* ------------------------------------------------------------------- */
/* Step 5: find the attribute for the service we're about to update */
/* ------------------------------------------------------------------- */
def objAttrib = objectFacade.loadObjectAttributeBean(objService.getId(), insightServiceSubscribedUsersID);
if (objAttrib == null) {
log.warn("ObjAttrib is null; will create a new one.");
objAttrib = objService.createObjectAttributeBean(objectTypeAttributeBean);
}
else {
log.info("Insight attribute definition: " + objAttrib);
}
/* ------------------------------------------------------------------- */
/* Step 6: retrieve list of current users in the Insight attribute */
/* ------------------------------------------------------------------- */
def existingUsersInService = objAttrib.getObjectAttributeValueBeans();
if (existingUsersInService == null)
{
log.warn ("Unable to retrieve any users from this service.");
}
else {
log.info("Existing subscribed users list: " + existingUsersInService);
}
/* ------------------------------------------------------------------- */
/* Step 7: populate list of values to be added with what's already in Insight */
/* ------------------------------------------------------------------- */
for (def existingUsers:existingUsersInService) {
log.info("Existing users found: " + existingUsers.getValue());
def objUsers = existingUsersInService;
log.info("objUsers: " + objUsers);
CustomField IssueSubscribedUsersField = ComponentAccessor.getCustomFieldManager().getCustomFieldObject(jiraCustomFieldIDForIssueSubscribedUsers);
if (IssueSubscribedUsersField == null) {
log.warn("No subscribed users found in this issue. Custom field identifier was " + IssueSubscribedUsersField + ". Exiting.");
return false;
}
else {
def SubscribedUserID = IssueSubscribedUsersField.getValue(issue);
//Although this field is defined in Jira as Insight Object (multi select), it returns an array so we need to pull the first item from the value returned
if (IssueSubscribedUsersField == null) {
log.warn("Subscribed Users not found. Exiting");
return false;
}
else {
log.info("Subscribed Users from Jira issue: " + SubscribedUserID[0]);
}
def newObjectAttributeValueBean = newObjectAttributeBean.createObjectAttributeValueBean();
newObjectAttributeValueBean.setReferencedObjectBeanId(SubscribedUserID); //add a user to the attribute
values.add(newObjectAttributeValueBean);
log.info("Array of values size is now: " + values.size());
}
//we'll use another for loop later to add more values to the current list
log.info("values list: " + values);
for (def value:values) {
log.info("Value: " + value);
}
}
/* --------------------------------------------------------------- */
/* Step 8: retrieve the Affected Object references from the issue */
/* --------------------------------------------------------------- */
CustomField affectedObjectField = ComponentAccessor.getCustomFieldManager().getCustomFieldObject(jiraCustomFieldIDForCPEInternalService);
if (affectedObjectField == null) {
log.warn("CPE Internal Service field not found in issue. Custom field identifier was " + jiraCustomFieldIDForCPEInternalService + ". Exiting");
return false;
}
else {
log.info("CPE Internal Service field: " + affectedObjectField);
}
//identify the values of the individual objects found
def affectedObjects = affectedObjectField.getValue(issue);
if (affectedObjects == null) {
log.warn("User failed to enter any objects in the CPE Internal Service field. Exiting");
return true;
}
else {
log.info("CPE Internal Service value from Jira issue: " + affectedObjects);
}
/* ----------------------------------------------------------------------------------------- */
/* Step 9: find the Assets in Insight that are referenced in Jira and add them to the list */
/* ----------------------------------------------------------------------------------------- */
for (def affectedObject:affectedObjects) {
log.info("loop");
if (affectedObject != null) {
log.info("User: " + affectedObject);
def UserId = affectedObject.getId();
log.info("User object ID: " + affectedObject.getId());
def newObjectAttributeValueBean = newObjectAttributeBean.createObjectAttributeValueBean();
newObjectAttributeValueBean.setReferencedObjectBeanId(UserId); //add an asset to the attribute
values.add(newObjectAttributeValueBean); // Add the value to the object attribute
}
else {
log.warn("User object is null!");
}
}
for (def value:values) {
log.info("Value: " + value);
log.info("Class: " + value.getClass());
//class for each value should be MutableObjectAttributeValueBean
}
log.info("Values to be added to newObjectAttributeBean, before dedupe: " + values);
/* -------------------------------------------------------- */
/* Step 10: dedupe the list */
/* -------------------------------------------------------- */
def dedupedValues = values.stream().distinct().collect(Collectors.toList());
log.info("Values to be added to newObjectAttributeBean, after dedupe: " + dedupedValues);
for (def value:dedupedValues) {
log.info("Deduped value: " + value);
log.info("class: " + value.getClass());
}
/* ------------------------------------------------------------------------ */
/* Step 11: add list to the new ObjectAttribute */
/* ------------------------------------------------------------------------ */
newObjectAttributeBean.setObjectAttributeValueBeans(dedupedValues);
log.info("newObjectAttributeBean: " + newObjectAttributeBean);
log.info("newObjectAttributeBean ID: " + newObjectAttributeBean.getId());
log.info("Final list of values that are being added to newObjectAttributeBean: " + newObjectAttributeBean.getObjectAttributeValueBeans());
/* --------------------------------------------------------------------------- */
/* Step 12: assign the new ObjectAttribute the original ObjectAttribute's ID */
/* --------------------------------------------------------------------------- */
def objAttribute = objectFacade.loadObjectAttributeBean(objService.getId(), insightServiceSubscribedUsersID);
if ( objAttribute != null) {
/* If attribute exists reuse the old id for the new attribute */
newObjectAttributeBean.setId( objAttribute.getId());
log.info("List of values before: " + objAttribute.getObjectAttributeValueBeans());
log.info(" objAttribute:" + objAttribute);
log.info(" objAttribute ID: " + objAttribute.getId());
}
/* ------------------------------------------------------------------------ */
/* Step 13: save changes */
/* ------------------------------------------------------------------------ */
try {
objAttribute = objectFacade.storeObjectAttributeBean(newObjectAttributeBean);
log.info("Successfully stored attribute");
}
catch (Exception vie) {
log.warn("Exception storing objAttribute: " + vie.getMessage());
return false;
}
//to do: add a comment to the Issue describing what was done.
return true;
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.