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

Next challenges

Recent achievements

  • Global
  • Personal

Recognition

  • Give kudos
  • Received
  • Given

Leaderboard

  • Global

Trophy case

Kudos (beta program)

Kudos logo

You've been invited into the Kudos (beta program) private group. Chat with others in the program, or give feedback to Atlassian.

View group

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

How do I update a custom field using a email listener.

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")
}

 

1 answer

1 accepted

0 votes
Answer accepted

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' }

Suggest an answer

Log in or Sign up to answer
TAGS

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you