Updating assignee via scriptrunner

Tayyab Bashir
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.
September 8, 2017

Hi,

I am running this in ScriptRunner's console, just for testing purpose.  And I am stummed on this.
In the following, I am just getting an issue, a user, and assigning the issue to that user. 

Why is this not working?
It just keeps returning null without changing the assignee.

import com.atlassian.jira.component.ComponentAccessor

def issueManager = ComponentAccessor.getIssueManager()
def userManager = ComponentAccessor.getUserManager()

def user = userManager.getUserByName("tbas")

def issueObject = issueManager.getIssueObject("HW_TEST-64")

issueObject.setAssignee(user)

 The logic makes sense, the method here says it should work aswell? See this.

But I don't kno why this aint working. 
Could anyone tell me? 

3 answers

1 accepted

4 votes
Answer accepted
Stephen Cheesley _Adaptavist_
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.
September 8, 2017

Hi Tayyab,

There are two main issues here. The first one is that, whilst you are setting the Assignee on the object, there is no persistence happening. I have included an adjusted sample script to do what you want below:

import com.atlassian.jira.component.ComponentAccessor

def issueManager = ComponentAccessor.getIssueManager()
def issueService = ComponentAccessor.getIssueService()
def userManager = ComponentAccessor.getUserManager()

def user = userManager.getUserByName("tbas")

def issue = issueManager.getIssueObject("HW_TEST-64")

def validateAssignResult = issueService.validateAssign(user, issue.id, issue.reporterId)
issueService.assign(user, validateAssignResult)

This script will perform the assignment you are looking for. There is a really good answer to this question by Jamie Echlin here Why can I not set assignee by ID Script Runner

I hope this helps!

Tayyab Bashir
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.
September 8, 2017

Hi Stephen,

This works. I didn't even think about validation because the method didn't indicate anything like that. But this makes sense and helps a lot.

Thanks :)

Like # people like this
Traian H October 5, 2020

Actually when running this code I found a small error. What the code does is assign the reporter to the assignee. What I believe that the question was actually how to assign a user to the assignee (in this case the user id is "tbas")

The line:

def validateAssignResult = issueService.validateAssign(user, issue.id, issue.reporterId)

Should be changed to:

def validateAssignResult = issueService.validateAssign(user, issue.id, "tbas")
Like Shereen likes this
Jayesh Raghuvanshi June 21, 2022

Hi Team,

I am facing some issue in assigning users to a Jira issue via script runner

We have a custom field where the assignee details are been added, I am trying the below code to fetch the value of the custom field but getting it as an object.

Kindly help me with the issue. I need to update assignee of Jira issue by the custom field value

def issueService = ComponentAccessor.getIssueService()
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def cField = customFieldManager.getCustomFieldObject("customfield_56900"
String cFieldValue = issue.getCustomFieldValue(cField) 
 log.error "Email address:" + cFieldValue

Output of log.error is : 2022-06-21 08:08:26,022 ERROR [runner.ScriptBindingsManager]: Email address:[jayraghu(JIRAUSER160013)]

Kindly help me with the issue
0 votes
Rafal Unit4Teta March 26, 2019

Hi,

I try update assignee field on postfunction. I read several topics, but I have a problem still.

My code:

import com.atlassian.jira.component.ComponentAccessor
import org.apache.log4j.Level
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.fugue.Option
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.onresolve.scriptrunner.runner.customisers.PluginModule
import com.onresolve.scriptrunner.runner.customisers.WithPlugin
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.issue.fields.layout.field.FieldLayoutItem
import com.atlassian.jira.issue.comments.CommentManager
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.util.ImportUtils
import com.atlassian.jira.user.util.UserManager
import com.atlassian.crowd.embedded.api.User


log.setLevel(Level.DEBUG)
//Grab necessary Components
def cfm = ComponentAccessor.getCustomFieldManager()
def optionsManager = ComponentAccessor.getOptionsManager()
def userManager = ComponentAccessor.getUserManager()
def issueService = ComponentAccessor.getIssueService()

def RealizujacyCf = cfm.getCustomFieldObject(11701L)
log.info("RealizujacyCf: " + RealizujacyCf)
def RealizujacyValue = issue.getCustomFieldValue(RealizujacyCf)
log.info("RealizujacyValue: " + RealizujacyValue)
def ClassOfField = RealizujacyValue.getClass()
log.info("ClassOfField: " + ClassOfField)
def ArreySize = RealizujacyValue.size()
log.info("ArreySize: " + ArreySize)


def Realizujacy = issue.getCustomFieldValue(RealizujacyCf).get(0).getName()
log.info("Realizujacy: " + Realizujacy)
def cfWlasciciel = cfm.getCustomFieldObject(11100L)


def user = userManager.getUserByName(Realizujacy);
def validateAssignResult = issueService.validateAssign(user, issue.id, issue.reporterId)
log.info("validateAssignResult: " + validateAssignResult)
//issueService.assign(user, validateAssignResult)

log.info("user: " + user)

//issue.store();

This line return the name of user, ex.: anowak(anowak),

def user = userManager.getUserByName(Realizujacy);

Now, I'd like to update assignee filed with value of "user ".

Without this line:

issueService.assign(user, validateAssignResult)

I have this result:

2019-03-26 16:27:39,829 INFO [workflow.ScriptWorkflowFunction]: RealizujacyCf: Realizujący
2019-03-26 16:27:39,830 INFO [workflow.ScriptWorkflowFunction]: RealizujacyValue: [anowak(anowak)]
2019-03-26 16:27:39,830 INFO [workflow.ScriptWorkflowFunction]: ClassOfField: class java.util.ArrayList
2019-03-26 16:27:39,830 INFO [workflow.ScriptWorkflowFunction]: ArreySize: 1
2019-03-26 16:27:39,830 INFO [workflow.ScriptWorkflowFunction]: Realizujacy: anowak
2019-03-26 16:27:39,830 INFO [workflow.ScriptWorkflowFunction]: user: anowak(anowak)
2019-03-26 16:27:39,830 INFO [workflow.ScriptWorkflowFunction]: validateAssignResult: com.atlassian.jira.bc.issue.IssueService$AssignValidationResult@330196c2

But with this line:

issueService.assign(user, validateAssignResult)

I have this result:

2019-03-26 16:32:23,095 INFO [workflow.ScriptWorkflowFunction]: RealizujacyCf: Realizujący
2019-03-26 16:32:23,095 INFO [workflow.ScriptWorkflowFunction]: RealizujacyValue: [anowak(anowak)]
2019-03-26 16:32:23,095 INFO [workflow.ScriptWorkflowFunction]: ClassOfField: class java.util.ArrayList
2019-03-26 16:32:23,096 INFO [workflow.ScriptWorkflowFunction]: ArreySize: 1
2019-03-26 16:32:23,096 INFO [workflow.ScriptWorkflowFunction]: Realizujacy: anowak
2019-03-26 16:32:23,096 INFO [workflow.ScriptWorkflowFunction]: user: anowak(anowak)
2019-03-26 16:32:23,096 INFO [workflow.ScriptWorkflowFunction]: validateAssignResult: com.atlassian.jira.bc.issue.IssueService$AssignValidationResult@1db86722
2019-03-26 16:32:23,098 ERROR [workflow.ScriptWorkflowFunction]: *************************************************************************************
2019-03-26 16:32:23,098 ERROR [workflow.ScriptWorkflowFunction]: Script function failed on issue: null, actionId: 1, file: <inline script>
java.lang.IllegalArgumentException: Issue cannot be null.
 at com.atlassian.jira.issue.fields.layout.field.AbstractFieldLayoutManager.getFieldLayout(AbstractFieldLayoutManager.java:135)
 at com.atlassian.jira.bc.issue.DefaultIssueService.assign(DefaultIssueService.java:557)
 at com.atlassian.jira.bc.issue.IssueService$assign$0.call(Unknown Source)
 at Script462.run(Script462.groovy:49)

Could you help me? :( 

0 votes
Eduard Diez
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.
November 6, 2018

Hey People, can you help me in Cloud?

Because I try but I don't, I need how I do in Cloud, I try to do this:

 

it.actors.any(){
   logger.info("Assignee to: " + it.name)
      def actorname = it.name
          if(actorname!=null && actorname!=""){
               issueInput.update.assignee = [[set: [name: actorname]]] 
               //issueInput.fields.assignee = [name: actorname] as Map
               logger.info("assign ok")
               assigned = true
               return "end"
       }
}

Suggest an answer

Log in or Sign up to answer