Hi Guys,
Need one help in completing scriptrunner mail handler.
I am trying to create a mail handler which add Email body in "Detailed Description" text field and set Enivornment select list field to "Production" , i tried to add the following script in mail handler but it's not working nor giving an error and not creating an issue when i am trying to add default value for these field. If i remove these highlighted linesthen only issue get created in project. but i want to set default values. Kindly suggest.
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
def userManager = ComponentAccessor.getComponent(UserManager)
def projectManager = ComponentAccessor.getProjectManager()
def issueFactory = ComponentAccessor.getIssueFactory()
def messageUserProcessor = ComponentAccessor.getComponent(MessageUserProcessor)
//text field detail description
def detail_description = getFieldByName("Detailed Description")
//mail data
def subject = message.getSubject() as String
def body = MailUtils.getBody(message)
def issue = ServiceUtils.findIssueObjectInString(subject)
//select list field Enivornment
def enviornment = getCustomFieldById("customfield_11755")
def optionsManager = ComponentAccessor.getOptionsManager()
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def customField = customFieldManager.getCustomFieldObject(enviornment.getFieldId())
def config = customField.getRelevantConfig(getIssueContext())
def options = optionsManager.getOptions(config)
def optionToSelect = options.find { it.value == "Production" }
ApplicationUser user = userManager.getUserByName("vikrant-yadav2")
ApplicationUser reporter = messageUserProcessor.getAuthorFromSender(message) ?: user
def project = projectManager.getProjectObjByKey("HBBOP")
def issueObject = issueFactory.getIssue()
issueObject.setProjectObject(project)
issueObject.setSummary(subject)
issueObject.detail_description.setFormValue(body)
issueObject.enviornment.setFormValue(optionToSelect.optionId)
issueObject.setIssueTypeId(project.issueTypes.find { it.name == "Bug" }.id)
issueObject.setReporter(reporter)
messageHandlerContext.createIssue(user, issueObject)
Upon viewing your code, I can already say that your approach will not work.
First of all, the way you have declared the custom field Detail Description and the environment field is incorrect. The approach you have taken is:-
def detail_description = getFieldByName("Detailed Description")
def environment = getFieldById("customfield_11755)
This methods getFieldByName() and getFieldById() only work for the ScriptRunner Behaviour. Hence, this approach can only work on the Behaviour and not MailHandler.
You should instead declare the field using the CustomFieldManager object as shown below:-
def detailedDescription = customFieldManager.getCustomFieldObjectsByName('Detailed Description').first()
def environment = customFieldManager.getCustomFieldObject(11755)
I also notice you are using:-
def customField = customFieldManager.getCustomFieldObject(enviornment.getFieldId())
def config = customField.getRelevantConfig(getIssueContext())
def options = optionsManager.getOptions(config)
Hi @Ram Kumar Aravindakshan _Adaptavist_ Thanks for the reponse :)
I am the using following script, working fine for Detailed description text field but not working for Environment select list field and default repoter for non-jira users otherwise senders will be the repoter.
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 def userManager = ComponentAccessor.getComponent(UserManager) def projectManager = ComponentAccessor.getProjectManager() def issueFactory = ComponentAccessor.getIssueFactory() def messageUserProcessor = ComponentAccessor.getComponent(MessageUserProcessor) def detail_description = ComponentAccessor.customFieldManager.getCustomFieldObjectByName("Detailed Description") def envionment = getFieldByName("Environment") def optionsManager = ComponentAccessor.getOptionsManager() def customFieldManager = ComponentAccessor.getCustomFieldManager() def customField = customFieldManager.getCustomFieldObject(envionment.getFieldId()) def config = customField.getRelevantConfig(getIssueContext()) def options = optionsManager.getOptions(config) def optionToSelect = options.find{ it.value == "Production" } def subject = message.getSubject() def body = MailUtils.getBody(message) def issue = ServiceUtils.findIssueObjectInString(subject) ApplicationUser user = userManager.getUserByName("vikrant-yadav2") ApplicationUser reporter = messageUserProcessor.getAuthorFromSender(message) ?: user def project = projectManager.getProjectObjByKey("HBBOP") def issueObject = issueFactory.getIssue() issueObject.setProjectObject(project) issueObject.setSummary(subject) issueObject.setCustomFieldValue(detail_description, body) issueObject.setCustomFieldValue(envionment.setFormValue(optionToSelect.optionId)) issueObject.setCustomFieldValue() issueObject.setIssueTypeId(project.issueTypes.find{ it.name == "Bug" }.id) issueObject.setReporter(reporter) messageHandlerContext.createIssue(user, issueObject)
Thanks
V.Y
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
In your latest code update, you have still used the getFieldByName method when you are trying to invoke the environment field, i.e.
def envionment = getFieldByName("Environment")
As mentioned in my previous comment, this will not work in the Mail Handler as the getFieldByName method is only usable in the Behaviour.
It would help if you instead tried something like this:-
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
//This requires the name of the user that will trigger the Mail Handler
final def username = 'admin'
//This requires the Project's Key that will be used for the Mail Handler
final def projectKey = 'MOCK'
//This requires the name of the Issue Type that will be used
final def issueTypeName = 'Bug'
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
def project = projectManager.getProjectObjByKey(projectKey)
def subject = message.subject as String
def messageBody = MailUtils.getBody(message)
def multiLineText = customFieldManager.getCustomFieldObjectsByName('Multi Line Text').first()
def sampleList = customFieldManager.getCustomFieldObjectsByName('Sample List').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(issueReporter)
issue = messageHandlerContext.createIssue(user, issueObject) as MutableIssue
issue.setCustomFieldValue(multiLineText, messageBody)
def fieldConfig = sampleList.getRelevantConfig(issue)
def options = optionsManager.getOptions(fieldConfig)
def option = options.find{it.value == "Option1"}
issue.setCustomFieldValue(sampleList, option)
issueManager.updateIssue(user, issue, EventDispatchOption.DO_NOT_DISPATCH,false)
Please note that the sample code above is not 100% exact to your environment. Hence, you will need to make the required modifications.
If you observe in the code, I have used two-sample custom fields, i.e. Multi Line Text and Sample List and have used the customFieldManager to declare them as shown below:-
def multiLineText = customFieldManager.getCustomFieldObjectsByName('Multi Line Text').first()
def sampleList = customFieldManager.getCustomFieldObjectsByName('Sample List').first()
Next, I have initialised the issue object using:-
issue = messageHandlerContext.createIssue(user, issueObject) as MutableIssue
And, to pass the value of the mail body to the multiLineText field, I have used this:-
issue.setCustomFieldValue(multiLineText, messageBody)
To pass the value to the sampleList object, I have used:-
def fieldConfig = sampleList.getRelevantConfig(issue)
def options = optionsManager.getOptions(fieldConfig)
def option = options.find{it.value == "Option1"}
issue.setCustomFieldValue(sampleList, option)
And finally, to store all the changes made to the custom fields, I have used:-
issueManager.updateIssue(user, issue, EventDispatchOption.DO_NOT_DISPATCH,false)
Below is a screenshot of the expected result you should get:-
I hope this helps to answer your question. :)
Thank you and Kind Regards
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi @Ram Kumar Aravindakshan _Adaptavist_ Thanks for lot as always.
I have replaced Envionment name with field id because we have 4 field with the same name.
I have added one more thing Acknowledgement email to sender, but it's not working, can you check the script what's wrong with email this issue section ?
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.mail.Email
import com.atlassian.mail.server.MailServerManager
import com.atlassian.mail.server.SMTPMailServer
//This requires the name of the user that will trigger the Mail Handler
final def username = 'vikrant-yadav2'
//This requires the Project's Key that will be used for the Mail Handler
final def projectKey = 'HBBOP'
//This requires the name of the Issue Type that will be used
final def issueTypeName = 'Bug'
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
def project = projectManager.getProjectObjByKey(projectKey)
def subject = message.subject as String
def messageBody = MailUtils.getBody(message)
def multiLineText = customFieldManager.getCustomFieldObjectsByName('Detailed Description').first()
def sampleList = customFieldManager.getCustomFieldObject(11755)
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(issueReporter)
issue = messageHandlerContext.createIssue(user, issueObject) as MutableIssue
issue.setCustomFieldValue(multiLineText, messageBody)
def fieldConfig = sampleList.getRelevantConfig(issue)
def options = optionsManager.getOptions(fieldConfig)
def option = options.find{it.value == "Production"}
issue.setCustomFieldValue(sampleList, option)
issueManager.updateIssue(user, issue, EventDispatchOption.DO_NOT_DISPATCH,false)
//Email this issue
def body = "Testing"
def emailAddr = MailUtils.getSenders(message) as String
def sendEmail(String emailAddr, String subject, String body) {
def mailServer = ComponentAccessor.getMailServerManager().getDefaultSMTPMailServer()
if (mailServer) {
Email email = new Email(emailAddr);
email.setMimeType("text/html")
email.setSubject(subject);
email.setBody(body);
mailServer.send(email);
log.error("Mail sent")
} else {
log.warn("Please make sure that a valid mailServer is configured")
}
}
sendEmail (emailAddr,subject, body)
Thanks
V.Y
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
If you want to send an email, your approach will not work.
It would help if you used something like:-
final static sendEmail(String to, String subject, String content) {
def mailServerManager = ComponentAccessor.mailServerManager
def mailServer = mailServerManager.defaultSMTPMailServer
def email = new Email(to)
email.setSubject(subject)
email.setBody(content)
email.setMimeType("text/html")
def threadClassLoader = Thread.currentThread().contextClassLoader
Thread.currentThread().contextClassLoader = mailServer.class.classLoader
mailServer.send(email)
Thread.currentThread().contextClassLoader = threadClassLoader
}
sendEmail(emailAddr, subject, body)
Since Jira 8.10, you need to include the Thread to handle the sending of your mail.
I hope this helps to solve your question. :)
Thank you and Kind regards,
Ram
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi @Ram Kumar Aravindakshan _Adaptavist_ Yeah, it's almost solved,only mail part is not working. What's wrong with the following script ? Script is showing green dot but not working.
def body = "Test Email"
def emailAddr = MailUtils.getSenders(message) as String
final static sendEmail(String to, String subject, String content) {
def mailServerManager = ComponentAccessor.mailServerManager
def mailServer = mailServerManager.defaultSMTPMailServer
def email = new Email(to)
email.setSubject(subject)
email.setBody(content)
email.setMimeType("text/html")
def threadClassLoader = Thread.currentThread().contextClassLoader
Thread.currentThread().contextClassLoader = mailServer.class.classLoader
mailServer.send(email)
Thread.currentThread().contextClassLoader = threadClassLoader
}
sendEmail(emailAddr, subject, body)
Thanks
V.Y
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi @Ram Kumar Aravindakshan _Adaptavist_ All set now.
Thanks a lot again for helping me as always.
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.