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
Atlassian Community Anniversary

Happy Anniversary, Atlassian Community!

This community is celebrating its one-year anniversary and Atlassian co-founder Mike Cannon-Brookes has all the feels.

Read more
Community showcase
Bridget Sauer
Published Thursday in Marketplace Apps

Calling all developers––You're invited to Atlas Camp 2018

 Atlas Camp   is our developer event which will take place in Barcelona, Spain  from the 6th -7th of   September . This is a great opportunity to meet other developers and get n...

236 views 0 6
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