Can not compile a scripted job on Confluence

support for vo August 2, 2022

Hello Community,

I'm writing to you as I have an issue on a job on Confluence.
My usecase : send automatically a Confluence page by email with the current Week number into the object.

Here is my code (ScriptRunner job) :

// Confluence imports
import com.atlassian.sal.api.component.ComponentLocator
import com.atlassian.mail.Email
import com.atlassian.mail.server.SMTPMailServer
import com.atlassian.confluence.mail.ConfluenceMailServerManager
import java.util.Calendar
   public static final int WEEKS = Calendar.WEEK_OF_YEAR

// Get Week number
def week = WEEKS

// Mail
def subject = "subject W${week}"
def body = "body"
def emailAddr = "email"

def sendEmail(String emailAddr, String subject, String body) {
def confluenceMailServerManager = ComponentLocator.getComponent(ConfluenceMailServerManager)
SMTPMailServer mailServer = confluenceMailServerManager.getDefaultSMTPMailServer();
    if (mailServer) {
        Email email = new Email(emailAddr);
        email.setSubject(subject);
        email.setBody(body);
        mailServer.send(email);
    } else {
        // Error to get the mail server from Confluence configuration :
        throw new RuntimeException("no mail server!");
    }
}
sendEmail(emailAddr, subject, body)

But the script can not be compiled, as I have this issue :

org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
Script1434.groovy: 16: Modifier 'public' not allowed here.
 @ line 16, column 4.
      public static final int WEEKS = Calendar.WEEK_OF_YEAR
      ^

Script1434.groovy: 16: Modifier 'static' not allowed here.
 @ line 16, column 4.
      public static final int WEEKS = Calendar.WEEK_OF_YEAR

 
Does anyone have any idea why I can not compile ?
Thanks for any help !

1 answer

1 accepted

1 vote
Answer accepted
Radek Dostál
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.
August 2, 2022

If it's saying "not allowed here", I'm going to go on a limb and say - try removing it.

support for vo August 2, 2022

Hello,

Indeed, but when I remove :

public static final int WEEKS = Calendar.WEEK_OF_YEAR

The Week number I have in the subject is not good ("3" instead of current 31) :(

Radek Dostál
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.
August 2, 2022

Well, that is working as intended. Calendar.WEEK_OF_YEAR is just a static integer used for getters and setters. In other words, Calendar as class itself does not have any idea what date and time it is, until you construct it.

 

You need to first create a Calendar instance (unless specified then by default that's the current date), and then you can get the week number from it, like so:

Calendar calendar = Calendar.getInstance(Locale.ITALY); 

int weekOfYear = calendar.get(Calendar.WEEK_OF_YEAR);

//31
java.util.Calendar itself is older and most tutorials will recommend to use more modern stuff, but it will get the job done just fine for what you are using it.
support for vo August 2, 2022

I didn't expect such a quick answer !
Now I fully understand where my issuecame from, when I use :

Calendar calendar = Calendar.getInstance(Locale.FRANCE)
int week = calendar.get(Calendar.WEEK_OF_YEAR)

It works perfectly.
Thank you very much for taking time to reply, it's greatly appreciated.

See you soon on the Community !

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
SERVER
TAGS
AUG Leaders

Atlassian Community Events