I need to update a custom field with a set value when a ticket is created.
Herewith the code.
The field is not populated at all.
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.component.ComponentAccessor
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
import javax.mail.Address
import javax.mail.Message
import javax.mail.internet.InternetAddress
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 sender = MailUtils.getSenders(message)
def addresses = message.getRecipients(Message.RecipientType.CC) as List<Address>
def issue = ServiceUtils.findIssueObjectInString(subject)
if (issue) {
return
}
ApplicationUser user = userManager.getUserByName("jira admin")
ApplicationUser reporteruser = messageUserProcessor.getAuthorFromSender(message) ?: user
def project = projectManager.getProjectObjByKey("OPS")
def issueObject = issueFactory.getIssue()
issueObject.setProjectObject(project)
issueObject.setSummary(subject)
issueObject.setDescription(MailUtils.getBody(message))
issueObject.setIssueTypeId(project.issueTypes.find { it.name == "OPS" }.id)
issueObject.setReporter(reporteruser)
def customFieldManager = ComponentAccessor.customFieldManager
def optionsManager = ComponentAccessor.optionsManager
def mySelectCf = customFieldManager.getCustomFieldObject("Myriad business area")
def mySelectContextConfig = mySelectCf.getRelevantConfig(issueObject)
def selectOptions = optionsManager.getOptions(mySelectContextConfig)
def myOption = selectOptions.find{it.value == "Policy & Admin"}
if(myOption){
issueObject.setCustomFieldValue(mySelectCf, myOption)
}
else
{
log.error("Ticket source value is not set")
}
getCustomFieldObject(<string>) expects the custom field id, not the name of a field. So your code is not getting the field at all, and stopping dead. That would be something like customfield_12345
Either get the id for your field, or use getCustomFieldsByName("Myriad Business area") - you'll need to iterate through that as it will return an array of customfields (in case you have two or more with the same name). Or be lazy and assume it's never going to happen and you can simply take the first element.
You'll also struggle with myOption - you're looking for a string, but the content of it.value will be an option object - for that, you can get the option name out with option.getName()
Thank you.
I changed it to
def mySelectCf = customFieldManager.getCustomFieldObject("customfield_15600")
def mySelectContextConfig = mySelectCf.getRelevantConfig(issueObject)
def selectOptions = optionsManager.getOptions(mySelectContextConfig)
def myOption = selectOptions.find { it.value == 'Policy & Admin' }
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.