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

how to update system and custom fields from a scriptrunner mail handler

Tomas Arguinzones Yahoo October 18, 2019

Hi...I am trying to write a scriptrunner incoming mail handler so when an email is received these three things must happen:

- Add a comment to the jira issue with the body of the email (I was able to do so!)

- Update Description field  with the body of the email (does not work)

- Increased by 1 an integer custom field named "Total Replies Received" (dont know how to do it)

 

I am following the examples in the scriptunner documenation https://scriptrunner.adaptavist.com/latest/jira/mail-handler.html,

in the exanple Find Existing Issue, doing something like issue.setDescription ("variable with the body of the email here") does not work

May anybody assist?

Thank you

1 answer

1 accepted

Suggest an answer

Log in or Sign up to answer
1 vote
Answer accepted
John Chin
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.
October 22, 2019

Hi Tomas,

I created this script below and is working from my side. Please give a try. 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.service.util.ServiceUtils
import com.atlassian.jira.service.util.handler.MessageUserProcessor
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.user.util.UserManager
import com.atlassian.mail.MailUtils
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.event.type.EventDispatchOption

def userManager = ComponentAccessor.getComponent(UserManager)
def projectManager = ComponentAccessor.getProjectManager()
def issueFactory = ComponentAccessor.getIssueFactory()
def messageUserProcessor = ComponentAccessor.getComponent(MessageUserProcessor)

def subject = message.getSubject() as String
def issue = ServiceUtils.findIssueObjectInString(subject)

if (issue) {

log.warn "==========================SR Incoming Mail issue exists"

log.warn "issue: $issue"

ApplicationUser user = userManager.getUserByName("admin")

def issueManager = ComponentAccessor.issueManager
MutableIssue issueObject = issueManager.getIssueObject(issue.key)

issueObject.setDescription("this is an example")

//Custom Field code here

issueManager.updateIssue(user,issueObject,EventDispatchOption.ISSUE_UPDATED,false)

log.warn "==========================SR Incoming mail Ended"
}
{code}
To update a custom field value, you can append the code in the script before "*issueManager.updateIssue()*" API:
{code:java}
def customField = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Custom Field")

issueObject.setCustomFieldValue(customField,"numbers")

Thank you.

Kind regards,
John Chin

Tomas Arguinzones Yahoo October 24, 2019

@John Chin 

 

Hi John...it worked like a charm! This line definitely did the trick:
issueManager.updateIssue(user,issueObject,EventDispatchOption.ISSUE_UPDATED,false)

You guys always rock! Thank you so much

Madhavi Annapureddy - DHS June 28, 2022

@John Chin I am trying a similar one, however, I need to update the custom field single select drop down after an email is sent out in the escalation service. Is this possible? I was able to send email but cannot update the single select custom field. Can you pleasse help?

 

import org.apache.log4j.Logger
import com.atlassian.jira.issue.fields.CustomField
import org.apache.log4j.Level
def log = Logger.getLogger("com.onresolve.jira.groovy")
log.setLevel(Level.INFO)
log.info "AutoEmail error log"

import com.atlassian.mail.Email;
import com.atlassian.mail.server.MailServerManager;
import com.atlassian.mail.server.SMTPMailServer;
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.MutableIssue;
import com.atlassian.jira.issue.ModifiedValue;
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder;
import com.atlassian.jira.service.util.ServiceUtils
import com.atlassian.jira.service.util.handler.MessageUserProcessor
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.user.util.UserManager
import com.atlassian.mail.MailUtils
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.event.type.EventDispatchOption

//customerName = issue.getCustomFieldValue(com.atlassian.jira.component.ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Customer Name"))
def subject = "${issue.key} Follow-up # 2"
def body = "Hi,\n\n This is our second follow-up on the mentioned ticket.  We are reaching out to gather additional information previously requested. Please respond at your earliest convenience. \n\nThank You! \n\n"
//def emailAddr1 = issue.getReporter().getEmailAddress()
emailAddr = issue.getCustomFieldValue(com.atlassian.jira.component.ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Customer Email ID"))
//def emailAddr = issue.getAssignee().getEmailAddress()
//def cemail= customFieldManager.getCustomFieldObject("customfield_13023") as String
//def emailAddr2 = issue.setCustomFieldValue(cemail)
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);
} else {
// Problem getting the mail server from JIRA configuration, log this error
}
}

sendEmail (emailAddr, subject, body)


TAGS
AUG Leaders

Atlassian Community Events