script runner: send custom email: To Issue Fields

Darren Weaver March 23, 2016

Currently we have a custom field called Primary Contact where the first drop-down contains a contact name and the second drop-down contains the email address

image2016-3-23 10:50:53.png

I'm trying to use script runner to send a custom email to the email addresses listed in the second drop-down.

 

The condition is as follows:

image2016-3-23 10:56:23.png

I'm just not sure what to put in the "To issue fields".  Do I need to find the custom field number?

image2016-3-23 10:57:35.png

 

 

2 answers

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

1 vote
Answer accepted
adammarkham
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.
March 23, 2016

The cascading select is not supported out of the box. You can only do this on text, multi user, multi group, and participants custom fields. Which seem to be nicer to select from than a cascading select option. You would enter the custom field id in the "To issue fields" box, example: "customfield_12345".

To get the custom field id you can do the following in the script console:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager

def customFieldManager = ComponentAccessor.getComponent(CustomFieldManager)
def customFieldId = customFieldManager.getCustomFieldObjectByName("Primary Contact").id
customFieldId

If you still want to use a cascading select you could write you own custom listener/post-function and modify this script for your needs:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.onresolve.scriptrunner.canned.jira.workflow.postfunctions.SendCustomEmail

def customFieldManager = ComponentAccessor.getComponent(CustomFieldManager)
def customField = customFieldManager.getCustomFieldObjectByName("Primary Contact")
def emailAddress = issue.getCustomFieldValue(customField)?.get("1")?.value

if (!emailAddress) {
    def params = [
        "issue"                                       : issue,
        (SendCustomEmail.FIELD_EMAIL_TEMPLATE)        : "template",
        (SendCustomEmail.FIELD_EMAIL_SUBJECT_TEMPLATE): "subject",
        (SendCustomEmail.FIELD_TO_ADDRESSES)          : emailAddress,
        (SendCustomEmail.FIELD_EMAIL_FORMAT)          : "HTML"
    ]

    def sendCustomEmail = new SendCustomEmail()
    if (!sendCustomEmail.doValidate(params, false).hasAnyErrors()) {
        sendCustomEmail.doScript(params)
    }
}
Darren Weaver March 23, 2016

How do I tie the listener into the post condition using script runner?

adammarkham
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.
March 23, 2016

I didn't realise you were doing this in a post function rather than a listener. Not to worry it applies for both, you just need to select "Custom script post-function" instead of the email one and put this script in the inline script. More documentation is here if you want to perform validation etc to disallow the transition. https://scriptrunner.adaptavist.com/latest/jira/custom-workflow-functions.html#_validators

Darren Weaver March 23, 2016

Thanks for the help.  so i would copy what I have in subject template to replace "subject" in your script, correct?

image2016-3-23 14:9:15.png

adammarkham
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.
March 23, 2016

Yes thats correct, you also need to edit "template" with the "Email template" you have. That should be about it.

Darren Weaver March 23, 2016

This is great information.  I'm getting an error:

image2016-3-23 14:15:44.png

 

Darren Weaver March 23, 2016

Also, where do I put my conditions for triggering the email?

image2016-3-23 14:17:50.png

adammarkham
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.
March 23, 2016

It should work even with the error, this is just a static type checking issue. I have edited the original script just now to have the condition... So the email will only be sent if there is an email address in the select list. Hope this helps.

adammarkham
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.
March 23, 2016

The "if (! emailAdress)" should be "if (emailAddress)"

Darren Weaver March 23, 2016

you mean "if (!emailAddress) right?

Darren Weaver March 23, 2016

Adam, I did not get an email.  Here is the whole script:

import com.atlassian.jira.component.ComponentAccessor

import com.atlassian.jira.issue.CustomFieldManager

import com.onresolve.scriptrunner.canned.jira.workflow.postfunctions.SendCustomEmail

 

def customFieldManager = ComponentAccessor.getComponent(CustomFieldManager)

def customField = customFieldManager.getCustomFieldObjectByName("Primary Contact")

def emailAddress = issue.getCustomFieldValue(customField)?.get("1")?.value

 

if (!emailAddress) {

    def params = [

        "issue"                                       : issue,

        (SendCustomEmail.FIELD_EMAIL_TEMPLATE)        : "New issue ${issue.key} has been created on your behalf in the Andesa Item Resolution System (AIRS). Andesa Services Issue Description: ${issue.summary}. This email has been automatically generated and does not require a response. If you have any questions concerning this email, please contact your Andesa Services Client Service Representative",

        (SendCustomEmail.FIELD_EMAIL_SUBJECT_TEMPLATE): "New Issue Created ${issue.key}",

        (SendCustomEmail.FIELD_TO_ADDRESSES)          : "emailAddress",

        (SendCustomEmail.FIELD_EMAIL_FORMAT)          : "HTML"

    ]

 

    def sendCustomEmail = new SendCustomEmail()

    if (!sendCustomEmail.doValidate(params, false).hasAnyErrors()) {

        sendCustomEmail.doScript(params)

    }

}

adammarkham
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.
March 23, 2016

You need to replace the "if (!email Address)" with "if(emailAddress)" as said before.

I can't edit the script in my answer because it's already been accepted. 

Darren Weaver March 23, 2016

apologies, the email comes through now but without the ${issue.key}.  Is the syntax correct?

Darren Weaver March 23, 2016

It didn't work.  I tried both.  what's strange is the second ${issue.summary} works fine.  I'm so frustrated.  Thanks for working this out with me.

adammarkham
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.
March 23, 2016

Sorry, the double quotes like you had originally is the right syntax. I just tested your script and it seems to work. In the script do something like: 

log.warn("${issue.key}")
log.warn(issue.key)

and you should see the key in both cases, then try with a simpler template and build it up.

Darren Weaver March 24, 2016

Just an FYI where I am at this point.  The email is producing but I'm losing the reference to variable $issue.key.  I know that this works because I used it in another script using the send custom email scriptrunner. 

It's definitely an issue with this variable, I'm just now sure why/how.

Here is the email I received:

D:\Users\dweaver\Documents\ATLASSIAN\JIRA-dev New Issue Created null - Message.png

Darren Weaver March 24, 2016

adammarkham
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.
March 24, 2016

Is your post function being run on issue create?

Darren Weaver March 24, 2016

Yes it is. 

Darren Weaver March 24, 2016
2016-03-24 13:09:03,500 ERROR [workflow.ScriptWorkflowFunction]: *************************************************************************************
2016-03-24 13:09:03,500 ERROR [workflow.ScriptWorkflowFunction]: Script function failed on issue: null, actionId: 1, file: <inline script>
java.lang.NullPointerException: Cannot get property 'issue' on null object
	at Script396.run(Script396.groovy:6)
Darren Weaver March 24, 2016

OMG ADAM, so I thought a bit about the ordering of the existing events in the post conditions and placed the scripts at the bottom so they run after create issue and now it has worked!!! Something so simple and obvious- palm to head for me. 

adammarkham
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.
March 24, 2016

Sorry, yeah the ordering matters in create. Glad you found the solution.

Darren Weaver March 24, 2016

Thank you SO much Adam for your patience with a groovy noob.  You have been great.  I wish I could give you 100 points for some Atlassian swag. 

0 votes
Kristian Walker _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
March 23, 2016

Hi Darren,

In order to specify the send a custom email Post Function provided by ScriptRunner then you will need to specify the field by ID using a syntax similar to customfield_12004.

However I notice above you using a cascading select list which would store 2 values in a format such as kristian, testemail@test.com which would not provide the to field with a valid email address.

I would recommend creating a single select list field called contact email which can be referenced by the customfield_id in order to provide the email address to send the email to. An example of this would be configured in JIRA 6.4.12 with ScriptRunner 4.1.3.10 is shown below.

image2016-3-23 16:46:58.png

I hope this helps.

Thanks

Darren Weaver March 23, 2016

Thanks Kristian, unfortunately there are groupings of email addresses "filtered" by Primary Contact selection.  So for example, if I select "me" from the Primary Contact dropdown I would only see the emails associated with "me" instead of the entire set.

TAGS
AUG Leaders

Atlassian Community Events