Merge multiple select list into one text field

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

Accepted Answer
1 vote
Vasiliy Zverev Community Champion Nov 03, 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)

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

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 Community Champion Nov 03, 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.

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 Community Champion Nov 06, 2016

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

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 Community Champion Nov 06, 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)

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 Community Champion Nov 06, 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)

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 Community Champion Nov 07, 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)

Suggest an answer

Log in or Sign up to answer
Community showcase
Posted Sep 18, 2018 in Jira

What modern development practices are at the heart of how your team delivers software?

Hey Community mates! Claire here from the Software Product Marketing team. We all know software development changes rapidly, and it's often tough to keep up. But from our research, we've found the h...

24,905 views 2 7
Join discussion

Atlassian User Groups

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

Find a group

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

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you