Change the assignee field from a custom field (user picker)

Karim Belhadj April 1, 2019

hello team 

I would like to filfil the system field assignee from a custom field , type of custom field = User picker . so i find trhis code but it work here , but when i put it in a post function , the assignee does not change  .

 

Image 2.png

 

i need your help.

 

Regards

3 answers

1 vote
Antoine Berry
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
April 1, 2019

Hi,

This snippet should work fine : 

def customFieldManager = ComponentAccessor.getCustomFieldManager()

//Jira User picker ID
int userPickerId = 10013
def cfUserPicker = customFieldManager.getCustomFieldObject(userPickerId)
def cfUserPickerValue = issue.getCustomFieldValue(cfUserPicker)

issue.setAssignee(cfUserPickerValue)

If it does not, add some logs to check where it is failing.

Antoine 

Karim Belhadj April 1, 2019

hi @Antoine Berry .

Thank you , yes there are always the same problem , i make some log but it return coorect result , but when i go to the issue there are always the same user assignee , it does not change .

Image 6.png

 

 

 

i can not understand what's the problem , also there are a warn appear , in the picture .

 

 

Regards

Antoine Berry
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
April 1, 2019

Don't worry about the WARN, it shows because we did not specify the type of cfUserPickerValue. But it is groovy so it is not a problem, as you can see it executes correctly. 

Did the assignee change ?

Karim Belhadj April 1, 2019

@Antoine Berry  no the assignee do not change , it still the same

Antoine Berry
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
April 1, 2019

Weird, this is working fine with me.

Can you add 

def issueIndexingService = ComponentAccessor.getComponent(IssueIndexingService)
issue.store()
issueIndexingService.reIndex(issue)

at the end ? See if that helped and provide a screenshot again ?

Thanks

Karim Belhadj April 1, 2019

there are another problem appear 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue
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.jira.user.ApplicationUser
def issueManager = ComponentAccessor.issueManager
def customFieldManager = ComponentAccessor.customFieldManager
import com.atlassian.jira.user.util.UserManager

def issue = ComponentAccessor.getIssueManager().getIssueByCurrentKey("AB-1");
//Jira User picker ID
int userPickerId = 10013
def cfUserPicker = customFieldManager.getCustomFieldObject(userPickerId)
log.warn("customfield " + cfUserPicker )
def cfUserPickerValue = issue.getCustomFieldValue(cfUserPicker)
log.warn("current assignee == " + issue.assignee )


issue.setAssignee(cfUserPickerValue)
log.warn(" new assigneee = " + cfUserPickerValue )

def issueIndexingService = ComponentAccessor.getComponent(IssueIndexingService)
issue.store()
issueIndexingService.reIndex(issue)

 

 

Image 7.png

 

regards

Antoine Berry
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
April 1, 2019

Ah, no big deal, just an import missing. Try

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue
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.jira.user.ApplicationUser
import com.atlassian.jira.issue.index.IssueIndexingService

def issueManager = ComponentAccessor.issueManager
def customFieldManager = ComponentAccessor.customFieldManager
import com.atlassian.jira.user.util.UserManager

def issue = ComponentAccessor.getIssueManager().getIssueByCurrentKey("AB-1");
//Jira User picker ID
int userPickerId = 10013
def cfUserPicker = customFieldManager.getCustomFieldObject(userPickerId)
log.warn("customfield " + cfUserPicker )
def cfUserPickerValue = issue.getCustomFieldValue(cfUserPicker)
log.warn("current assignee == " + issue.assignee )


issue.setAssignee(cfUserPickerValue)


def issueIndexingService = ComponentAccessor.getComponent(IssueIndexingService)
issue.store()
issueIndexingService.reIndex(issue)
log.warn(" new assigneee = " + issue.assignee )

I added the value of the assignee at the end so we can see if it updated successfully.

Karim Belhadj April 1, 2019

now it works fine thank you

0 votes
Alexander April 17, 2019

Hi @Karim Belhadj 
Try this:

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.event.type.EventDispatchOption;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.user.ApplicationUser;

CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager();

def issue = ComponentAccessor.issueManager.getIssueByCurrentKey('AB-1'); 

def cField = customFieldManager.getCustomFieldObjectByName("Your cf name"); // You can use ID
ApplicationUser user = issue.getCustomFieldValue(cField) as ApplicationUser;
issue.setAssignee(user);
ComponentAccessor.getIssueManager().updateIssue(user, issue, EventDispatchOption.ISSUE_UPDATED, false);

Give me an answer about the result!

Good luck!

 

Regards

0 votes
Alexander Pappert
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.
April 1, 2019

You want to fill Assignee from a custom user picker field, when a specific Transition/status is reached (i.e. Task is opened, set to V&V, ...)?

there are several addons on Marketplace which help you to do this.

a free one is for example https://marketplace.atlassian.com/apps/1211836/automation-lite-for-jira?hosting=server&tab=overview

https://docs.automationforjira.com/getting-started/actions.html#add-service-desk-customer

Karim Belhadj April 1, 2019

@Alexander Pappert  i need this with a sample code script runner , i do not ask for addon , thank you.

Suggest an answer

Log in or Sign up to answer