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.
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
Thanks a lot.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Sure, can you provide a screenshot of the JMWE view ? And maybe the logs ?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thanks man
i send you screen shots of my work step by step:
then add post function:
then
And 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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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())
}
I have tried triggering the post function with success too.
Let me know if that worked. :)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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. :)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.