Am I assigning the users in the right way?

I am using the following code to assign user in a postfunction depending on a condition:

 

 

for(Issue subtask: issue.getSubTaskObjects()){

    mutableIssue = issueManager.getIssueObject(subtask.getId())

if(mutableIssue.summary=='SomeSummary'){

    ApplicationUser usera=userManager.getUserByName("SomePerson")

mutableIssue.setAssignee(usera);

issueManager.updateIssue(user, mutableIssue, com.atlassian.jira.event.type.EventDispatchOption.DO_NOT_DISPATCH, false);
    } 

 

 

After creation, it is assigned to SomePerson. I also have a Kanban board which has a filter of displaying the issues which are only assigned to SomePerson. This issue doesn't show up there. When I go to the issue and manually assign it to some other user and then back to this user (SomePerson), this issue shows up on the kanban board. So I am assuming this is due to some error in assigning the issue programmatically. What is the problem here?

 

 

 

2 answers

Hi Shagufta,

The following code will assign the issue TP-1 to the user with username admin

def issueKey = 'TP-1'
def result = put('/rest/api/2/issue/' + issueKey)
        .header('Content-Type', 'application/json')
        .body([
        fields:[
                assignee: [name: 'admin']
        ]
])
        .asString()
if (result.status == 204) {
    println 'Successfully assigned to admin'
} else {
    println "Failed to assign to admin: ${result.status}: ${result.body}"
}

Regards, Jon

Tarun Sapra Community Champion Nov 28, 2016

Why would you want to do an REST call from an post function? 

In JIRA Cloud, REST APIs are the only way to interact with JIRA. See the ScriptRunner documentation for more information.

Tarun Sapra Community Champion Nov 29, 2016

Thanks Jon for clarification.

1 vote
Vasiliy Zverev Community Champion Nov 28, 2016

Dear @Shagufta Gurmukhdas, try this code:

for(Issue subtask: issue.getSubTaskObjects()) {
    mutableIssue = issueManager.getIssueObject(subtask.getId())
    if (mutableIssue.summary == 'SomeSummary') {
        ApplicationUser usera = userManager.getUserByName("SomePerson")
        mutableIssue.setAssignee(usera.directoryUser);
        issueManager.updateIssue(user, mutableIssue, com.atlassian.jira.event.type.EventDispatchOption.DO_NOT_DISPATCH, false);
    }
}

But the setAssignee function is expecting an ApplicationUser object @Vasiliy Zverev

This code will not work in JIRA Cloud - see the ScriptRunner documentation for more info.

Vasiliy Zverev Community Champion Nov 29, 2016

Class depends on JIRA version. Which one do you use?

I did convert it using ApplicationUsers.from(usera.directoryUser) too, but the original issue is still there

Vasiliy Zverev Community Champion Nov 29, 2016

For me it is better to use this:

for(Issue subtask: issue.getSubTaskObjects()) {
    mutableIssue = issueManager.getIssueObject(subtask.getId())
    if (mutableIssue.summary == 'SomeSummary') {
        ApplicationUser usera = userManager.getUserByName("SomePerson")
        mutableIssue.setAssigneeId(usera.getName());
        issueManager.updateIssue(user, mutableIssue, com.atlassian.jira.event.type.EventDispatchOption.DO_NOT_DISPATCH, false);
    }
}

It does not work. The above code just assigns that string with the name to the issue which is not clickable( a hyperlink of user's profile) . I mean, it does not assign it to the user.

Vasiliy Zverev Community Champion Nov 29, 2016

Hm, let try this one:

for(Issue subtask: issue.getSubTaskObjects()) {
    mutableIssue = issueManager.getIssueObject(subtask.getId())
    if (mutableIssue.summary == 'SomeSummary') {
        ApplicationUser usera = userManager.getUserByName("SomePerson")
        mutableIssue.setAssigneeId(usera.getKey());
        issueManager.updateIssue(user, mutableIssue, com.atlassian.jira.event.type.EventDispatchOption.DO_NOT_DISPATCH, false);
    }
}

This works, but the original issue is still there(the kanban board of this user doesnt display the issue until manually assigned. ) 

Vasiliy Zverev Community Champion Nov 29, 2016

Is this postfunction is used on create transiton?

No, this is on the progress transition

when this issue is transitioned to inprogress, the subtasks should be assigned to appropriate users automatically. And that is happening fine in the code. The only problem is, it isnt shpowing up on the users' kanban boards, but after i again manually assign it to them, it is showing. This is so strange..

Vasiliy Zverev Community Champion Nov 30, 2016

Hm, is there reindex postfunction after this script one?

Yes there is reindex after 2-3 steps. This script is the first step.

Vasiliy Zverev Community Champion Nov 30, 2016

I have never faced this case. I think that you should create another answer about it.

Suggest an answer

Log in or Sign up to answer
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Published Thursday in Jira

5 ways you can make the most of Jira Software and Bitbucket Cloud

As part of the Bitbucket product team I'm always interested in better understanding what kind of impact the use of our tools have on the way you work. In a recent study we conducted of software devel...

69 views 0 5
Read article

Atlassian User Groups

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

Find a group

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

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you