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

Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,556,699
Community Members
 
Community Events
184
Community Groups

scriptrunner behaviours autopopulate asset customfield on portal

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