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

scriptrunner behaviours autopopulate asset customfield on portal

Martin Svensson April 12, 2023

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)


import com.atlassian.jira.component.ComponentAccessor
import com.onresolve.jira.groovy.user.FieldBehaviours
import com.riadalabs.jira.plugins.insight.services.model.ObjectAttributeBean
import com.riadalabs.jira.plugins.insight.services.model.ObjectAttributeValueBean
import groovy.transform.BaseScript
import com.onresolve.scriptrunner.runner.customisers.WithPlugin
import com.onresolve.scriptrunner.runner.customisers.PluginModule
import com.riadalabs.jira.plugins.insight.channel.external.api.facade.ObjectFacade
import com.riadalabs.jira.plugins.insight.channel.external.api.facade.ObjectTypeAttributeFacade

@WithPlugin('com.riadalabs.jira.plugins.insight')
@BaseScript FieldBehaviours fieldBehaviours
@PluginModule ObjectFacade objectFacade
@PluginModule ObjectTypeAttributeFacade ObjectTypeAttributeFacade
@PluginModule ObjectAttributeValueBean ObjectAttributeValueBean

// systemchoice customfield_12800
// approver customfield_12803


def CustomFieldManager = ComponentAccessor.getCustomFieldManager()

def System =CustomFieldManager.getCustomFieldObject(customfield_12800)
   
def Approver = CustomFieldManager.getCustomFieldObject(customfield_12803)

def SystemApprover = 'System approver'

// Set approver with 'system approver'

    def SystemObject = ObjectFacade.loadObjectBean(System[0])
    def approverAttribute = ObjectTypeAttributeFacade.loadObjectTypeAttribute(systemObject.objectTypeId, SystemApprover)
    def ObjectAttribute = SystemObject.objectAttributeBean.find{ it.objectTypeAttributeId == approverAttribute.id }
    def ApproverUsers = ObjectAttribute.objectAttributeValueBeans.collect{Name.value as String}
Approver.setFormValue(ApproverUsers)
log.warn(Approver : ApproverUsers)
Any ideas?

1 answer

0 votes
Peter-Dave 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.
April 14, 2023

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)

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
SERVER
TAGS
AUG Leaders

Atlassian Community Events