You're on your way to the next level! Join the Kudos program to earn points and save your progress.
Level 1: Seed
25 / 150 points
Next: Root
1 badge earned
Challenges come and go, but your rewards stay with you. Do more to earn more!
What goes around comes around! Share the love by gifting kudos to your peers.
Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!
Join now to unlock these features and more
The Atlassian Community can help you and your team get more value out of Atlassian products and practices.
Hello,
I am trying to set the value of custom field for participants after they have been creating or already existed. The value is not set. Kindly advice. Please see my code below:
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 javax.mail.Message
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.event.type.EventDispatchOption
import javax.mail.internet.InternetAddress
import com.atlassian.jira.config.util.JiraHome
import org.apache.commons.io.FileUtils
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
final targetProjectKey = 'BA'
final defaultReporterName = 'admin'
final defaultIssueType = 'Unsorted'
def messageUserProcessor = ComponentAccessor.getComponent(MessageUserProcessor) as MessageUserProcessor
def userManager = ComponentAccessor.getComponent(UserManager)
def projectManager = ComponentAccessor.projectManager
def issueFactory = ComponentAccessor.issueFactory
def customFieldManager = ComponentAccessor.customFieldManager
JiraHome jiraHome = ComponentAccessor.getComponent(JiraHome)
def subject = message.subject as String
def issue = ServiceUtils.findIssueObjectInString(subject)
def cf = customFieldManager.getCustomFieldObject("customfield_10101")
def project = projectManager.getProjectObjByKey(targetProjectKey)
def user = userManager.getUserByName(defaultReporterName)
def reporterUser = messageUserProcessor.getAuthorFromSender(message) ?: user
def allCcRecipients = message.getRecipients(Message.RecipientType.CC) as ArrayList<InternetAddress>
def allToRecipients = message.getRecipients(Message.RecipientType.TO) as ArrayList<InternetAddress>
log.warn ("allCcRecipients: " + allCcRecipients)
def participantList = [] as ArrayList<ApplicationUser>
def issueObject = issueFactory.issue
def body = MailUtils.getBody(message)
allCcRecipients.findAll {
allCcRecipients.each {
InternetAddress internetAddress = it as InternetAddress
def participant1 = messageUserProcessor.findUserByEmail(it.address) as ApplicationUser
if (!participant1){
def email = internetAddress.address
def fullName = internetAddress.getPersonal() ?: email
def mailparticipant = userManager.getUserByName(fullName)
participant1 = messageHandlerContext.createUser(email, null, email, fullName, null)
}
participantList.push(participant1)
}
}
allToRecipients.findAll {
allToRecipients.each {
InternetAddress internetAddress = it as InternetAddress
def participant1 = messageUserProcessor.findUserByEmail(it.address) as ApplicationUser
if (!participant1){
def email = internetAddress.address
def fullName = internetAddress.getPersonal() ?: email
def mailparticipant = userManager.getUserByName(fullName)
participant1 = messageHandlerContext.createUser(email, null, email, fullName, null)
}
participantList.push(participant1)
}
}
def comment = MailUtils.getBody(message)
messageHandlerContext.createComment(issue, reporterUser, comment, false)
log.warn (">>>>> participantList after comment: " + participantList)
def attachments = MailUtils.getAttachments(message) // <2>
attachments.each { MailUtils.Attachment attachment ->
def destination = new File(jiraHome.home, FileService.MAIL_DIR).getCanonicalFile()
def file = FileUtils.getFile(destination, attachment.filename) as File // <3>
FileUtils.writeByteArrayToFile(file, attachment.contents)
messageHandlerContext.createAttachment(file, attachment.filename, attachment.contentType, user, issue) // <4>
}
issueObject.setCustomFieldValue(cf,participantList)
ComponentAccessor.getIssueManager().updateIssue(user, issueObject, EventDispatchOption.ISSUE_UPDATED, false)
Thanks,
Swarna