Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Next challenges

Recent achievements

  • Global
  • Personal


  • Give kudos
  • Received
  • Given


  • Global

Trophy case

Kudos (beta program)

Kudos logo

You've been invited into the Kudos (beta program) private group. Chat with others in the program, or give feedback to Atlassian.

View group

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

How to convert custom single select field to text

Is there a way to convert the value of a Jira Service Desk custom field that is a single select field to text in scriptrunner? 


Example:   custom field  called  PagerEmailID  has predefined list of emails in it. 

As users open new issues they will select an emailID from a drop-down list





My basic question is how to covert the  PagerEmailID select list custom field to a field that is a string field -  call it  PagerEmailID-Text 

PagerEmailID-Text is a scripted field,  so that it is always current regardless of if the PagerEmailID field is updated after the Issue is opened.



My full use case is I have post-function in a workflow to send out a custom email,  and the TO:  address will use the PagerEmailID-Text field,  since the custom email function can not use a single select type field  for the TO: address and  I can't hardcode the email address

So,  If I can find a way to convert  PagerEmailID (single select field) to  PagerEmailID-Text (String field) I've got the rest of the custom email process worked out.   

Any suggestions would be appreciated,  i found posts on how to convert Text/String field to Single select field, but not the other way around.

2 answers

2 accepted

1 vote
Answer accepted

You can actually get a 'String' value of a single select list option. Can't you use that in you post function or am I missing something here?

import com.atlassian.jira.component.ComponentAccessor

def pagerEmailId = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("PagerEmailID")

return issue.getCustomFieldValue(pagerEmailId).getValue()


Hi @Ivan Tovbin , I am trying to determine why the bold part of the code block is necessary. I can copy code and snippets all day, but I was wondering if you'd be willing to break it down so I that I could better understand when something like that would be needed in the future. In other scenarios, using .getCustomFieldValue('field name') would return the needed value but as you can tell I needed that extra piece to make it work. 



Like Daniel Brvnišťan likes this

Hi @Jorge Pino , here's the deal. The thing with getCustomFieldValue(customField) method is that it returns different types depending on the type of custom field object passed into the method. For example if you call this method on a user picker type field, you'll get an ApplicationUser object. A text field will get you a String, a number field will get you a Double, and so on. 

So in case of a single value select list type field you get an option object. And to get that options value (which returns a String), a getValue() method is called.

Hope this clarifies.

Like Daniel Brvnišťan likes this

Hi @Ivan Tovbin, I used your script with slight modification:

import com.atlassian.jira.component.ComponentAccessor

def myCustomField = ComponentAccessor.getCustomFieldManager().getCustomFieldObject("customfield_13911")

return issue.getCustomFieldValue(myCustomField).getValue()

But I am getting cannot find matching method. Could the issue be that my field is Calculated (scripted) Single-select Field (JMCF app)?

1 vote
Answer accepted

That is my question,  how can I get a string value of a single select list option...   its probably something simple and second nature to you,  but I couldn't find an example of how to put  the string value of  PagerEmailID  into PagerEmailID-Text field.  

Could you suggest the command(s) to do that?

From what I understand, your 'PagerEmailID-Text' is a scripted field. So the code is as follows:

import com.atlassian.jira.component.ComponentAccessor

def pagerEmailId = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("PagerEmailID")

return issue.getCustomFieldValue(pagerEmailId).getValue()

I'm not sure what kind of post function you are trying to use, but I suppose you can pass it your 'PagerEmailID' field value directly without having to use an "in between" custom field. If you can give more details I might be able to help.  

Like Jorge Pino likes this

Thanks - Yes the script above does properly set the scripted field to the text value of the single select field.


However,  now I find the the scripted field doesn't show up as an option in the create custom email  selection for to fields as I thought it would.


Here is what I'm trying to do.. 

When an Issue is raised from a non-Sev1 (ie.  Sev 2,  Sev 3)  to a Sev 1,   send an email to a service that will send a page/text out.    The email address will vary depending on who owns the issue (DBA team, Network team...etc).


So, I create a workflow transition called  "Raise to Sev 1" 

When the user hits the "Raise to Sev 1" button another screen will appear for the user to enter a comment and select the Pager Email to use

Then I have post-functions to

 1)  update the Priority field to Sev 1

 2)  send a custom email

       The customer email is simple it will just say Issue  XYX  has been declared a Sev 1.    But I'm stuck right now on how to dictate what email address to use since it could vary.   The email address the user selected would be in the PagerEmailID select field,  but that field is not one is provided as option on  To address selection (it wants a user-picker field or group id)

So that is my full use case...

Now, I'm experimenting now with adding a script in the condition/configure part of the custom email feature,  to set the mail.setTo field to a given string based on valuue of the PagerEmailID field

I take it you are using some 3rd party addon to send custom emails via post function?

Yes,  Scriptrunner  -  you can setup a post-function to run a scriptrunner script and  one of the Scriptrunner option is to send a custom email.     So that is what am trying to use.


Have also combination of newly defined Event  and Notifications  to send email to the Pager server,  but can't vary the email address.    In that approach,  have post-function on a transition to "fire" the new Event I created,   and have Notification setup to send email to a specific email,  but doesn't fulfill my requirements to vary email address the email goes too.   I have multiple support teams using the JSD Project and each support has a different PagerDuty email that will send and manage page-outs to their team.    

I see. Well then I suppose writing a custom script which will send an email rather than using a built-in one is the solution here. This will also allow you to get the destination address directly from your select list which will dispense your from having to use any additional custom fields.

Try this:

import com.atlassian.mail.Email
import com.atlassian.jira.component.ComponentAccessor

def pagerEmailId = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("PagerEmailID")
def emailAddr = issue.getCustomFieldValue(pagerEmailId).getValue()
def subject = "Issue ${issue.getKey()} has been declared a Sev 1".toString()
def body = "Issue ${issue.getKey()} has been declared a Sev 1".toString()

def sendEmail(String emailAddr, String subject, String body) {
def mailServer = ComponentAccessor.getMailServerManager().getDefaultSMTPMailServer()

if (mailServer) {
def email = new Email(emailAddr)
sendEmail (emailAddr, subject, body)

Thanks Ivan for the script above...

I got some compile errors on the def emailAddr  line above.  See screenshot attached.   (I added some lines to add log entries to help debug)



However,  if I hard coded the emailAddr value to my email,  the script above does work to send an email   and could be an alternative to using the scriptrunner  'send custom email' function.

I ended up changing approaches so  I didn't need to convert the (email) value in a select field to a string to be able to use it send an email.


I set the mail.setTo field to  a certain value based on another custom field.


Not exactly what I'm doing since I'm using another custom field to decide what email addr in reality,  but in keeping with my example above  the script is to determine if email should be sent,  and sets where it should go is...

// If Sev1 send email to either  team A , team  B  or Team C inbox

if (issue.priority?.name == "Sev 1") {
   if (cfValues['PagerEmailID']?.value == '') {
       return true}
    else {
      if (cfValues['PagerEmailID']?.value == '') {
           return true}
       else {
         return true}
else { return false}  


Then this email template is used to control email body content

$ <a href=${baseUrl}/browse/${issue.key}>${issue.key}</a> has been declared a Sev 1.
<% if (issue.summary != null)
    out << "Summary: " << issue.summary << "<br><br>"
<% if (issue.description != null)
    out << "Description: " << issue.description << "<br><br>"
<% out << "Assignment Group: " << issue.getCustomFieldValue(com.atlassian.jira.component.ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Assignment Group")) << "<br><br>"
<% if (lastComment)
    out << "Last comment: " << lastComment << "<br><br>"
<i>Issue Submitted by <% out << issue.reporter?.displayName %></i>


Thanks again for your help.

Check out this documentation to learn more about static type checking in Scriptrunner. 

Thing is, static type checking in ScriptRunner is not always accurate and thus can be ignored at times.

In this very case you can pretty much ignore this error, it'll work just fine as is.


Suggest an answer

Log in or Sign up to answer
Community showcase
Published in Jira Service Management

Why upgrade to Jira Service Management Premium?

We often have questions from folks using Jira Service Management about the benefits to using Premium. Check out this video to learn how you can unlock even more value in our Premium plan.  &nb...

235 views 1 6
Read article

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