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

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

Scriptrunner - add license

I want to create an automation rule on post-function and struggle with my code:

 

So, when I will put code like this:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.user.ApplicationUser

def groupManager = ComponentAccessor.getGroupManager()
def customFieldManager = ComponentAccessor.getCustomFieldManager()

def user_obj = customFieldManager.getCustomFieldObject("customfield_18702")
def user = issue.getCustomFieldValue(user_obj)

def group = groupManager.getGroup("jira-users")
groupManager.addUserToGroup(user,group)

 

Code working fine and the user will receive a correct license (in this case software license)
BUT!
When I tried to mix 2 custom fields so

1. User picker - who should receive a license
2. License - what kind of license should receive

it's not working at all, code below:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.user.ApplicationUser

def groupManager = ComponentAccessor.getGroupManager()
def customFieldManager = ComponentAccessor.getCustomFieldManager()

def user_obj = customFieldManager.getCustomFieldObject("customfield_18702")
def user = issue.getCustomFieldValue(user_obj)

def license_obj = customFieldManager.getCustomFieldObject("customfield_11510")
def license = issue.getCustomFieldValue(license_obj)

if(license == "Service Desk License"){
                def group = groupManager.getGroup("jira-service-desk-agents")
                groupManager.addUserToGroup(user,group)
}

else if(license == "Software License"){
                def group = groupManager.getGroup("jira-users")
                groupManager.addUserToGroup(user,group)
}

And the error is: Cannot find matching method

2 answers

0 votes

My guess is that your licence field is a select list?  If that's so, then it is the "if" that is failing, as you can't compare an array of options with a string like that.

Put in some debug code to have a look at what is in the variable "licence" just before the first "if"

Hello Nic,

That was helpful, I've resolved the issue by adding .toString() to the variable. I will put here code for next generations to leave ready solution. Short description: 

Automatically adding selected license (Jira Software license or Jira Service Desk license) (from a select list) to the selected user (from user picker).

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.user.ApplicationUser

def groupManager = ComponentAccessor.getGroupManager()
def customFieldManager = ComponentAccessor.getCustomFieldManager()

def user_obj = customFieldManager.getCustomFieldObject("customfield_18702")
def user = issue.getCustomFieldValue(user_obj)


def license_obj = customFieldManager.getCustomFieldObject("customfield_11510")
def license = issue.getCustomFieldValue(license_obj).toString()

if(license == "Service Desk License"){
def group = groupManager.getGroup("jira-service-desk-agents")
groupManager.addUserToGroup(user,group)
}
else if(license == "Software License"){
def group = groupManager.getGroup("jira-users")
groupManager.addUserToGroup(user,group)
}
else if(license == "Service Desk and Software License"){
def group = groupManager.getGroup("jira-users")
def group1 = groupManager.getGroup("jira-service-desk-agents")
groupManager.addUserToGroup(user,group)
groupManager.addUserToGroup(user,group1)
}

Code added to post-function in the workflow.

Kind regards,
Piotr Baumert 

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Marketplace Apps & Integrations

5 mobile apps for Jira Cloud to boost productivity

  It’s very important to have access to the workflow process from anywhere. Especially if you manage the work of others. There is no difference whether you’re out of office, or drive a ca...

241 views 2 5
Read article

Community Events

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

Find an event

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

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you