How Include a custom field Scriptrunner MailHanlder?

Shah Baloch
Contributor
June 30, 2023

Hi Community,

We set up an email handler using ScriptRunner. The script was working fine, it was creating issues whenever an email was received. However, we added a custom drop-down field "Email Type" with multiple values. We would like to have the "Potential Spam" value selected whenever an issue is created by the mail handler. I added three lines to the script and try to make it works but it's not creating an issue when an email is received. When I remove these additional lines I added for the custom field then it works fine. Any idea what else needs to be added or removed in order to make it works?

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 org.apache.commons.io.FileUtils

def userManager = ComponentAccessor.getComponent(UserManager)
def projectManager = ComponentAccessor.getProjectManager()
def issueFactory = ComponentAccessor.getIssueFactory()
def messageUserProcessor = ComponentAccessor.getComponent(MessageUserProcessor)
def customFieldManager = ComponentAccessor.getCustomFieldManager() //added this line for custom field

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

if (issue) {
return
}

ApplicationUser user = userManager.getUserByName("security")
ApplicationUser reporter = messageUserProcessor.getAuthorFromSender(message) ?: user
def project = projectManager.getProjectObjByKey("NAS")
def issueObject = issueFactory.getIssue()
def emailType = customFieldManager.getFieldObjectByName("Email Type") //added this line for custom field

issueObject.setProjectObject(project)
issueObject.setSummary(subject)
issueObject.setDescription(MailUtils.getBody(message))
issueObject.setIssueTypeId(project.issueTypes.find { it.name == "Task" }.id)
issueObject.setCustomFieldValue(emailType, "Potential Spam") //added this line for custom field
issueObject.setReporter(reporter)
issue = messageHandlerContext.createIssue(user, issueObject)

 Thank you,

2 answers

1 accepted

Suggest an answer

Log in or Sign up to answer
2 votes
Answer accepted
Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
July 1, 2023

Hi @Shah Baloch

The approach you are taking will not work. If you intend to set the value of a custom field using the Mail Handler, you cannot do it when the Issue is being created.

Instead, you need to set the value to the custom field after the issue has been created by updating the Issue.

I have alredy provided a similar solution in this Community Post. However, I am adding it once again here.

Below is a sample working code for your reference:-

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

//This requires the name of the user that will trigger the Mail Handler
final def username = 'admin'

//This requires the Project's Key that will be used for the Mail Handler
final def projectKey = 'MOCK'

//This requires the name of the Issue Type that will be used
final def issueTypeName = 'Bug'

def userManager = ComponentAccessor.userManager
def projectManager = ComponentAccessor.projectManager
def issueManager = ComponentAccessor.issueManager
def issueFactory = ComponentAccessor.issueFactory
def messageUserProcessor = ComponentAccessor.getComponent(MessageUserProcessor)
def customFieldManager = ComponentAccessor.customFieldManager
def optionsManager = ComponentAccessor.optionsManager

def user = userManager.getUserByName(username)
def issueReporter = messageUserProcessor.getAuthorFromSender(message) ?: user
def project = projectManager.getProjectObjByKey(projectKey)
def subject = message.subject as String
def messageBody = MailUtils.getBody(message)

def sampleList = customFieldManager.getCustomFieldObjectsByName('Mail Type').first()
def issue = ServiceUtils.findIssueObjectInString(subject) as MutableIssue

if (issue) {
return
}

def issueObject = issueFactory.issue
issueObject.setProjectObject(project)
issueObject.setDescription(messageBody)
issueObject.setSummary(subject)
issueObject.setIssueTypeId(project.issueTypes.find { it.name == issueTypeName }.id)
issueObject.setReporter(issueReporter)
issue = messageHandlerContext.createIssue(user, issueObject) as MutableIssue

def fieldConfig = sampleList.getRelevantConfig(issue)
def options = optionsManager.getOptions(fieldConfig)
def option = options.find{it.value == 'Possible Spam'}
issue.setCustomFieldValue(sampleList, option)

issueManager.updateIssue(user, issue, EventDispatchOption.DO_NOT_DISPATCH,false)

Please note that the sample working code above is not 100% exact to your environment. Hence, you will need to make the required modifications.

Below is a screenshot of the Mail Handler configuration for your reference:-

mail_handler_config.png

 

I am also including a screenshot from a test performed.

The screenshot below displays a issue that has been created by the mail handler. If you observe, the value for the custom field Mail Type which is a Single Select List is set to Possible Spam as expected via the Mail Handler.

test1.png

I hope this helps to solve your question. :-)

Thank you and Kind regards,

Ram

Shah Baloch
Contributor
July 3, 2023

Thank you @Ram Kumar Aravindakshan _Adaptavist_ I actually already have an automation job to edit the issue and update the Email type field value. However, the issue is that there will be timestamps in the update field. I was trying to get rid of it timestamp from the update field. I created a filter to pull potential spam type of issue that was created an hour ago if the update field doesn't have a time stamp then escalate it to level 2 support. That was the reason I wanted to do it without edit.

Is it possible to have a component instead of a custom field? I mean if I use a component field instead of a custom field, will it be included when an issue is created using the mail handler?

Thanks,

Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
July 3, 2023

Hi @Shah Baloch

In your last comment, you mentioned:-

Is it possible to have a component instead of a custom field? I mean if I use a component field instead of a custom field, will it be included when an issue is created using the mail handler?

To answer your question, yes, it is possible, but you must modify your code.

Below is an updated working sample code for your reference:-

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

//This requires the name of the user that will trigger the Mail Handler
final def username = 'admin'

//This requires the Project's Key that will be used for the Mail Handler
final def projectKey = 'MOCK'

//This requires the name of the Issue Type that will be used
final def issueTypeName = 'Story'

def userManager = ComponentAccessor.userManager
def projectManager = ComponentAccessor.projectManager
def issueManager = ComponentAccessor.issueManager
def issueFactory = ComponentAccessor.issueFactory
def messageUserProcessor = ComponentAccessor.getComponent(MessageUserProcessor)
def projectComponentManager = ComponentAccessor.projectComponentManager

def user = userManager.getUserByName(username)
def issueReporter = messageUserProcessor.getAuthorFromSender(message) ?: user
def project = projectManager.getProjectObjByKey(projectKey)

def components = projectComponentManager.findByComponentNameCaseInSensitive('Possible Spam')

def subject = message.subject as String
def messageBody = MailUtils.getBody(message)

def issue = ServiceUtils.findIssueObjectInString(subject) as MutableIssue

if (issue) {
return
}

def issueObject = issueFactory.issue
issueObject.setProjectObject(project)
issueObject.setDescription(messageBody)
issueObject.setSummary(subject)
issueObject.setIssueTypeId(project.issueTypes.find { it.name == issueTypeName }.id)
issueObject.setReporter(issueReporter)
issueObject.setComponent(components)
messageHandlerContext.createIssue(user, issueObject) as MutableIssue
Please note that the sample code above is not 100% exact to your environment. Hence, you will need to make the required modifications.
Below is a screenshot of the test result:-
test1.png
As you can see in the screenshot above, instead of setting the value for a custom field, the option is selected from the Component/s field this time.
And since this is for a System Field, the Component/s field value can be set during issue creation.
I hope this helps solve your question. :-)

Thank you and Kind regards,
Ram
Shah Baloch
Contributor
July 5, 2023

It worked, thank you Ram.

Shah Baloch
Contributor
July 6, 2023

@Ram Kumar Aravindakshan _Adaptavist_I'm trying to add an assignee. I added the below code to the script but it's still assigning the issue to the default project assignee. Is it possible to add an assignee?

final def assignee ='poweruser'
def
issueAssignee = userManager.getUserByName(assignee)
issueObject.setAssignee(issueAssignee)
Thanks
0 votes
Tansu Akdeniz
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
June 30, 2023

Hi @Shah Baloch 

Does it work if you provide select list option id?

You can also try issueInputParameters to create an issue. Please check.

Shah Baloch
Contributor
June 30, 2023

Hi @Tansu Akdeniz I tried the below input parameters but it didn't work.

//IssueInputParameters addCustomFieldValue(String customFieldId=17200, String "Potential Spam")
//IssueInputParameters addCustomFieldValue(Long customFieldId=17200, String "Potential Spam")
//IssueInputParameters addCustomFieldValue(String "Email Type", String "Potential Spam")
IssueInputParameters addCustomFieldValue(Long "Email Type", String "Potential Spam")
How can I get the selection option id?
Thank you,
TAGS
AUG Leaders

Atlassian Community Events