Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

What is the correct script to copy a label field value from one issue to another?

Lara Arthur July 9, 2024

Hi there community.

I am trying to copy over the value of a label cf called 'Support Request Label' from the Epic to a Support Request within a behaviour initialiser. image.png

Although technically the script I have is copying the field value...it is adding brackets. Is there anyone out there that know how to get rid of these? 

Any help is appreciated.

Epic create screen: image.png

Script (partial) behind create screen pop up after clicking on Request Support:

 

//create screen when creating from IT project

if (getBehaviourContextId() == "link-create-support-request-IT") {

    getFieldById("project-field").setReadOnly(true)

    getFieldById("issuetype-field").setReadOnly(true)

   

    def contextIssue = issueManager.getIssueObject(getContextIssueId())

    getFieldById("issuelinks-linktype").setFormValue("epics requiring support").setReadOnly(true)

    getFieldById("issuelinks-issues").setFormValue(contextIssue.key).setReadOnly(true)

    //below does copy the value, but adds brackets

    //getFieldById("customfield_18300").setFormValue("${contextIssue.getCustomFieldValue("Support Request Label")}").setReadOnly(true)

    //below adds test without brackets but this is not what I want

    //getFieldById("customfield_18300").setFormValue("test")

    //below does copy the value, but brings in brackets as well

    getFieldByName("Support Request Label").setFormValue(contextIssue.getCustomFieldValue("Support Request Label").toString())

}

Result: 

image.png

1 answer

1 vote
Radek Dostál
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.
July 10, 2024
contextIssue.getCustomFieldValue("Support Request Label")

returns a Set<Label> (https://docs.atlassian.com/software/jira/docs/api/9.12.8/com/atlassian/jira/issue/label/Label.html), so the brackets are coming from the .toString() method because it is converting a set(=collection) to String

 

I don't know the format for setting labels in behaviours, so might want to experiment with it a bit to get it to work for multiple labels, but if you want to confirm this on this particular case where you only have a single element in the set, you could do

getFieldByName("Support Request Label").setFormValue(contextIssue.getCustomFieldValue("Support Request Label")?.first()?.getLabel())

 

And that should go without brackets.

If you want to handle more cases, I'd say it would be better to first store the value as a variable

Set<Label> labels = (Set<Label>) contextIssue.getCustomFieldValue("Support Request Label")

 

And then handle it in some way such as

if (labels != null && !labels.isEmpty()) {
// has values
List<String> labelValues = labels.stream().map(l -> l.getLabel()).collect(Collectors.toList())

for (String labelValue : labelValues) {
// do something per value
}

// or do something with the list, such as
String spaceDelimitedLabelValues = String.join(" ", labelValues)

}
else {
// null or no values so whatever
}

 

Again though I don't know the exact format with labels, would need to find some time to play around with it but if you have a test case already it's probably better for you to experiment with it a bit to know how it works in practice.

 

Lara Arthur
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
December 11, 2024

This worked. Thanks so much for your explanation! Appreciate your help.

Suggest an answer

Log in or Sign up to answer