Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

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

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

1 vote
Answer accepted

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

@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

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Apps & Integrations

Send an Email or Publish to Confluence - What should you do with your release notes?

Background When you hear the words ‘Release notes’, almost always you think of an unsolicited email from a software vendor. But I am here to tell you that from our data, sending release notes via E...

138 views 1 2
Read article

Community Events

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

Find an event

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

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you