Behaviour script to set field based on "isUserInGroup"

I'm trying to write a Behaviours validator script to update the "Client" field based on whether the reporter belongs to a particular user group.

I'm struggling to get "isUserInGroup" working - the script works fine if I just say "if(true)"

Can someone please help me? What's wrong?

import com.atlassian.jira.component.ComponentAccessor

def groupManager = ComponentAccessor.getGroupManager()

FormField client = getFieldByName("Client")
FormField reporter = getFieldByName("Reporter")

if (groupManager.isUserInGroup(reporter.getFormValue(), "External Clients (CCB)"))
{
 client.setFormValue("10304")
}

thanks,

Mark

1 answer

1 accepted

1 vote

You should dump out the reporter and client fields to the log...

Most likely the issue is you need to use getFieldById("reporter"), not byName. byName is for custom fields IIRC.

changing to getFieldById("reporter") worked first time - thanks!

Jamie, It fixed my script too, however my script works only on "first run" and does not react on changing the reporter (that should change the group I believe): {code} import com.atlassian.jira.component.ComponentAccessor def groupManager = ComponentAccessor.getGroupManager() FormField Reporter = getFieldById("reporter") FormField OnBehalfof = getFieldByName("On Behalf of") if (groupManager.isUserInGroup(Reporter.getFormValue(), "employees")) { OnBehalfof.setRequired(true) } if (groupManager.isUserInGroup(Reporter.getFormValue(), "customers")) { OnBehalfof.setRequired(false) } {code}

Suggest an answer

Log in or Sign up to answer
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Published Tuesday in Marketplace Apps

If you’re an Atlassian app developer, you’ll want to know about Atlas Camp!

This September 6-7, hundreds of Atlassian App developers will flock to Barcelona Spain to build skills, discover product roadmaps, meet face-to-face with the Atlassian team, and learn how to extend t...

108 views 0 2
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you