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.

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.

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

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.

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. ) 

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..

Hm, is there reindex postfunction after this script one?

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

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
Atlassian Community Anniversary

Happy Anniversary, Atlassian Community!

This community is celebrating its one-year anniversary and Atlassian co-founder Mike Cannon-Brookes has all the feels.

Read more
Community showcase
Julia Dillon
Posted Tuesday in Jira

Tell us how your team runs on Jira!

Hey Atlassian Community! Today we are launching a bunch of customer stories about the amazing work teams, like Dropbox and Twilio, are doing with Jira. You can check out the stories here. The thi...

533 views 1 18
Join discussion

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