How to set custom field in incoming mail handler in sciptrunner?

nihcet July 10, 2019

I want to set custom field in scriptrunner mail handler. Works issueInputParameters.addCustomFieldValue(cf.id, 'my values') in transition post function. But, don't work in mail handler.

JIRA v7.12.1

ScriptRunner v5.5.7

Thanks for help.

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def cf = customFieldManager.getCustomFieldObjects(issue).find {it.name == 'My Custom Field'}
issueInputParameters.addCustomFieldValue(cf.id, 'My Values')

 snap02002.jpg

1 answer

0 votes
Bob August 2, 2019

Hi Nihat, 

using <issueobject>.setCustomFieldValue(<customfieldobject>,<value>) should work. 

Something like this ;

def issueObject = issueFactory.getIssue()
def cf = customFieldManager.getCustomFieldObject(12345)
...
issueObject.setCustomFieldValue(cf, "value")
messageHandlerContext.createIssue(user, issueObject)

 

Kind regards, 

 

Bob

nihcet August 16, 2019

Hi Bob,

Thanks for your answer.

Solved with this code,

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def TYtest = customFieldManager.getCustomFieldObjectByName("TYtest1")
issueObject.setCustomFieldValue(TYtest, addresses)
Brian Keefe October 30, 2019

Hello - I'm trying to do this with a single select custom field. Glad to come across this, thanks for getting the thread started. But I still get a generic "Exception thrown while executing the script." error. Any ideas?

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.ComponentManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.Issue

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) {
return
}

ApplicationUser user = userManager.getUserByName("first.last")
ApplicationUser reporter = messageUserProcessor.getAuthorFromSender(message) ?: user
def project = projectManager.getProjectObjByKey("XYZ")

def cfSelect = ComponentAccessor.customFieldManager.getCustomFieldObjectByName("Country/Region")
def cfConfig = cfSelect.getRelevantConfig(issue)
def value = ComponentAccessor.optionsManager.getOptions(cfConfig)?.find {
it.toString() == 'GB'
}

def issueObject = issueFactory.getIssue()
issueObject.setProjectObject(project)
issueObject.setSummary(subject)
issueObject.setDescription(MailUtils.getBody(message))
issueObject.setIssueTypeId(project.issueTypes.find { it.name == "Task" }.id)
issueObject.setReporter(reporter)
issueObject.setCustomFieldValue(cfSelect, value)

messageHandlerContext.createIssue(user, issueObject)

 I started with the example from ScriptRunner's documentation on Mail Handlers  + setting a select list + this thread. Thank you!

nihcet October 31, 2019

Hi Brian,

What is custom field type for "Country/Region"?

Brian Keefe October 31, 2019

Single Select List - And it does have two different contexts, but if that's the blocker we could probably be okay with having the same values/options in the one field.

Rodolfo So October 26, 2021

Do we have an update on this? I also encountered this issue.

Omprakash Thamsetty
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.
December 23, 2021

@Brian Keefe @Did you fixed the issue ? Same issue I am also facing 

I am looking to set the value for single selection field which is having multiple issue context.

thanks,

om 

Omprakash Thamsetty
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.
July 21, 2022

Hi @Brian Keefe I know, This is very old but it will help to someone else. I was searching the same after 2 yrs for the same. I couldn't find the answer so I had fixed this issue way back.

I am converting the created issue to mutable issue and then updating the custom field values for severity and request source type etc. 

Here is the portion of the code .

issue = messageHandlerContext.createIssue(user, issueObject)



//creating the issue first with email reader using script runner
issue = messageHandlerContext.createIssue(user, issueObject) 

//converting the created issue to mutableissue
MutableIssue mailissue = issue as MutableIssue;

//Getting the ID of relavenconfig for severity field.

def sevFld = customFieldManager.getCustomFieldObjectsByName("Severity").first()

def cfConfig = sevFld.getRelevantConfig(issue)
def value = ComponentAccessor.optionsManager.getOptions(cfConfig)?.find {
it.toString() == '4-Low'
}

mailissue.setCustomFieldValue(sevFld, value)

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

Suggest an answer

Log in or Sign up to answer