ScriptRunner create-by-mail - custom field formatting?

Connor Kelly September 24, 2019

Hello! I'm trying to configure a ScriptRunner mail handler so that custom field values of various types can be set when the issue is created. I've got it working for Text fields and Components, but I can't seem to get other types to work.

For example, I want to set a checkbox field "Checkboxes" (field id 10202) with the value "Other" checked using the following:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.config.util.JiraHome
import com.atlassian.jira.service.services.file.FileService
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()
def optionsManager = ComponentAccessor.getOptionsManager()
def projectComponentManager = ComponentAccessor.getProjectComponentManager()
JiraHome jiraHome = ComponentAccessor.getComponent(JiraHome)

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

if (issue) {
return
}

ApplicationUser user = userManager.getUserByName("admin")
ApplicationUser reporter = messageUserProcessor.getAuthorFromSender(message) ?: user
def project = projectManager.getProjectObjByKey("TESTPROJ")

def issueObject = issueFactory.getIssue()
def cf1 = customFieldManager.getCustomFieldObject("customfield_10202")
def fieldConfig = cf.getRelevantConfig(issue)
def option = optionsManager.getOptions(fieldConfig).getOptionForValue("Other", null)
issueObject.setProjectObject(project)
issueObject.setSummary(subject)
issueObject.setDescription(MailUtils.getBody(message))
issueObject.setIssueTypeId(project.issueTypes.find { it.name == "Story" }.id)
issueObject.setReporter(reporter)
issue.setCustomFieldValue(cf, [option])
issue = messageHandlerContext.createIssue(user, issueObject)

def attachments = MailUtils.getAttachments(message)

attachments.each { MailUtils.Attachment attachment ->
def destination = new File(jiraHome.home, FileService.MAIL_DIR).getCanonicalFile()
def file = FileUtils.getFile(destination, attachment.filename) as File
FileUtils.writeByteArrayToFile(file, attachment.contents)
messageHandlerContext.createAttachment(file, attachment.filename, attachment.contentType, user, issue)
}

But, it doesn't take, the handler failing to create the issue at all. There's no errors in the script box when setting up the handler, so I assume I'm not formatting some part of the definitions or issueObject call correctly. Anyone know how to get this to work?

I have a similar problem with dropdown fields, not sure how to get that working. Thanks!

1 answer

0 votes
Connor Kelly September 24, 2019

Answered my own question after some more digging - the problem was these lines:

def fieldConfig = cf.getRelevantConfig(issue)
def option = optionsManager.getOptions(fieldConfig).getOptionForValue("Other", null)

It actually needs to be:

def fieldConfig = cf1.getRelevantConfig(issueObject)
def optionlist = optionsManager.getOptions(fieldConfig)
def option = optionlist.find{it.value == 'Other'}

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events