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
Community moderators have prevented the ability to post new answers.
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) } }
How do I tie the listener into the post condition using script runner?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Yes thats correct, you also need to edit "template" with the "Email template" you have. That should be about it.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
This is great information. I'm getting an error:
image2016-3-23 14:15:44.png
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Also, where do I put my conditions for triggering the email?
image2016-3-23 14:17:50.png
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
The "if (! emailAdress)" should be "if (emailAddress)"
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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)
}
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
apologies, the email comes through now but without the ${issue.key}. Is the syntax correct?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Is your post function being run on issue create?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Sorry, yeah the ordering matters in create. Glad you found the solution.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.