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

Tony Mackin Feb 04, 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

0 votes
Answer accepted
Ivan Tovbin Community Leader Feb 04, 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?

0 votes
Answer accepted
Tony Mackin Feb 04, 2018 • edited

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 Community Leader Feb 04, 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.  

Tony Mackin Feb 04, 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 Community Leader Feb 04, 2018 • edited

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

Tony Mackin Feb 04, 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 Community Leader Feb 04, 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 Feb 07, 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 Community Leader Feb 08, 2018 • edited

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
This widget could not be displayed.
This widget could not be displayed.
Community showcase
Published in Jira Service Desk

4 changes to queues and issues you don’t want to miss

Hello Atlassian Community! I'm Tania, a Senior Product Marketing Manager for Jira Service Desk Cloud! I'm excited to announce some exciting improvements coming up for those who work with queues and...

654 views 9 13
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