How to convert custom single select field to text

Tony Mackin February 4, 2018

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

   PagerEmailID:  aaa@xyz.com

                    bbb@xyz.com

                    ccc@xyz.com

 

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

2 votes
Answer accepted
Tony Mackin February 4, 2018

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?

Ivan Tovbin
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.
February 4, 2018

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
Tony Mackin February 4, 2018

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

Ivan Tovbin
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.
February 4, 2018

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

Tony Mackin February 4, 2018

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.    

Ivan Tovbin
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.
February 4, 2018

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)
email.setSubject(subject)
email.setBody(body)
mailServer.send(email)
}
}
sendEmail (emailAddr, subject, body)
Tony Mackin February 7, 2018

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)

screenshot.jpg

 

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 == 'aaa@xyz.com') {
       mail.setTo("aaa@xyz.com")
       return true}
    else {
      if (cfValues['PagerEmailID']?.value == 'bbb@xyz.com') {
           mail.setTo("bbb@xyz.com")
           return true}
       else {
        mail.setTo("ccc@xyz.com")
         return true}
    }
 }
else { return false}  

 

Then this email template is used to control email body content

$issue.issueType.name <a href=${baseUrl}/browse/${issue.key}>${issue.key}</a> has been declared a Sev 1.
<br><br>
<% 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>
<br><br>

 

Thanks again for your help.

Ivan Tovbin
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.
February 8, 2018

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.

 

1 vote
Answer accepted
Ivan Tovbin
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.
February 4, 2018

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?

Jorge Pino
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!
June 3, 2020
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. 

 

Thanks!

Like Daniel Brvnišťan likes this
Ivan Tovbin
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.
June 13, 2020

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
Daniel Brvnišťan
Contributor
January 23, 2021

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)?

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events