Forums

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

How can I move a user from a custom text field to assignee field?

Tim Perrault
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.
December 14, 2022

I have a script that grabs the value of a text field, which is the user id of the user. For example the text field has a value of 432100. Before the changes for gdpr the script worked great, in fact it still works great for users that were created before the change. Now any user created after the change, the assignee field updates with the numbers but doesn't find the user. The assignee field will just have 432100 - ?. How would I fix this? Please be gentle, I don't really code :) Script below:

 

import com.atlassian.jira.component.ComponentAccessor


// Get required Managers

def customFieldManager = ComponentAccessor.getCustomFieldManager()

// get a pointer to the Implemented By user picker field and its value

def userCf = ComponentAccessor.customFieldManager.getCustomFieldObjects(issue).findByName("Project Lead")

def implUser = issue.getCustomFieldValue(userCf)

// If the Implemented By Field is not null then set the assignee

if(implUser){

    issue.assigneeId = implUser

}

1 answer

1 accepted

0 votes
Answer accepted
Florian Bonniec
Community Champion
December 14, 2022

Hi @Tim Perrault 

 

You have to get the object that represent the user instead the user id. You can do it using userManager class.

Base on the code available here

I've create a script that I've not tested but should work. Btw, the website mention have lot of usecase you can use.

 

import com.atlassian.jira.component.ComponentAccessor

import com.atlassian.jira.issue.IssueInputParametersImpl

import com.atlassian.jira.event.type.EventDispatchOption

// Get required Managers

def customFieldManager = ComponentAccessor.getCustomFieldManager()

// get a pointer to the Implemented By user picker field and its value

def userCf = ComponentAccessor.customFieldManager.getCustomFieldObjects(issue).findByName("Project Lead")

String implUser = issue.getCustomFieldValue(userCf)

//Dependency to save issue update

def issueService = ComponentAccessor.issueService

def loggedInUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser

// If the Implemented By Field is not null then set the assignee

if(implUser){

    def userManager = ComponentAccessor.getUserManager()

    def user = userManager.getUserByName(implUser)

    if(user){

        def issueInputParameters = new IssueInputParametersImpl()

        issueInputParameters.setAssignee(user)

        def updateValidationResult = issueService.validateUpdate(loggedInUser, issue.id, issueInputParameters)

        assert updateValidationResult.valid : updateValidationResult.errorCollection

        def issueUpdateResult = issueService.update(loggedInUser, updateValidationResult, EventDispatchOption.ISSUE_UPDATED, sendMail)

        assert issueUpdateResult.valid : issueUpdateResult.errorCollection

    }

}

 

 

Regards

Tim Perrault
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.
December 15, 2022

I kept on getting errors from your script about the object type, but you got me going down the right path. Basically needed to turn the string into an application user. Thanks for the help. I was able to get it working with the code below:

 

import com.atlassian.jira.component.ComponentAccessor

// Get required Managers

def customFieldManager = ComponentAccessor.getCustomFieldManager()

// get a pointer to the Implemented By user picker field and its value

def userCf = ComponentAccessor.customFieldManager.getCustomFieldObjects(issue).findByName("Project Lead")
String implUser = issue.getCustomFieldValue(userCf)

//Dependency to save issue update


def
issueService = ComponentAccessor.issueService
def loggedInUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def user = ComponentAccessor.userManager.getUserByName(implUser)

// If the Implemented By Field is not null then set the assignee
     if(implUser){
      issue.setAssignee(user)
       }

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
SERVER
VERSION
8.22.6
TAGS
AUG Leaders

Atlassian Community Events