How to check if assignee is in a project role through post function?

Teja July 10, 2018

Hi Team,

I want to copy Assignee value to Testers field only if the Assignee is a QA User project role using groovy script.

Can someone help me.

Thanks in advance

1 answer

1 accepted

4 votes
Answer accepted
Ivan Tovbin
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 10, 2018

Hi Teju,

Here's how you do it. I'm going to assume that your 'Testers' field type is a single user picker and its ID is 11111:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.security.roles.ProjectRoleManager
import com.atlassian.jira.security.roles.ProjectRole


ProjectRoleManager roleMgr = ComponentAccessor.getComponent(ProjectRoleManager)
ProjectRole qaUserRole = roleMgr.getProjectRole("QA User")
CustomField testers = ComponentAccessor.getCustomFieldManager().getCustomFieldObject((Long) 11111)


if (roleMgr.isUserInProjectRole(issue.getAssignee(), qaUserRole, issue.getProjectObject())){
issue.setCustomFieldValue(testers, issue.getAssignee())
}
Teja July 11, 2018

@Ivan Tovbin,

Yes it worked. but the Testers field is multi user picker and need to append users not to override. How to do that?

Ivan Tovbin
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 11, 2018

That's easy.

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.security.roles.ProjectRoleManager
import com.atlassian.jira.security.roles.ProjectRole
import com.atlassian.jira.user.ApplicationUser


ProjectRoleManager roleMgr = ComponentAccessor.getComponent(ProjectRoleManager)
ProjectRole qaUserRole = roleMgr.getProjectRole("QA User")
CustomField testers = ComponentAccessor.getCustomFieldManager().getCustomFieldObject((Long) 11111)


if (roleMgr.isUserInProjectRole(issue.getAssignee(), qaUserRole, issue.getProjectObject())){
ArrayList<ApplicationUser> testerList = issue.getCustomFieldValue(testers)
if (!testerList.contains(issue.getAssignee()){
testerList.add(issue.getAssignee())
issue.setCustomFieldValue(testers, testerList)
}

}
Like # people like this
Teja July 11, 2018

Hi @Ivan Tovbin,

Now am getting error.

Attached screenshot

tttt.PNG

Ivan Tovbin
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 12, 2018

You can pretty much ignore this error, the code should still work. Static type checking in Scriptrunner is not always accurate. Oh and looking at your screenshot, don't forget to change 11111 to an actual ID of your field.

Like Teja likes this

Suggest an answer

Log in or Sign up to answer