Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

How to add a custom user picker field to a specific project role?

Muhammet Ayal
Community Champion
October 5, 2019

Hi, 

I have two user custom fields like "Project Manager" and "Project Sponsor".

When I create the issue, people in this fields are automatically added to the corresponding project roles.

Project Manager(custom user picker field)  --> Project Managers (role)

Project Sponsor(custom user picker field)  --> Project Sponsors (role)

Also I have Script Runner  and Workflow Toolbox add-ons.

1 answer

1 accepted

0 votes
Answer accepted
Ravi Sagar _Sparxsys_
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.
October 7, 2019

Hi @Muhammet Ayal 

Try this code in the create transition's script post function.

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.bc.user.UserService
import com.atlassian.jira.bc.projectroles.ProjectRoleService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.security.roles.ProjectRoleActor
import com.atlassian.jira.security.roles.ProjectRoleManager
import com.atlassian.jira.util.SimpleErrorCollection
import com.atlassian.jira.user.ApplicationUser


//get the value of Project Manager
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def projectManagerField = customFieldManager.getCustomFieldObjectsByName("Project Manager")
def projectManagerValue = issue.getCustomFieldValue(projectManagerField[0]) as ApplicationUser

//get the value of Project Manager
def projectSponsorField = customFieldManager.getCustomFieldObjectsByName("Project Sponsor")
def projectSponsorValue = issue.getCustomFieldValue(projectSponsorField[0]) as ApplicationUser

def pmActors = []
def psActors = []
pmActors.add(projectManagerValue.getUsername().toString())
psActors.add(projectSponsorValue.getUsername().toString())

def projectManager = ComponentAccessor.getProjectManager()
def projectRoleService = ComponentAccessor.getComponent(ProjectRoleService)
def projectRoleManager = ComponentAccessor.getComponent(ProjectRoleManager)
def errorCollection = new SimpleErrorCollection()
def project = issue.getProjectObject()

def projectManagerRole = projectRoleManager.getProjectRole("Project Managers")
def projectSponsorsRole = projectRoleManager.getProjectRole("Project Sponsors")

//Add to Project Managers role
projectRoleService.addActorsToProjectRole(pmActors,
projectManagerRole,
project,
ProjectRoleActor.USER_ROLE_ACTOR_TYPE,
errorCollection)
//Add to Project Sponsors role
projectRoleService.addActorsToProjectRole(psActors,
projectSponsorsRole,
project,
ProjectRoleActor.USER_ROLE_ACTOR_TYPE,
errorCollection)


Let me know if it works. It is based on single user picker field.

Ravi

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events