Missed Team ’24? Catch up on announcements here.

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

Groovy script ClassCastException when creating issue from Service Desk portal

Boyan Angelov
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 22, 2015

Hello,

I have a Groovy script executed as a post-function on the Create Issue transition, which is supposed to send a mail to the reporter if certain condition is met. When I create issue from the normal JIRA Create button, it works fine. When I create the issue from the Service Desk customer portal though (JIRA Service Desk version is 2.2), it does not work and in the atlassian-jira.log I get the following exception:

javax.script.ScriptException: javax.script.ScriptException: java.lang.ClassCastException: com.sun.mail.handlers.text_plain cannot be cast to javax.activation.DataContentHandler
at com.onresolve.scriptrunner.runner.ScriptRunnerImpl.runFileAsScript(ScriptRunnerImpl.groovy:194)
at com.onresolve.scriptrunner.runner.ScriptRunner$runFileAsScript$8.call(Unknown Source)
at com.onresolve.scriptrunner.canned.jira.utils.CustomScriptDelegate.doScript(CustomScriptDelegate.groovy:47)
at com.onresolve.scriptrunner.canned.jira.utils.CustomScriptDelegate$doScript$11.call(Unknown Source)
at com.onresolve.scriptrunner.canned.jira.workflow.postfunctions.CustomScriptFunction.doScript(CustomScriptFunction.groovy:20)
Caused by: javax.script.ScriptException: java.lang.ClassCastException: com.sun.mail.handlers.text_plain cannot be cast to javax.activation.DataContentHandler
... 5 more
Caused by: java.lang.ClassCastException: com.sun.mail.handlers.text_plain cannot be cast to javax.activation.DataContentHandler
at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1121)
at com.atlassian.mail.server.impl.SMTPMailServerImpl.sendWithMessageId(SMTPMailServerImpl.java:175)
at com.atlassian.mail.server.impl.SMTPMailServerImpl.send(SMTPMailServerImpl.java:150)
at Script18.run(Script18.groovy:74)
... 5 more

 

Here is the Groovy script I execute:

if (totalAmount >= 500) {
    // Setup mail server
    MailServerManager mailServerManager = componentManager.getMailServerManager();
    SMTPMailServer mailServer = mailServerManager.getDefaultSMTPMailServer();
   
    reporter = userUtil.getUserByName(someIssue.reporter.name); 
    reporterEmail = reporter.getEmailAddress();
    reporterFirstName = reporter.displayName;
 
    if (mailServer) {
            Email email = new Email(reporterEmail);
            email.setSubject("Monthly limit exceeded");
            email.setFrom("IT Support <some@mail.com>");
            String content = """\
Hello ${reporterFirstName},
 
Your purchase request is exceeding the monthly limit. Your total purchases this month are ${totalAmount}. 
IT Help Desk
""";
 
            email.setBody(content)
            mailServer.send(email)
        }
        else {
            log.error "[Send Survey Cron] No SMTP mail server defined.";
        }    
    }

Obviously there is some difference in the way issues are created from JIRA and from Service Desk, but I have no idea where is the problem here. Any help is much appreciated!

1 answer

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

0 votes
Answer accepted
Boyan Angelov
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 27, 2015

Well, I changed the way the e-mail is sent and that fixed the problem. I switched to using the Mail Queue, here is the code:

if (totalAmount >= 500) {
		// Setup mail server
        SMTPMailServer mailServer = MailFactory.getServerManager().getDefaultSMTPMailServer();
    
        reporter = userUtil.getUserByName(someIssue.reporter.name);
        reporterEmail = reporter.getEmailAddress();
        reporterFirstName = reporter.displayName;
 
        if (mailServer) {
            Email email = new Email(reporterEmail);
            email.setSubject("Monthly limit exceeded");
            email.setFrom("IT Support <some@mail.com>");
			email.setMimeType("text/html");
            String content = """\
Hello ${reporterFirstName},
 
Your purchase request is exceeding the monthly limit. Your total purchases this month are ${totalAmount}. 
IT Help Desk
some@mail.com
""";
 
            email.setBody(content)
			SingleMailQueueItem item = new SingleMailQueueItem(email)
			ComponentAccessor.getMailQueue().addItem(item);
        }
        else {
            log.error "[Send Survey Cron] No SMTP mail server defined.";
        }    
    }

 

 

JamieA
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 20, 2015

Glad you found a way. Using the mail queue is more reliable anyway.

TAGS
AUG Leaders

Atlassian Community Events