Send E-mail Notification after editing a specific custom field in issue edit screen

We have a customfield namely "Call Type". We need jira mail notication whenever a value of this customfield is changed. I have used following script in "behaviour" for this purpose but it is not working -

import com.onresolve.jira.groovy.user.FieldBehaviours
import com.onresolve.jira.groovy.user.FormField
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.Issue
import com.atlassian.mail.Email
import com.atlassian.mail.server.MailServerManager
import com.atlassian.mail.server.SMTPMailServer
import org.apache.log4j.Category
  
 
  
FormField "Call Type" = getFieldById(fieldChanged)
ComponentManager componentManager = ComponentManager.getInstance()
MailServerManager mailServerManager = componentManager.getMailServerManager()
SMTPMailServer mailServer = mailServerManager.getDefaultSMTPMailServer()
  
if (mailServer) {
     
        Email email = new Email("someuser@example.com") // Set the TO address, optionally CC and BCC
        email.setSubject("Blocking issue raised by ${issue.getReporter().getFullName()}: ${issue.summary}") // todo: check the subject value
        String content = "Construct your body here.";  //TODO: Set email's body.
        email.setBody(content)
        mailServer.send(email)
     
}
else {
    log.error("No SMTP mail server defined")
}

It gives us below mentioned error -

Compilation failure: startup failed: Script1.groovy: 14: unexpected token: Call Type @ line 14, column 11. FormField "Call Type" = getFieldById(fieldChanged) ^ 1 error

Could you please help us regarding this ?

Regards

Sumit

7 answers

Hi Sameera,

After changing following error occured -

Compilation failure: startup failed: Script1.groovy: 7: unable to resolve class com.atlassian.mail.Email @ line 7, column 1. import com.atlassian.mail.Email ^ Script1.groovy: 8: unable to resolve class com.atlassian.mail.server.MailServerManager @ line 8, column 1. import com.atlassian.mail.server.MailServerManager ^ Script1.groovy: 9: unable to resolve class com.atlassian.mail.server.SMTPMailServer @ line 9, column 1. import com.atlassian.mail.server.SMTPMailServer ^ Script1.groovy: 5: unable to resolve class com.atlassian.jira.ComponentAccessor @ line 5, column 1. import com.atlassian.jira.ComponentAccessor ^ 4 errors

Regards

Sumit

Looks like a variable declaration problem. Can you please try it like this (line #12),

FormField call_type = getFieldById(fieldChanged)

(replace "Call Type" by call_type everywhere)

Can you please convert your post to a comment to my answer? So it will be easier to keep track of discussion.

Seems you have imported the wrong package.

Looking at the developer docs, I find only one path under 'com.atlassian.mail...' hierarchi.

Can you import 'com.atlassian.mail.server.managers.OFBizMailServerManager' instead of 'com.atlassian.mail.server.MailServerManager' ?

I believe this should work.

I have changed my code as per your suggestion. Still no luck. Please find my Code below -

import com.onresolve.jira.groovy.user.FieldBehaviours
import com.onresolve.jira.groovy.user.FormField
import com.atlassian.jira.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.mail.Email
import com.atlassian.mail.server.managers.OFBizMailServerManager
import com.atlassian.mail.server.SMTPMailServer
import org.apache.log4j.Category
   
  
   
FormField call_type= getFieldById(fieldChanged)
ComponentAccessor ComponentAccessor = ComponentAccessor.getInstance()
OFBizMailServerManager mailServerManager = ComponentAccessor.getOFBizMailServerManager()
SMTPMailServer mailServer = OFBizMailServerManager.getDefaultSMTPMailServer()
   
if (mailServer) {
      
        Email email = new Email("system-admin@nrifintech.com") // Set the TO address, optionally CC and BCC
        email.setSubject("Blocking issue raised by ${issue.getReporter().getFullName()}: ${issue.summary}") // todo: check the subject value
        String content = "Construct your body here.";  //TODO: Set email's body.
        email.setBody(content)
        mailServer.send(email)
      
}
else {
    log.error("No SMTP mail server defined")
    }

Now below mentioned error showing -

Compilation failure: startup failed: Script1.groovy: 7: unable to resolve class com.atlassian.mail.Email @ line 7, column 1. import com.atlassian.mail.Email ^ Script1.groovy: 8: unable to resolve class com.atlassian.mail.server.managers.OFBizMailServerManager @ line 8, column 1. import com.atlassian.mail.server.managers.OFBizMailServerManager ^ Script1.groovy: 9: unable to resolve class com.atlassian.mail.server.SMTPMailServer @ line 9, column 1. import com.atlassian.mail.server.SMTPMailServer ^ Script1.groovy: 5: unable to resolve class com.atlassian.jira.ComponentAccessor @ line 5, column 1. import com.atlassian.jira.ComponentAccessor ^ 4 errors


I have changed my code as per your suggestion. Still no luck. Please find my Code below -

import com.onresolve.jira.groovy.user.FieldBehaviours
import com.onresolve.jira.groovy.user.FormField
import com.atlassian.jira.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.mail.Email
import com.atlassian.mail.server.managers.OFBizMailServerManager
import com.atlassian.mail.server.SMTPMailServer
import org.apache.log4j.Category
    
   
    
FormField call_type= getFieldById(fieldChanged)
ComponentAccessor ComponentAccessor = ComponentAccessor.getInstance()
OFBizMailServerManager mailServerManager = ComponentAccessor.getOFBizMailServerManager()
SMTPMailServer mailServer = OFBizMailServerManager.getDefaultSMTPMailServer()
    
if (mailServer) {
       
        Email email = new Email("system-admin@nrifintech.com") // Set the TO address, optionally CC and BCC
        email.setSubject("Blocking issue raised by ${issue.getReporter().getFullName()}: ${issue.summary}") // todo: check the subject value
        String content = "Construct your body here.";  //TODO: Set email's body.
        email.setBody(content)
        mailServer.send(email)
       
}
else {
    log.error("No SMTP mail server defined")
    }

Now below mentioned error showing -

Compilation failure: startup failed: Script1.groovy: 7: unable to resolve class com.atlassian.mail.Email @ line 7, column 1. import com.atlassian.mail.Email ^ Script1.groovy: 8: unable to resolve class com.atlassian.mail.server.managers.OFBizMailServerManager @ line 8, column 1. import com.atlassian.mail.server.managers.OFBizMailServerManager ^ Script1.groovy: 9: unable to resolve class com.atlassian.mail.server.SMTPMailServer @ line 9, column 1. import com.atlassian.mail.server.SMTPMailServer ^ Script1.groovy: 5: unable to resolve class com.atlassian.jira.ComponentAccessor @ line 5, column 1. import com.atlassian.jira.ComponentAccessor ^ 4 errors

I have changed my code as per your suggestion. Still no luck. Please find my Code below -

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import com.onresolve.jira.groovy.user.FieldBehaviours
import com.onresolve.jira.groovy.user.FormField
import com.atlassian.jira.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.mail.Email
import com.atlassian.mail.server.managers.OFBizMailServerManager
import com.atlassian.mail.server.SMTPMailServer
import org.apache.log4j.Category
FormField call_type= getFieldById(fieldChanged)
ComponentAccessor ComponentAccessor = ComponentAccessor.getInstance()
OFBizMailServerManager mailServerManager = ComponentAccessor.getOFBizMailServerManager()
SMTPMailServer mailServer = OFBizMailServerManager.getDefaultSMTPMailServer()
if (mailServer) {
Email email = new Email("system-admin@nrifintech.com") // Set the TO address, optionally CC and BCC
email.setSubject("Blocking issue raised by ${issue.getReporter().getFullName()}: ${issue.summary}") // todo: check the subject value
String content = "Construct your body here."; //TODO: Set email's body.
email.setBody(content)
mailServer.send(email)
}
else {
log.error("No SMTP mail server defined")
}

Now below mentioned error showing -

1
Compilation failure: startup failed: Script1.groovy: 7: unable to resolve class com.atlassian.mail.Email @ line 7, column 1. import com.atlassian.mail.Email ^ Script1.groovy: 8: unable to resolve class com.atlassian.mail.server.managers.OFBizMailServerManager @ line 8, column 1. import com.atlassian.mail.server.managers.OFBizMailServerManager ^ Script1.groovy: 9: unable to resolve class com.atlassian.mail.server.SMTPMailServer @ line 9, column 1. import com.atlassian.mail.server.SMTPMailServer ^ Script1.groovy: 5: unable to resolve class com.atlassian.jira.ComponentAccessor @ line 5, column 1. import com.atlassian.jira.ComponentAccessor ^ 4 errors

I have changed my code as per your suggestion. Still no luck. Please find my Code below -

import com.onresolve.jira.groovy.user.FieldBehaviours
import com.onresolve.jira.groovy.user.FormField
import com.atlassian.jira.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.mail.Email
import com.atlassian.mail.server.managers.OFBizMailServerManager
import com.atlassian.mail.server.SMTPMailServer
import org.apache.log4j.Category
     
    
     
FormField call_type= getFieldById(fieldChanged)
ComponentAccessor ComponentAccessor = ComponentAccessor.getInstance()
OFBizMailServerManager mailServerManager = ComponentAccessor.getOFBizMailServerManager()
SMTPMailServer mailServer = OFBizMailServerManager.getDefaultSMTPMailServer()
     
if (mailServer) {
        
        Email email = new Email("system-admin@nrifintech.com") // Set the TO address, optionally CC and BCC
        email.setSubject("Blocking issue raised by ${issue.getReporter().getFullName()}: ${issue.summary}") // todo: check the subject value
        String content = "Construct your body here.";  //TODO: Set email's body.
        email.setBody(content)
        mailServer.send(email)
        
}
else {
    log.error("No SMTP mail server defined")
    }

Now below mentioned error showing -

Compilation failure: startup failed: Script1.groovy: 7: unable to resolve class com.atlassian.mail.Email @ line 7, column 1. import com.atlassian.mail.Email ^ Script1.groovy: 8: unable to resolve class com.atlassian.mail.server.managers.OFBizMailServerManager @ line 8, column 1. import com.atlassian.mail.server.managers.OFBizMailServerManager ^ Script1.groovy: 9: unable to resolve class com.atlassian.mail.server.SMTPMailServer @ line 9, column 1. import com.atlassian.mail.server.SMTPMailServer ^ Script1.groovy: 5: unable to resolve class com.atlassian.jira.ComponentAccessor @ line 5, column 1. import com.atlassian.jira.ComponentAccessor ^ 4 errors

Could you please look into this ?

Suggest an answer

Log in or Sign up to answer
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Published Monday in Jira Software

How large do you think Jira Software can grow?

Hi Atlassian Community! My name is Shana, and I’m on the Jira Software team. One of the many reasons this Community exists is to connect you to others on similar product journeys or with comparabl...

689 views 6 13
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you