Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
Deleted user
0 / 0 points
Next:
badges earned

Your Points Tracker
Challenges
Leaderboard
  • Global
  • Feed

Badge for your thoughts?

You're enrolled in our new beta rewards program. Join our group to get the inside scoop and share your feedback.

Join group
Recognition
Give the gift of kudos
You have 0 kudos available to give
Who do you want to recognize?
Why do you want to recognize them?
Kudos
Great job appreciating your peers!
Check back soon to give more kudos.

Past Kudos Given
No kudos given
You haven't given any kudos yet. Share the love above and you'll see it here.

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

How to add a current user to the project role by post-function

Hello!

I have a post-function which should add a user to a project role, depending on value of custom field selected. 

I created a script based on samples which I found here on the Atlassian answers. 

My script is working but only with one condition: if I define the user name directly in the script: 

def actors = ['lsmith']

but I need script to work with current user, without defining their username in a code.  So that the current user will be added to selected project role. 

I tried this: 

def user = (ApplicationUser) ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("test_mav_user_picker").getValue(issue)

and then: 

projectRoleService.addActorsToProjectRole(currentUser,
projectRole1,
project,
ProjectRoleActor.USER_ROLE_ACTOR_TYPE,
errorCollection)

but this does not work and show an error: "Cannot find matching method"

Please help to understand how can I add a current user to a project role in a post-function? 

 

here is the full script: 

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
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.bc.issue.IssueService
import com.atlassian.jira.issue.IssueInputParameters
import groovy.transform.Field
import com.atlassian.jira.issue.customfields.option.LazyLoadedOption
import com.atlassian.jira.user.ApplicationUser


def customFieldManager = ComponentAccessor.getCustomFieldManager()
def projectManager = ComponentAccessor.getProjectManager()
def projectRoleService = ComponentAccessor.getComponent(ProjectRoleService)
def projectRoleManager = ComponentAccessor.getComponent(ProjectRoleManager)
def errorCollection = new SimpleErrorCollection()

//name of the custom field
def customFieldName = 'Notification type'
//get the custom field object
def customFieldObject = customFieldManager.getCustomFieldObjectByName(customFieldName)
Issue issue = issue

def customFieldValues = customFieldObject.getValue(issue)

def user = (ApplicationUser) ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("test_mav_user_picker").getValue(issue)


def project = projectManager.getProjectObjByKey("ATL")
def actors = ['lsmith']
def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()


customFieldValues.each { LazyLoadedOption it ->
def optionValue = it.getValue()
switch (optionValue){

case "Comments":
def projectRole = projectRoleManager.getProjectRole("Comments")
projectRoleService.addActorsToProjectRole(actors,
projectRole1,
project,
ProjectRoleActor.USER_ROLE_ACTOR_TYPE,
errorCollection)
break
case "Transition":
def projectRole1 = projectRoleManager.getProjectRole("Transition")
projectRoleService.addActorsToProjectRole(actors,
projectRole1,
project,
ProjectRoleActor.USER_ROLE_ACTOR_TYPE,
errorCollection)
break
case "Issue Update":
def projectRole2 = projectRoleManager.getProjectRole("Issue Update")
projectRoleService.addActorsToProjectRole(actors,
projectRole2,
project,
ProjectRoleActor.USER_ROLE_ACTOR_TYPE,
errorCollection)
break
case "Issue resolved":
def projectRole3 = projectRoleManager.getProjectRole("Issue resolved")
projectRoleService.addActorsToProjectRole(actors,
projectRole3,
project,
ProjectRoleActor.USER_ROLE_ACTOR_TYPE,
errorCollection)
break
}
}

 

2 answers

1 accepted

0 votes
Answer accepted
Ismael Jimoh Community Leader Feb 22, 2019

Hello Ismael Jimoh!

Thank you for the reply!

I tried to use getLoggedInUser() and then add the result  to the addActorsToProjectRole but this is not working: error-.png

Ismael Jimoh Community Leader Feb 23, 2019

Hi @Elena Oleksenko 

The following script works for me.

Can you adjust yours to match?

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.*
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.user.util.*
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.security.roles.ProjectRoleActor
import com.atlassian.jira.security.roles.ProjectRoleManager
import com.atlassian.jira.util.SimpleErrorCollection
import com.atlassian.jira.bc.projectroles.ProjectRoleService
import com.atlassian.jira.security.roles.ProjectRole
import com.atlassian.jira.util.SimpleErrorCollection
import com.atlassian.jira.project.ProjectManager
import com.atlassian.jira.project.Project

IssueManager issueManager = ComponentAccessor.getIssueManager()
Issue issue = issue //Manager.getIssueByCurrentKey("ATM-262")


def projectRoleService = ComponentAccessor.getComponent(ProjectRoleService);
def projectRoleManager = ComponentAccessor.getComponent(ProjectRoleManager);
def projectManager = ComponentAccessor.getComponent(ProjectManager);
def errorCollection = new SimpleErrorCollection()

log.warn(issue.getProjectObject().key)

ApplicationUser user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
Collection<String> actorCollection = new ArrayList<>();
actorCollection.add(user.getUsername());

log.warn(user)
Project project = projectManager.getProjectObjByKey(issue.getProjectObject().key)
//def projectRole = projectRoleManager.getProjectRole("Service Desk Customers")
ProjectRole projectRoleObject = projectRoleManager.getProjectRole("Service Desk Customers")
//def actors = ['actor1Key', 'actor2Key']
projectRoleService.addActorsToProjectRole(actorCollection,
projectRoleObject,
project,
ProjectRoleActor.USER_ROLE_ACTOR_TYPE,
errorCollection)

Do let me know if you have any further problems.

Like Elena Oleksenko likes this

It works for me, thank you!

Like Ismael Jimoh likes this

This works great, except I encounter a "We can't find '<username>' in any accessible user directory. Check they're still active and available, and try again.error when the user is not in the Jira Internal Directory.  Almost all my users are in a second user directory that's connected to our LDAP, so for them the error is produced and the user is not added to the role.  Any thoughts on how to resolve this?

One question that I've... for a search a role developer in this scripts. How I can do?

you search:

def projectRoleService = ComponentAccessor.getComponent(ProjectRoleService);
def projectRoleManager = ComponentAccessor.getComponent(ProjectRoleManager);

and if I need 

def projectRoleDeveloper = ???

Thank's for all

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Jira Service Management

JSM June Challenge #2: Share how your business teams became ITSM rockstars

For JSM June Challenge #2, share how your non-technical teams like HR, legal, marketing, finance, and beyond started using Jira Service Management! Tell us: Did they ask to start using it or...

200 views 6 7
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