It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

ScriptRunner Listener: Copy free-text custom field value into a label custom field

Similar to: https://community.atlassian.com/t5/Jira-Core-questions/Copy-Custom-field-value-from-one-field-to-another/qaq-p/73207

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!

2 answers

2 accepted

1 vote
Answer accepted
Ivan Tovbin Community Leader Mar 06, 2018

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. 

Ivan Tovbin Community Leader Mar 07, 2018

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)

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?

Ivan Tovbin Community Leader Mar 08, 2018

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'.

Ivan Tovbin Community Leader Mar 08, 2018

@Chase Brown

Great news! Please mark the solution as "Accepted" if it resolved your issue.

0 votes
Answer accepted

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);
   }

Suggest an answer

Log in or Sign up to answer
Community showcase
Posted in Jira Core

How to manage many similar workflows?

I have multiple projects that use variations of the same base workflow. The variations depend on the requirements of the project or issue type. The variations mostly come in the form of new statuses ...

884 views 7 0
Join discussion

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you