script for copying value of assignee to custom field as multi user picker

Ashkan Malekly
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 23, 2019

Hello

Could any one help me to write a script for copying value of assignee to custom field as multi user picker.

Thanks a lot.

2 answers

2 accepted

1 vote
Answer accepted
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 23, 2019

Hi @Ashkan Malekly ,

Please try this snippet : 

import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.component.ComponentAccessor

def customFieldManager = ComponentAccessor.getCustomFieldManager()

int cfMultiUserId = 13200
def cfMultiUser = customFieldManager.getCustomFieldObject(cfMultiUserId)
def cfMultiUserValue = issue.getCustomFieldValue(cfMultiUser)

cfMultiUser.updateValue(null, issue, new ModifiedValue(cfMultiUserValue, [issue.getAssignee()]), new DefaultIssueChangeHolder())

Antoine

Ashkan Malekly
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 23, 2019

Thanks a lot.

Ashkan Malekly
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 23, 2019

Dear @Antoine Berry 

I test the snippet. But when i use it in post function (Scripted (Groovy) operation on issue (JMWE add-on)), it dose not work but when test Groovy script it works. could you help me ?

Cheers

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 23, 2019

Sure, can you provide a screenshot of the JMWE view ? And maybe the logs ?

Ashkan Malekly
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 23, 2019

@Antoine Berry 

Thanks man

i send you screen shots of my work step by step:

301.png

 

then add post function:

302.pngthen 

303.pngAnd it dose not have any error and even say it is right. But in my issues the post function does not work. When i test the groovy and define specific issue, it works.

cheers

Ashkan Malekly
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 23, 2019

@Antoine Berry  another question is when i filled the custom field with another user IDs before, and test the Groovy and see that all of users cleare and just the peson that is assign to that issue in that status copy to custm filed, i want to add the user to the last users in custom field.

cheers

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 23, 2019

The initial script was for ScriptRunner, I just tried in JMWE, it is working as well but I would add a null checker : 

import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.fields.*

def customFieldManager = ComponentAccessor.getCustomFieldManager()

def assignee = issue.getAssignee()
if (assignee != null){
int cfMultiUserId = 13200
CustomField cfMultiUser = (CustomField)customFieldManager.getCustomFieldObject(cfMultiUserId)
def cfMultiUserValue = issue.getCustomFieldValue(cfMultiUser)

cfMultiUser.updateValue(null, issue, new ModifiedValue(cfMultiUserValue, [issue.getAssignee()]), new DefaultIssueChangeHolder())
}

image.pngI have tried triggering the post function with success too.

Let me know if that worked. :)

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 23, 2019

Oh I see you want to add the current assignee to the users already selected in the field ? Please try this snippet, I think I have covered all the cases and tried it myself : 

import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.component.ComponentAccessor

def customFieldManager = ComponentAccessor.getCustomFieldManager()

def assignee = issue.getAssignee()
if (assignee != null){
int cfMultiUserId = 13200
def cfMultiUser = customFieldManager.getCustomFieldObject(cfMultiUserId)
def cfMultiUserValue = issue.getCustomFieldValue(cfMultiUser)
def cfMultiUserNewValue
if (cfMultiUserValue != null && cfMultiUserValue.size() > 0){
cfMultiUserNewValue = cfMultiUserValue.clone()
if (!cfMultiUserValue.contains(assignee)){
cfMultiUserNewValue << assignee
}
}
cfMultiUserNewValue = cfMultiUserNewValue == null ? [assignee] : cfMultiUserNewValue
if (cfMultiUserValue != cfMultiUserNewValue){
cfMultiUser.updateValue(null, issue, new ModifiedValue(cfMultiUserValue, cfMultiUserNewValue), new DefaultIssueChangeHolder())
}
}

Let me know how that went. 

Antoine

David Fischer
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 23, 2019

Please note that this is not how you're supposed to set field values in JMWE. While it might work in some cases, it is not recommended.

The recommended way to set a field value from a Groovy script (which is still not the recommended way, as using a Set Field Value post-function is easier and more future-proof) is to use the setFieldValue method:

issue.setFieldValue("my field", value)

 And of course, to simply copy from one field to another, see my reply below.

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 23, 2019

Thanks David, I was not sure about that, updateValue works fine but you could replace it with 

issue.setFieldValue(cfMultiUserId , cfMultiUserNewValue)

then. Also I think groovy is more fun than built-in functions and help you exercise for when you need to do more complex things. But I agree it might be a bit overkill sometimes. :)

Ashkan Malekly
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 23, 2019

@Antoine Berry  And @David Fischer Thanks a lot for your quick and useful answer. Both of your suggestion work as well as i thought. Now i find third way in project automation. Finally You are hero.

Thanks a lot.

Like Antoine Berry likes this
0 votes
Answer accepted
David Fischer
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 23, 2019

Hi Ashkan,

you don't need any Groovy script for that. Just use the "Copy Value from Field to Field" post-function, select the source (Assignee) and destination (multi user picker) fields and use the "Add source value(s) to destination field" option.

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events