Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
Celebration

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,559,731
Community Members
 
Community Events
185
Community Groups

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

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

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 Leaders.
May 19, 2023 • edited

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 Leaders.
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

Thankyou so much Ken and Redak.

this works with slight modification

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events