Merge multiple select list into one text field

Deleted user November 2, 2016

I have multiple select list that i would like to merge into one textfield via post function on issue creation. 

This is the code i currently have: 

import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.MutableIssue

def grt = customFieldManager.getCustomFieldObjects(sourceIssue).find {it.name == 'Greeting'}
def grtval = Issue.getCustomFieldValue(grt)

def feedb = customFieldManager.getCustomFieldObject(10636)
def feedbval = Issue.getCustomFieldValue(feedb)

issue.setCustomFieldValue(feedbval,grtval)

 

im fairly new to scriptrunner, any help would be greatly appreciated. 

 

Thanks. 

 

1 answer

1 accepted

1 vote
Answer accepted
Vasiliy Zverev
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.
November 3, 2016

Here you are. I assume that "Greeting" is source multiselect field and 10636 is id for text field:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.CustomFieldManager

import com.atlassian.jira.issue.MutableIssue

//Comment into post-function
MutableIssue issue = ComponentAccessor.getIssueManager().getIssueObject("issue key")

StringBuilder result = new StringBuilder()
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()

for(String value: (ArrayList) issue.getCustomFieldValue(customFieldManager.getCustomFieldObjectByName("Greeting"))){
    result.append(value)
}

//Comment it into postfunction
issue.setCustomFieldValue(customFieldManager.getCustomFieldObject(10636), result.toString())
ComponentAccessor.getIssueManager().updateIssue(
        ComponentAccessor.getJiraAuthenticationContext().getUser().getDirectoryUser()
        , issue
        , EventDispatchOption.ISSUE_UPDATED
        , false)
Deleted user November 3, 2016

Thank yo so much Vasily. 

 

I just tried this and got the following error: 

 

Error!
Cannot invoke method getCustomFieldValue() on null object


java.lang.NullPointerException: Cannot invoke method getCustomFieldValue() on null object
	at Script3839.run(Script3839.groovy:13)

Any solutions to get around this?

 

just a note, i need to merge multiple select list but in the code example i only have one select list which is "Greeting". Another select list to need to merge is called "Privacy".

 

The purpose of this is for Quality assurance, when we QA a call, we require to select set criterias that the call has met in the select list field, and require this to be populated into a text field.

 

Many thanks, 

Pon

Deleted user November 3, 2016

Ahh, i removed the extra closing bracket in line 13 and now i have a new error: 

 

Error!

startup failed: Script3874.groovy: 19: expecting ')', found 'issue' @ line 19, column 1. issue.setCustomFieldValue(customFieldManager.getCustomFieldObject(10636),'') ^ 1 error

org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed: Script3874.groovy: 19: expecting ')', found 'issue' @ line 19, column 1. issue.setCustomFieldValue(customFieldManager.getCustomFieldObject(10636),'') ^ 1 error

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.CustomFieldManager
 
import com.atlassian.jira.issue.MutableIssue
 
//Comment into post-function
MutableIssue issue = ComponentAccessor.getIssueManager().getIssueObject("issue key")
 
StringBuilder result = new StringBuilder()
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
 
for(String value: (ArrayList) issue.getCustomFieldValue(customFieldManager.getCustomFieldObjectByName("Greeting")){
    result.append(value)
}
 
//Comment it into postfunction
    
issue.setCustomFieldValue(customFieldManager.getCustomFieldObject(10636),result.toString())
ComponentAccessor.getIssueManager().updateIssue(
        ComponentAccessor.getJiraAuthenticationContext().getUser().getDirectoryUser()
        , issue
        , EventDispatchOption.ISSUE_UPDATED
        , false)

 

smile Cheers

Vasiliy Zverev
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.
November 3, 2016

Note that issue is set here: 

//Comment into post-function
MutableIssue issue = ComponentAccessor.getIssueManager().getIssueObject("issue key")
Replace "issue key" with actual issue key into script console or comment it for post-function.
Deleted user November 6, 2016

Vasilym, thank you so much for helping me. 

What im trying to do is that, post function on create the customfield_10636 should populate from what ever is chosen from the select list "Greeting".

So i cant hard code the issue key in or are you referring to the issue type?

Many thanks, 

Pon

 

 

Vasiliy Zverev
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.
November 6, 2016

Comment line to get issue into a post-function. See first comment. This is for script console smile

Deleted user November 6, 2016

wow THANK YOU!!! it works smile Just one issue, i've merged a new select list into the code. 

 

The string value is comming up next to each other without space e.g "Cannot be foundCan be found"

 

Is there a way i can add the customfield title and then a new line for the new customfield string.

E.G 

Greeting: Cannot be found

Privacy: Can be found

This is the code i currently have: 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.CustomFieldManager
 
StringBuilder result = new StringBuilder()
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
 
for(String value: (ArrayList) issue.getCustomFieldValue(customFieldManager.getCustomFieldObjectByName("Greeting"))){
    result.append(value)
}
for(String value: (ArrayList) issue.getCustomFieldValue(customFieldManager.getCustomFieldObjectByName("Privacy"))){
    result.append(value)
}
 
//Comment it into postfunction
issue.setCustomFieldValue(customFieldManager.getCustomFieldObject(10636),result.toString())
ComponentAccessor.getIssueManager().updateIssue(
        ComponentAccessor.getJiraAuthenticationContext().getUser().getDirectoryUser()
        , issue
        , EventDispatchOption.ISSUE_UPDATED
        , false)

 

Thank you so much Vasiliy, your a great help and im learning so much!

Cheers, 

Pon

Vasiliy Zverev
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.
November 6, 2016

Here you are. I also add some checks for empty values of custom fields:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.CustomFieldManager

StringBuilder result = new StringBuilder()
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()

List<String> stringList = new ArrayList<>();

stringList = (ArrayList) issue.getCustomFieldValue(customFieldManager.getCustomFieldObjectByName("Greeting"));
if(stringList != null)
    for (String value : stringList) {
        result.append("Greeting:").append(value).append("/n")
    }

stringList = (ArrayList) issue.getCustomFieldValue(customFieldManager.getCustomFieldObjectByName("Privacy"))
if(stringList != null)
    for (String value : stringList) {
        result.append("Privacy").append(value)
    }

//Comment it into postfunction
issue.setCustomFieldValue(customFieldManager.getCustomFieldObject(10636),result.toString())
ComponentAccessor.getIssueManager().updateIssue(
        ComponentAccessor.getJiraAuthenticationContext().getUser().getDirectoryUser()
        , issue
        , EventDispatchOption.ISSUE_UPDATED
        , false)
Deleted user November 6, 2016

Wow Vasiliy your amazing! Thank you so much it work. 

I dont want to bother so much, but i keep trying and faily, i have a customfield called "Call Recording Ref" and its a single line free text. i also want to merge this into the multiline text field along with the select list array.

 

I tried adding it to the string list but it didnt work, any suggestion?

 

smile

Vasiliy Zverev
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.
November 6, 2016

It should work:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.CustomFieldManager

StringBuilder result = new StringBuilder()
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()

List<String> stringList = new ArrayList<>();

stringList = (ArrayList) issue.getCustomFieldValue(customFieldManager.getCustomFieldObjectByName("Greeting"));
if(stringList != null)
    for (String value : stringList) {
        result.append("Greeting:").append(value)
    }

stringList = (ArrayList) issue.getCustomFieldValue(customFieldManager.getCustomFieldObjectByName("Privacy"))
if(stringList != null)
    for (String value : stringList) {
        result.append("/nPrivacy").append(value)
    }

result.append("/nCall Recording Ref").append(issue.getCustomFieldValue(customFieldManager.getCustomFieldObjectByName("Call Recording Ref")));

//Comment it into postfunction
issue.setCustomFieldValue(customFieldManager.getCustomFieldObject(10636),result.toString())
ComponentAccessor.getIssueManager().updateIssue(
        ComponentAccessor.getJiraAuthenticationContext().getUser().getDirectoryUser()
        , issue
        , EventDispatchOption.ISSUE_UPDATED
        , false)
Deleted user November 7, 2016

Fantastic, thank you millions!

Another challenge i found is that when i select multiple lines in the select list example. ctrl+click on multiple string in the select list it only pulls the title in a new line, see below: 

Privacy: Yes
Privacy: Did not confirm ID check at call transfer
Privacy: Did not ask for PIN number

Instead of this, is there a way to pull the string for that custom field in this format ==

Privacy: Yes, Did not confirm ID check at call transfer, Did not ask for PIN number

 

Any work around for this??

You amazing smile

 

Thanks

Vasiliy Zverev
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.
November 7, 2016

Here you are

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.CustomFieldManager

StringBuilder result = new StringBuilder()
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()

List<String> stringList = new ArrayList<>();

stringList = (ArrayList) issue.getCustomFieldValue(customFieldManager.getCustomFieldObjectByName("Greeting"));
if(stringList != null)
    for (String value : stringList) {
        result.append("Greeting:").append(value)
    }

stringList = (ArrayList) issue.getCustomFieldValue(customFieldManager.getCustomFieldObjectByName("Privacy"))
if(stringList != null) {
    result.append("/nPrivacy")
    for (String value : stringList) {
        result.append(" ").append(value)
    }
}

result.append("/nCall Recording Ref").append(issue.getCustomFieldValue(customFieldManager.getCustomFieldObjectByName("Call Recording Ref")));

//Comment it into postfunction
issue.setCustomFieldValue(customFieldManager.getCustomFieldObject(10636),result.toString())
ComponentAccessor.getIssueManager().updateIssue(
        ComponentAccessor.getJiraAuthenticationContext().getUser().getDirectoryUser()
        , issue
        , EventDispatchOption.ISSUE_UPDATED
        , false)
Doug Kaplan November 12, 2018

Hi Vasiliy!

I'm finding this thread several years later but what you've helped with here is exactly what I wanted!  Can you help me a bit with my request?  I want to combine several custom fields into the summary.  Here are my custom fields:

Block Status
Block Start Date/Time
Block Length
Advertiser Name
Device Type
AE
CSM

I want all of these joined with a " - " and have the summary field be updated with this.

I am asking here because while I am definitely a huge fan of JIRA and i'm pushing it to it's limits, my experience is very limited technically so my ability to write or modify script is next to non existent.  Are you able to help with this?

Thanks if you can and let me know if you need any further information!

- DAK

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events