Forums

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

Groovy Script for updating assignee and reporter

DILEEP KUMAR April 12, 2018

Hello All,

The below script i am using for the giving the condition for Unassigned issue.

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue;

if(issue.assigneeId=='Unassigned'){
issue.assigneeId = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser().name
}

Is the above script correct ? Here if condition is not working.

Any Help/Suggestion would be appreciated.

Regards,

Dileep Kumar

1 answer

1 accepted

0 votes
Answer accepted
Tarun Sapra
Community Champion
April 12, 2018

When you want to update an issue you have to put the script in "post-function" . This code should work

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue;


if(issue.assigneeId){ // if assignee is empty
issue.assignee = ComponentAccessor.jiraAuthenticationContext.loggedInUser;
}
DILEEP KUMAR April 12, 2018

Thanks Tarun for the help !!

PFA. I have tested the above script, but its not able to update the Assignee with current/Logged in user.

Its taking the Unassigned value. Can you please help me regarding this ?

Regards,

Dileep Kumar

IssuePage.PNG

Tarun Sapra
Contributor
April 12, 2018

where are you inserting the script?

DILEEP KUMAR April 12, 2018

Hi Tarun,

Thanks for the help !!

I am inserting my script in post function.

Any suggestion ?

 

Regards,

Dileep Kumar

Tarun Sapra
Community Champion
April 13, 2018

Hello @DILEEP KUMAR

The code I showed is for updating the assignee but you also have to store the value in the DB, thus better use the issueService instead of issue.setAssignee method and then it should work for sure.

Please see 2 code samples here

https://community.atlassian.com/t5/Jira-questions/Why-can-I-not-set-assignee-by-ID-Script-Runner/qaq-p/87404

 

https://community.atlassian.com/t5/Jira-questions/Updating-assignee-via-scriptrunner/qaq-p/637155

DILEEP KUMAR April 13, 2018

Thanks for the help Tarun !!

I will check it.

Regards,

Dileep Kumar

Tarun Sapra
Community Champion
April 13, 2018

Working code

import com.atlassian.jira.component.ComponentAccessor

def issueService = ComponentAccessor.issueService

def user = ComponentAccessor.jiraAuthenticationContext.loggedInUser;
def validateAssignResult = issueService.validateAssign(user,issue.id, user.username)
if(validateAssignResult.isValid()) {
issueService.assign(user,validateAssignResult)
}

If it works, then please upvote/accept the answer so that others are helped as well.

Like # people like this
DILEEP KUMAR April 13, 2018

Hi Tarun, 

Thanks for the help !!

I am able to get the issue assigned if its Unassigned/Automatic to logged-in user.

But, if am assigning issue to other users, its taking logged-in user.

So, Can you please put the condition on the above script ?

Like if Assignee==Unassigned/Automatic  then only assignee==logged-in user.

 

Regards,

Dileep Kumar

Tarun Sapra
Community Champion
April 13, 2018

Hello @DILEEP KUMAR

The APIs are available here - https://docs.atlassian.com/software/jira/docs/api/7.6.1/

As i have already shared a code sample which was the original question, thus it has been answered. Please go through the APIs docs and the Script Runner docs

DILEEP KUMAR April 15, 2018

Hi Tarun,

Thanks for the help !!

Yes you have answered.

But issue with your code is that even if i am assigning issue to other assignee its taking the logged-in user.

It should take the assignee as logged-in user, only if assignee is empty not if i am assigning it to other assignee.

Suppose, i have 10 different assignee then how i can make list and test the if condition ?

 

Regards,

Dileep Kumar

Tarun Sapra
Contributor
April 16, 2018

You can enter a condition to check if the assignee exists or not and if it didn't exist then you can assign the issue to current user whcih I have already shared with you, Hope this answers your question.

if(issue.assignee) { 
//rest of the code here
}

  

DILEEP KUMAR April 16, 2018

Hi Tarun,

Thanks for the help !! I will check and update the same.

Regards,

Dileep Kumar

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events