However in my case I'm trying to copy a free-text custom field into a label custom field.
E.G. I've got a free-text custom field named "Country" that is auto-filled by a behavior mapped to a local gazetteer that I want to copy to a label custom field named "Country (Index)". The second thing I have to consider is that some values are multi-word... So I believe I would need to convert the value to HTML and substitute spaces with underscores like: "United_States".
Any suggestions would be greatly appreciated!
Hi Chase,
The difference between a free-text custom field and label custom field is that the former contains a string and the latter contains an array of Label objects. And so you need to treat these fields accordingly when setting their respective values.
As per LabelManager Interface documentation, addLabel method seems to be the one you need:
addLabel(ApplicationUser remoteUser, Long issueId, Long customFieldId, String label, boolean sendNotification)
As you can see, you simply have to use your free-text custom field value as the 4th argument of this method and it should do it.
Can you give me an example of how this would be scripted?
E.G.
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.issue
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.label
import com.atlassian.jira.user
def country = getFieldById("Country")
def defaultValue = addLabel(ApplicationUser remoteUser, Long issueId, Long customFieldId, String label, boolean sendNotification)
I think I'm a bit lost, but not far from what I'm trying to accomplish.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Here's how you do it:
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.label.LabelManager
def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def cfMgr = ComponentAccessor.getCustomFieldManager()
def country = issue.getCustomFieldValue(cfMgr.getCustomFieldObjectByName("Country"))
def labelCfId = cfMgr.getCustomFieldObjectByName("your label custom field name").getIdAsLong()
def labelMgr = ComponentAccessor.getComponent(LabelManager)
labelMgr.addLabel(currentUser, issue.getId(), labelCfId, country, false)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Ivan,
Thank you for your support! I see how far off I am now..
ScriptRunner is showing an error with this section:
def country = issue.getCustomFieldValue(cfMgr.getCustomFieldObjectByName("Country"))
The error is "The variable [issue] is undeclared.
What do you suggest?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Oh, my bad. I've just realized that you want a listener and not a post function where 'issue' is pre-declared. But not to worry, it's an easy fix. Simply replace 'issue' in the code with 'event.issue'.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Great news! Please mark the solution as "Accepted" if it resolved your issue.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
That worked but I had to make a few tweaks.
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.label.LabelManager
def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def cfMgr = ComponentAccessor.getCustomFieldManager()
def country = event.issue.getCustomFieldValue(cfMgr.getCustomFieldObjectByName("Country"))toString().replaceAll(" ","_")
def labelCfId = cfMgr.getCustomFieldObjectByName("your label custom field name").getIdAsLong()
def labelMgr = ComponentAccessor.getComponent(LabelManager)
if (country == "null") { }else{
labelMgr.addLabel(currentUser, event.issue.getId(), labelCfId, country, false);
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Chase Brown,
thank you for this! I'm trying to run this as a listener but "import com.atlassian.jira.user" is causing an error:
The script could not be compiled:
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed: Script397.groovy: 13: unable to resolve class com.atlassian.jira.user @ line 13, column 1. import com.atlassian.jira.user ^ 1 error
As I understand, import com.atlassian.jira.user was replaced by Application user, but even if I insert all these into the beginning of the code, it still doesn't work:
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.label.LabelManager
import com.atlassian.jira.security.Permissions
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.user.ApplicationUser;
import com.atlassian.jira.user.util.UserUtil
import com.atlassian.jira.user.UserUtils
com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.user
Do you maybe have an idea how to solve this?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
There is also an option, to auto populate specific field from historical free text field (like summary or description) with ML1 plugin.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.label.LabelManager
def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def cfMgr = ComponentAccessor.getCustomFieldManager()
def country = event.issue.getCustomFieldValue(cfMgr.getCustomFieldObjectByName("Country"))toString().replaceAll(" ","_")
def labelCfId = cfMgr.getCustomFieldObjectByName("your label custom field name").getIdAsLong()
def labelMgr = ComponentAccessor.getComponent(LabelManager)
if (country == "null") { }else{
labelMgr.addLabel(currentUser, event.issue.getId(), labelCfId, country, false);
}
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.label.LabelManager
def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def cfMgr = ComponentAccessor.getCustomFieldManager()
def country = event.issue.getCustomFieldValue(cfMgr.getCustomFieldObjectByName("Country"))toString().replaceAll(" ","_")
def labelCfId = cfMgr.getCustomFieldObjectByName("your label custom field name").getIdAsLong()
def labelMgr = ComponentAccessor.getComponent(LabelManager)
if (country == "null") { }else{
labelMgr.addLabel(currentUser, event.issue.getId(), labelCfId, country, false);
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
thanks! In the meantime I have actually managed to find my own answer based on this thread
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@Michael Virden I have another question though. Do you think the "{ }" in this in the if statement "if (country == "null") { }" could cause a problem? For eg if I filter for the issue created even in the listener where I run the script, eventually there are gonna be cases where it doesn't go into the else branch because the value is null. Is then the empty "{ }" not a problem, does that never create a loop or anything?
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.