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

Update multi select field based from email body

Rodolfo So May 28, 2022




Hi  

Can someone can help me on how to update multi select field based from the email body using scriptrunner email handler?

Jira data center

Vetrical field is multi select field

 

Email body



final String body = """This is a test body

Vertical: [ "option1","option2"]

Description: sample description

"""

 

Appreciate your help on this

1 answer

Suggest an answer

Log in or Sign up to answer
0 votes
Nic Brough -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.
May 28, 2022

This is a script which is generally useless because it's a composite example - it shows you how to set a value of every type of standard custom field.

You should be able to extract the bits that refer to multi-selects quite easily

https://library.adaptavist.com/entity/update-the-value-of-custom-fields-through-the-script-console

Rodolfo So May 30, 2022

Hi @Nic Brough -Adaptavist- 

 

I've already tried that one but I'm not sure how to pass the variable to select option.

 

 

Here's my Scriptrunner mail handler script

 

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
import com.atlassian.jira.user.ApplicationUser
//This requires the name of the user that will trigger the Mail Handler
final def username = 'testuser'

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

//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 customFieldManager = ComponentAccessor.customFieldManager
def optionsManager = ComponentAccessor.optionsManager

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


def cfVertical = customFieldManager.getCustomFieldObjectsByName('Vertical').first()

def issue = ServiceUtils.findIssueObjectInString(subject) as MutableIssue

if (issue) {
return
}

def issueObject = issueFactory.issue
issueObject.setProjectObject(project)
issueObject.setSummary(subject)
issueObject.setIssueTypeId(project.issueTypes.find { it.name == issueTypeName }.id)
issueObject.setReporter(reporter)

//vertical
def regex = /(?<=\[\[)([^\[\]]*)(?=\]\])/
def emailMatch = (MailUtils.getBody(message) =~ regex).findAll().first() as List
def VerticalEmail = emailMatch.first() as String

//issueObject.setDescription(MailUtils.getBody(message))
issueObject.setDescription(VerticalEmail)
issue = messageHandlerContext.createIssue(user, issueObject) as MutableIssue

 

def fieldConfig = cfVertical.getRelevantConfig(issue)
def options = optionsManager.getOptions(fieldConfig)
def option = options.findAll { it.value in [VerticalEmail] }

issue.setCustomFieldValue(cfVertical, option)

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

TAGS
AUG Leaders

Atlassian Community Events