Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

How to trigger an email when some condition met ?

Shivaprasad Hattaraki
Contributor
July 2, 2018

Hi,

I have "Valid warranty date" Date field when it lesser than 2 days than system's date (i mean when warranty date expiring in two days) it should trigger an email to specific user.

I have this following code but not triggering an email

Could you help me on this.

Thank u in advance

import com.atlassian.jira.component.ComponentAccessor
import groovy.xml.MarkupBuilder
def issueManager = ComponentAccessor.issueManager
def customFieldManager = ComponentAccessor.customFieldManager

def warrantyField = customFieldManager.getCustomFieldObjectByName("Valid Warranty Date")

def warranty = issue.getCustomFieldValue(warrantyField)
def systemDate = new Date()

if(warranty < systemDate)

def sendEmail(String emailAddr, String subject, String body) {
SMTPMailServer mailServer = ComponentAccessor.getMailServerManager().getDefaultSMTPMailServer()
if (mailServer) {
Email email = new Email(prasadhattaraki@gmail.com)
email.setSubject(subject)
email.setBody(body)
mailServer.send(email)
log.debug("Mail sent")
} else {
log.warn("Please make sure that a valid mailServer is configured")
}
}

3 answers

1 accepted

1 vote
Answer accepted
Grigory Salnikov
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.
July 2, 2018

Hello!

Why don't you use just a filter subscription? It should be quite easy:

1. Create a filter with a JQL to evaluate the warranty field between today and 2 days after;

2. Create a filter subscription to send an email at any time during the day.

I think it is much easier.

Shivaprasad Hattaraki
Contributor
July 2, 2018

Using escalation service right?

Grigory Salnikov
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.
July 3, 2018

No, it's basic functionality.

For example, in my organization, I added a special time field called "Scheduled action" not to forget about postponed tasks. Then I created a filter to find all tasks scheduled for today. The last step is to subscribe to this filter. That is all, without any coding. In the morning I received the letter on my work email with a list of all the issues that require my attention today.

If you have any specific questions I'm here to answer.

Shivaprasad Hattaraki
Contributor
July 3, 2018

I have "Valid Warranty Date" field , before 2 days of  expiring warranty date it has to trigger email to specific user.

Could you help me in this?

Grigory Salnikov
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.
July 3, 2018

The first step for us is to create a right JQL to search the needed issues. It would be something like that:

project = <name of the project> AND "Valid Warranty Date" >= now() AND "Valid Warranty Date" < "2d"

This will give us the required list of issues. 

We should save this filter and give it a name.

The final step is to subscribe to this filter. You can find it under "My filters":

My filters.jpg

Click on it and then "Subscription" or "Subscribe". 

The final step is to create the subscription. On the screen below there's an "Edit" screen but parameters are the same:

Subscription.jpgSubscription2.jpg

You can choose the recipient (you can make it a group or an individual user). You can also specify the time when you want an email to be delivered.

Should you encounter any difficulties, please, don't hesitate to get back to me.

Shivaprasad Hattaraki
Contributor
July 3, 2018

Thanks lot garg 

Its working

Grigory Salnikov
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.
July 3, 2018

It's "Greg" :)

Happy to help. 

You might want to mark the answer as "Accepted" so that other users with the same question would find the solution easier.

Have a nice day!

0 votes
Mark Markov
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.
July 2, 2018

Hello @Shivaprasad Hattaraki

Is this full code? Here you define function and doesnt use it.

I should be like this

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

def issueManager = ComponentAccessor.issueManager
def customFieldManager = ComponentAccessor.customFieldManager

def warrantyField = customFieldManager.getCustomFieldObjectByName("Valid Warranty Date")

def warranty = issue.getCustomFieldValue(warrantyField)
def systemDate = new Date()

if(warranty < systemDate)
{
sendEmail("prasadhattaraki@gmail.com", "Test subject", "Test body")
}

def sendEmail(String emailAddr, String subject, String body) {
SMTPMailServer mailServer = ComponentAccessor.getMailServerManager().getDefaultSMTPMailServer()
if (mailServer) {
Email email = new Email(emailAddr)
email.setSubject(subject)
email.setBody(body)
mailServer.send(email)
log.debug("Mail sent")
} else {
log.warn("Please make sure that a valid mailServer is configured")
}
}

 

0 votes
Alexey Matveev
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.
July 2, 2018

Hello,

Where do you write this code? You should see in the logs if you make it to the log.debug("Mail sent")

Shivaprasad Hattaraki
Contributor
July 2, 2018

In script listener i wrote

Alexey Matveev
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.
July 2, 2018

You should debug your code and look for errors in the atlassian-jira.log file. Did you look there? Are there any errors? You defined the sendEmail function but you do not use it.

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events