Assign user from another issue customfield scriptrunner

Oleksiy Ohmush July 24, 2019

Hello!

I have a project that we store some user hierarchy

I try to set assignee in new issue in project TEST from this project, depending on customfield value.

But as a result i  receive null and Assignee is unassigned

 

import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.user.util.UserManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.crowd.embedded.api.User
import com.atlassian.jira.user.ApplicationUsers
import com.atlassian.jira.ComponentManager

// any task where i want to change assignee
IssueManager issueManager = ComponentAccessor.getOSGiComponentInstanceOfType(IssueManager.class);
Issue issue = issueManager.getIssueObject("TEST-429");
log.warn(issue.key+ " id " +issue.id)
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def finId = issue.getCustomFieldValue(customFieldManager.getCustomFieldObject("customfield_15208"))
Node fid = new XmlParser().parseText(finId)
long fcpid = Long.parseLong(fid.value.text());

// issue with
Issue fcpIssue = issueManager.getIssueObject(fcpid);
//fields from fcp
def user = fcpIssue.getCustomFieldValue(customFieldManager.getCustomFieldObject("customfield_15200"))?.username

issue.setAssignee(ComponentAccessor.getUserManager().getUserByName(user))

 

How to use setAssignee properly?

3 answers

1 accepted

1 vote
Answer accepted
Ismael Jimoh
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.
July 24, 2019

Hi @Oleksiy Ohmush 

Can you output the result of the following line to the log and make sure you are getting the value you want?

def user = fcpIssue.getCustomFieldValue(customFieldManager.getCustomFieldObject("customfield_15200"))?.username

Depending on the result of the above, we could confirm if you can set the assignee.

I would start with getting the value of the following:

def finId = issue.getCustomFieldValue(customFieldManager.getCustomFieldObject("customfield_15208"))
Node fid = new XmlParser().parseText(finId)
long fcpid = Long.parseLong(fid.value.text());

Confirm you are getting the correct value first before we proceed any further. I would like to ask why you are not just parsing the issue-key directly from your customfield or something similar.

I will need more context to be able to help you further with the above.

Cheers.

Oleksiy Ohmush July 24, 2019

logs:

WARN [runner.AbstractScriptRunner]: TEST-429 id 193718

WARN [runner.AbstractScriptRunner]: fid= content[attributes={}; value=[value[attributes={}; value=[206189]]]]

WARN [runner.AbstractScriptRunner]: fcpid= 206189

WARN [runner.AbstractScriptRunner]: user prokashik 

Oleksiy Ohmush July 24, 2019

@Ismael Jimoh 

 tried with 

issue.setAssignee(ComponentAccessor.getUserManager().getUserByKey(user))

same result 

Ismael Jimoh
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.
July 25, 2019

Hi @Oleksiy Ohmush 

Try:

issue.setAssignee(ComponentAccessor.getUserManager().getUserByKey(user))

Ensure you persist the value with an issue update event. For example:

assetOwner = userManager.getUserByKey('myuser')
log.warn(assetOwner)
issue.setCustomFieldValue(assetOwnerField, assetOwner)
issueManager.updateIssue(user, issue, EventDispatchOption.ISSUE_UPDATED, false)

The last line ensures that JIRA persists your change in a post-function or event.

Can you give this a try and confirm if this is the same as what you are aiming for?

Oleksiy Ohmush July 26, 2019

I find my problem

def user = fcpIssue.getCustomFieldValue(customFieldManager.getCustomFieldObject("customfield_15200"))?.key
log.warn("user " +user)
issue.setAssignee(ComponentAccessor.getUserManager().getUserByKey(user))
issueManager.updateIssue(user, issue, EventDispatchOption.ISSUE_UPDATED, false

Thank You a lot for help, @Ismael Jimoh 

0 votes
C_ Derek Fields
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.
July 24, 2019

It seems almost certain the the username that is returned from the custom_field is resulting in a value that doesn't return a user from the getUserByName() call. That is where I would focus. Make sure that you are getting a String value that is a username that can be found by the call. 

0 votes
Lady Di
Marketplace Partner
Marketplace Partners provide apps and integrations available on the Atlassian Marketplace that extend the power of Atlassian products.
July 24, 2019

Ready script for our free plugin Mygroovy

 

import com.atlassian.jira.component.ComponentAccessor

//def issue = ComponentAccessor.issueManager.getIssueObject('CUSTOM-17405')
def someField = getCustomFieldValue(issue, 43312) //field id

if (!someField){
issue.setAssignee(ass1)
} else{
if(someField.optionId == 38538){
issue.setAssignee(ass2)

}
}
def getCustomFieldValue(issue, Long fieldId) {
//ComponentAccessor.customFieldManager.getCustomFieldObject(fieldId)?.getValue(issue)
issue.getCustomFieldValue(ComponentAccessor.customFieldManager.getCustomFieldObject(fieldId))
}

 

 

 

 

ps. By the way, here we have scripted the templates of the scripts, the most frequently usecases, just copy and paste what u need. https://github.com/mailru/jira-scripts/tree/master/JIRA%20groovy%20templates

Oleksiy Ohmush July 24, 2019

Thanks, i will try to use this plugin

Suggest an answer

Log in or Sign up to answer