Scriptrunner - add license

Piotr Baumert October 28, 2019

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
Piotr Baumert October 29, 2019

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 

0 votes
Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
October 28, 2019

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"

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events