Hi,
I want to add e-mail sender to custom field (email adress). I can add to text to custom field, but I can't add sender variable.
JIRA v7.12.1 & Scriptrunner 5.5.9
My code in incoming mail handler.
// get "TO" from mail
import javax.mail.Message
def addresses = message.getRecipients(Message.RecipientType.TO) as String
// get "TO" from mail
// issue create and add attachment
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
import com.atlassian.mail.MailUtils
import org.apache.commons.io.FileUtils
def userManager = ComponentAccessor.getComponent(UserManager)
def projectManager = ComponentAccessor.getProjectManager()
def issueFactory = ComponentAccessor.getIssueFactory()
def messageUserProcessor = ComponentAccessor.getComponent(MessageUserProcessor)
JiraHome jiraHome = ComponentAccessor.getComponent(JiraHome)
def subject = message.getSubject() as String
def issue = ServiceUtils.findIssueObjectInString(subject)
if (issue) {
return
}
ApplicationUser user = userManager.getUserByName("jiramail")
ApplicationUser reporter = messageUserProcessor.getAuthorFromSender(message) ?: user
def project = projectManager.getProjectObjByKey("TY")
def issueObject = issueFactory.getIssue()
issueObject.setProjectObject(project)
// add "To" info
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def TYtest = customFieldManager.getCustomFieldObjectByName("TYtest1")
issueObject.setCustomFieldValue(TYtest, addresses)
// add "To" info
issueObject.setSummary(subject)
issueObject.setDescription(MailUtils.getBody(message))
issueObject.setIssueTypeId(project.issueTypes.find { it.name == "Mail" }.id)
issueObject.setReporter(reporter)
// add sender to custom field
def emailCf = customFieldManager.getCustomFieldObjectByName("E-Mail Adress")
issueObject.setCustomFieldValue(emailCf, reporter)
// add sender to custom field
issue = messageHandlerContext.createIssue(user, issueObject)
def attachments = MailUtils.getAttachments(message)
attachments.each { MailUtils.Attachment attachment ->
def destination = new File(jiraHome.home, FileService.MAIL_DIR).getCanonicalFile()
def file = FileUtils.getFile(destination, attachment.filename) as File
FileUtils.writeByteArrayToFile(file, attachment.contents)
messageHandlerContext.createAttachment(file, attachment.filename, attachment.contentType, user, issue)
}
// issue create and add attachment
What error are you getting ?
What's the type of the E-Mail Address Field ?
Hi David,
Custom field (E-Mail address) is Text Field (single line).
Log 1: Sender isn't a user in jira.
2019-08-16 16:21:01,025 Caesium-1-1 ERROR anonymous teknikbilgi [c.o.scriptrunner.mail.MailHandler] Failed to execute email handler 'teknikbilgi(as provided on first page)' on message with subject 'test16'
com.atlassian.jira.exception.CreateException: com.atlassian.jira.workflow.WorkflowException: Error occurred while creating issue. This could be due to a plugin being incompatible with this version of JIRA. For more details please consult the logs, and see: http://confluence.atlassian.com/x/3McB
at com.atlassian.jira.issue.managers.DefaultIssueManager.createIssue(DefaultIssueManager.java:519)
at com.atlassian.jira.issue.managers.DefaultIssueManager.createIssueObject(DefaultIssueManager.java:608)
at com.atlassian.jira.issue.managers.RequestCachingIssueManager.createIssueObject(RequestCachingIssueManager.java:209)
at com.atlassian.jira.service.util.handler.DefaultMessageHandlerContext.createIssueWithIssueManager(DefaultMessageHandlerContext.java:248)
at com.atlassian.jira.service.util.handler.DefaultMessageHandlerContext.createIssue(DefaultMessageHandlerContext.java:166)
at com.atlassian.jira.service.services.mail.DelegatingMessageHandlerContext.createIssue(DelegatingMessageHandlerContext.java:43)
at com.atlassian.jira.service.util.handler.MessageHandlerContext$createIssue$0.call(Unknown Source)
at Script119.run(Script119.groovy:53)
Caused by: com.atlassian.jira.workflow.WorkflowException: Error occurred while creating issue. This could be due to a plugin being incompatible with this version of JIRA. For more details please consult the logs, and see: http://confluence.atlassian.com/x/3McB
at com.atlassian.jira.workflow.OSWorkflowManager.createIssue(OSWorkflowManager.java:766)
at com.atlassian.jira.issue.managers.DefaultIssueManager.createIssue(DefaultIssueManager.java:510)
... 7 more
Caused by: java.lang.ClassCastException: com.atlassian.jira.user.DelegatingApplicationUser cannot be cast to java.lang.String
at com.atlassian.jira.issue.customfields.impl.GenericTextCFType.getDbValueFromObject(GenericTextCFType.java:51)
at com.atlassian.jira.issue.fields.ImmutableCustomField.createValue(ImmutableCustomField.java:693)
at com.atlassian.jira.workflow.function.issue.IssueCreateFunction.execute(IssueCreateFunction.java:81)
at com.opensymphony.workflow.AbstractWorkflow.executeFunction(AbstractWorkflow.java:1014)
at com.opensymphony.workflow.AbstractWorkflow.transitionWorkflow(AbstractWorkflow.java:1407)
at com.opensymphony.workflow.AbstractWorkflow.initialize(AbstractWorkflow.java:606)
at com.atlassian.jira.workflow.OSWorkflowManager.createIssue(OSWorkflowManager.java:742)
... 8 more
Log 2: Sender is a user in jira.
2019-08-16 16:21:01,618 Caesium-1-1 ERROR anonymous teknikbilgi [c.o.scriptrunner.mail.MailHandler] Failed to execute email handler 'teknikbilgi(as provided on first page)' on message with subject 'test 0816 16'
com.atlassian.jira.exception.CreateException: com.atlassian.jira.workflow.WorkflowException: Error occurred while creating issue. This could be due to a plugin being incompatible with this version of JIRA. For more details please consult the logs, and see: http://confluence.atlassian.com/x/3McB
at com.atlassian.jira.issue.managers.DefaultIssueManager.createIssue(DefaultIssueManager.java:519)
at com.atlassian.jira.issue.managers.DefaultIssueManager.createIssueObject(DefaultIssueManager.java:608)
at com.atlassian.jira.issue.managers.RequestCachingIssueManager.createIssueObject(RequestCachingIssueManager.java:209)
at com.atlassian.jira.service.util.handler.DefaultMessageHandlerContext.createIssueWithIssueManager(DefaultMessageHandlerContext.java:248)
at com.atlassian.jira.service.util.handler.DefaultMessageHandlerContext.createIssue(DefaultMessageHandlerContext.java:166)
at com.atlassian.jira.service.services.mail.DelegatingMessageHandlerContext.createIssue(DelegatingMessageHandlerContext.java:43)
at com.atlassian.jira.service.util.handler.MessageHandlerContext$createIssue$0.call(Unknown Source)
at Script119.run(Script119.groovy:53)
Caused by: com.atlassian.jira.workflow.WorkflowException: Error occurred while creating issue. This could be due to a plugin being incompatible with this version of JIRA. For more details please consult the logs, and see: http://confluence.atlassian.com/x/3McB
at com.atlassian.jira.workflow.OSWorkflowManager.createIssue(OSWorkflowManager.java:766)
at com.atlassian.jira.issue.managers.DefaultIssueManager.createIssue(DefaultIssueManager.java:510)
... 7 more
Caused by: java.lang.ClassCastException: com.atlassian.jira.user.DelegatingApplicationUser cannot be cast to java.lang.String
at com.atlassian.jira.issue.customfields.impl.GenericTextCFType.getDbValueFromObject(GenericTextCFType.java:51)
at com.atlassian.jira.issue.fields.ImmutableCustomField.createValue(ImmutableCustomField.java:693)
at com.atlassian.jira.workflow.function.issue.IssueCreateFunction.execute(IssueCreateFunction.java:81)
at com.opensymphony.workflow.AbstractWorkflow.executeFunction(AbstractWorkflow.java:1014)
at com.opensymphony.workflow.AbstractWorkflow.transitionWorkflow(AbstractWorkflow.java:1407)
at com.opensymphony.workflow.AbstractWorkflow.initialize(AbstractWorkflow.java:606)
at com.atlassian.jira.workflow.OSWorkflowManager.createIssue(OSWorkflowManager.java:742)
... 8 more
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I believe your problem is located here :
Caused by: java.lang.ClassCastException: com.atlassian.jira.user.DelegatingApplicationUser cannot be cast to java.lang.String
You're trying to cast an object to a string, maybe try to add a .toString()
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thanks David.
I added in code.
"def sender = messageUserProcessor.getAuthorFromSender(message) as String"
Code worked for a user in jira. But code didn't work if sender don't user in jira.
// get "TO" from mail
import javax.mail.Message
def addresses = message.getRecipients(Message.RecipientType.TO) as String
// get "TO" from mail
// issue create and add attachment
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
import com.atlassian.mail.MailUtils
import org.apache.commons.io.FileUtils
def userManager = ComponentAccessor.getComponent(UserManager)
def projectManager = ComponentAccessor.getProjectManager()
def issueFactory = ComponentAccessor.getIssueFactory()
def messageUserProcessor = ComponentAccessor.getComponent(MessageUserProcessor)
JiraHome jiraHome = ComponentAccessor.getComponent(JiraHome)
def subject = message.getSubject() as String
def issue = ServiceUtils.findIssueObjectInString(subject)
if (issue) {
return
}
ApplicationUser user = userManager.getUserByName("jiramail")
ApplicationUser reporter = messageUserProcessor.getAuthorFromSender(message) ?: user
def sender = messageUserProcessor.getAuthorFromSender(message) as String
def project = projectManager.getProjectObjByKey("TY")
def issueObject = issueFactory.getIssue()
issueObject.setProjectObject(project)
// add "TO" info
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def TYtest = customFieldManager.getCustomFieldObjectByName("TYtest1")
issueObject.setCustomFieldValue(TYtest, addresses)
// add "TO" info
issueObject.setSummary(subject)
issueObject.setDescription(MailUtils.getBody(message))
issueObject.setIssueTypeId(project.issueTypes.find { it.name == "Mail" }.id)
issueObject.setReporter(reporter)
// add sender to custom field
def emailCf = customFieldManager.getCustomFieldObjectByName("E-Posta Adresiniz")
issueObject.setCustomFieldValue(emailCf, sender)
// add sender to custom field
issue = messageHandlerContext.createIssue(user, issueObject)
def attachments = MailUtils.getAttachments(message)
attachments.each { MailUtils.Attachment attachment ->
def destination = new File(jiraHome.home, FileService.MAIL_DIR).getCanonicalFile()
def file = FileUtils.getFile(destination, attachment.filename) as File
FileUtils.writeByteArrayToFile(file, attachment.contents)
messageHandlerContext.createAttachment(file, attachment.filename, attachment.contentType, user, issue)
}
// issue create and add attachment
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
getAuthorFromSender returns an ApplicationUser object which will be null if not a valid JIRA user.
I would instead use something like
def sender = message.getFrom() as String
that would actually give you the E-Mail Address of the Sender (actually the From: attribute, which is a bit different, but should serve your purpose!)
Let me know if not clear.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thanks David :) Works.
Only, There are problem for names.
for hotmail : [Nihat Test2 <nihattest2@hotmail.com>]
for gmail : [=?UTF-8?Q?Nihat_=C3=87etinkaya?= <nihat.cetinkaya@gmail.com>]
for company : [=?iso-8859-9?Q?Nihat_=C7etinkaya?= <nihat.cetinkaya@example.com.tr>]
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
If you need only the email address, you could extract the email by using regex and getting everything between < and > .
This regex would do that : (?<=\<).+?(?=\>)
I'll leave it up to you to find the proper implementation of this in JAVA :)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.