The Atlassian Community Forums are currently in read-only mode. We will be relaunching on a new platform on September 22 (read more here). We apologize for the extended downtime. For concerns or questions, please email communitymanagers@atlassian.com. See you on the other side, on the new Atlassian Community Forums! :)

×

Forums

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

creating Listener (script) via script Runner if 2 condition is met

Bhagwan Basnet _Aakash_
May 19, 2023

Hi Friends,

I am new to groovy scripting and need your help to learn

I need to create a Listener to send an custom email when following 2 condition is met

  • 1. when custom field with field type (single select line) is provided certain value to it 
  • e.g Development State Field should hold value "In Review"
  • 2. when issue type is "SW system Engineering"
  • it would be great if someone could help me providing the script that sends an email if these 2 condition are there

-Thankyou in advance

1 answer

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

1 vote
Ken McClean
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 Champions.
May 19, 2023

This should get you started:

 


import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.mail.server.MailServerManager
import com.atlassian.sal.api.component.ComponentLocator
import com.atlassian.mail.Email;
import com.atlassian.mail.server.SMTPMailServer;

def mailManager = ComponentLocator.getComponent(MailServerManager)
def issueManager = ComponentAccessor.getIssueManager()
SMTPMailServer mailServer = mailManager.getDefaultSMTPMailServer()


def userEmail = "user@email.com"

//Who should the email be sent to?


Email email = new Email("${userEmail}");

 


//def issue = issueManager.getIssueByCurrentKey("ISSUE-7")
//uncomment this line to test the script against a specific issue

def customFieldValue = issue.getCustomFieldValue(10500)

//Change to the value of the custom field ID

 

if ((customFieldValue.toString() == "In Review") && issue.issueType == "SW system Engineering") {
email.setSubject("Issue Detected");

email.setBody("Issue ${issue.key} has a custom field with a value of ${customFieldValue.toString()}, and had an issue type of ${issue.issueType}");
mailServer.send(email);


}

Radek Dostál
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 Champions.
May 20, 2023
def customFieldValue = issue.getCustomFieldValue(10500)

This part won't work, as per https://docs.atlassian.com/software/jira/docs/api/9.8.1/com/atlassian/jira/issue/Issue.html#getCustomFieldValue-com.atlassian.jira.issue.fields.CustomField- it expects a CustomField parameter.

 

So just need to change this to

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def customField = customFieldManager.getCustomFieldObject("customfield_11111") //obviously change this

def customFieldValue = issue.getCustomFieldValue(customField)

 

This comparison

customFieldValue.toString() == "In Review"

I think works, I believe that the option implements toString() to return the value, but in case it doesn't it would be "customFieldValue.getValue() == "In Review", but I think it's not necessary, think that the toString() works as is but I don't have the source code on hands right now to verify, so just mentioning this in case..

 

other than that lgtm

Bhagwan Basnet _Aakash_
May 25, 2023

Thankyou so much Ken and Redak.

this works with slight modification

TAGS
AUG Leaders

Atlassian Community Events