Can not compile a scripted job on Confluence
Asked by support for vo:

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
```

<br />

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


Answers:
author: Radek Dost?l
body: If it's saying "not allowed here", I'm going to go on a limb and say - try removing it.

comments: author: support for vo
body: 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) :(


author: Radek Dost?l
body: 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
```

Just took the example from <https://www.baeldung.com/java-get-week-number>  
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.


author: support for vo
body: 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 !


Tags: confluence-server

server