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
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 !
If it's saying "not allowed here", I'm going to go on a limb and say - try removing it.
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) :(
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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 !
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.