I been struggling for a week to figure out why i cant get this script to work.
i have 2 customfields, one with a system list, and one with an employee list.
If a system is choosen i want to autopopulate the employee list with the system approvers that is added for the system, and also be able to add more users.
My script so far. ( i'm not a coder, bear with me)
I have several pointers:
1) it is best practice in java and groovy to reserve the initial cap for class names. Instances of those classes or any other variables should start with a lowercase.
So
@PluginModule ObjectTypeAttributeFacade objectTypeAttributeFacade
//and
def systemApprover = 'System approver'
Rather than
@PluginModule ObjectTypeAttributeFacade ObjectTypeAttributeFacade
//and
def SystemApprover = 'System approver'
2) Don't confuse CustomFields and FormFields. In behaviours, we interact with formFields. In postFunctions and listeners, we interact with CustomFields
3) You have a few other issues with either missing quotes around strings or calling invalid methods (objectAttributeBeans vs objectAttributeBean).
4) I'm assuming your System approver insight attribute is a "Jira User" type. This attribute stores the "User Key". Which may be something like JIRAUSER1234. But behavior needs to specify the User Name for a user picker field.
Try this .... it should work, or at least be close:
import com.atlassian.jira.component.ComponentAccessor
import com.onresolve.jira.groovy.user.FieldBehaviours
import com.onresolve.scriptrunner.runner.customisers.PluginModule
import com.onresolve.scriptrunner.runner.customisers.WithPlugin
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.model.ObjectBean
import groovy.transform.BaseScript
@WithPlugin('com.riadalabs.jira.plugins.insight')
@BaseScript FieldBehaviours fieldBehaviours
@PluginModule ObjectFacade objectFacade
@PluginModule ObjectTypeAttributeFacade objectTypeAttributeFacade
// systemchoice customfield_12800
// approver customfield_12803
def systemFld = getFieldById('customfield_12800')
def approverFld = getFieldById('customfield_12803')
//in my experience, there are time when behaviour returns a list of object key, while others just the single value.
def systemFldValue = systemFld.value
if (systemFldValue instanceof List) {
systemFldValue = systemFldValue[0]
}
//get an object bean for the selected system
def systemObject = objectFacade.loadObjectBean(systemFldValue as String) as ObjectBean
//get the System approver value from the System object
def systemApproverAttName = 'System approver'
def approverAttribute = objectTypeAttributeFacade.loadObjectTypeAttribute(systemObject.objectTypeId, systemApproverAttName)
def systemObjApproverAttr = systemObject.objectAttributeBeans.find { it.objectTypeAttributeId == approverAttribute.id }
def approverUsers = systemObjApproverAttr.objectAttributeValueBeans.collect { ComponentAccessor.userManager.getUserByKey(it.value as String) }
// Set approver with 'system approver'
approverFld.setFormValue(approverUsers*.name)
log.warn(Approver: approverUsers)
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.