Scriptrunner: how to set the first and last day of the month in an issue's customfields

Volodymyr
Contributor
October 17, 2023

Hi there,
I'd like to know if the following can be done:
When creating a ticket that has two customfields "Planned Start" and "Planned End", automatically set the first day of the month in the first field and the last day of the month in the second field regardless of the month in which the issue was created. The values should be of type Date.
So far I have no idea how this can be done or if it can be done at all, so I'd like to ask the community.
Maybe Calendar class can help with it.

If you have any ideas on how this can be done, please share.
Unfortunately, I have done very little with dates and time, but the feeling is that this case is doable.
Thank you.

2 answers

1 accepted

0 votes
Answer accepted
Peter-Dave Sheehan
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.
October 17, 2023

Automation is also available for Server/DC, you either need to get it as a separate app (or I think it might also be part of JSM).

But it should also definitely be possible with groovy/scriptrunner.

I have never liked working with the Calendar class.

Here is how I would calculate those dates:

 

def today = new Date()
def firstOfCurrentMonth = today.toLocalDate().withDayOfMonth(1).toDate().toTimestamp()
def lastOfCurrentMonth = today.toLocalDate().plusMonths(1).withDayOfMonth(1).minusDays(1).toDate().toTimestamp()

I'm outputting both as "timeStamp" since that's what date/time fields require

Volodymyr
Contributor
October 18, 2023

Thank you for correcting me.
Unfortunately, we don't have the plugin and won't be able to install it for our JIRA, so I'm using Scriptrunner as best I can.

Thank you very much for the script; it works great and fits my requester's requirements.
I've been experimenting with the methods and noticed that you can do more with Date than Calendar, but I could be wrong due to my ignorance.

My post function looks like this for those who are interested:

import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.component.ComponentAccessor

def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()

// The day and time when the issue is created
def today = new Date()

// Using the variable above, we can find the first and last days of the month
def firstDayOfCurrentMonth = today.toLocalDate().withDayOfMonth(1).toDate().toTimestamp()
def lastDayOfCurrentMonth = today.toLocalDate().plusMonths(1).withDayOfMonth(1).minusDays(1).toDate().toTimestamp()

// Customfields of type Date
def cfPlannedStart = ComponentAccessor.getCustomFieldManager()?.getCustomFieldObject(11700)
def cfPlannedEnd = ComponentAccessor.getCustomFieldManager()?.getCustomFieldObject(11701)

// Set the above values in these fields
issue.setCustomFieldValue(cfPlannedStart, firstDayOfCurrentMonth)
issue.setCustomFieldValue(cfPlannedEnd, lastDayOfCurrentMonth)

ComponentAccessor.getIssueManager().updateIssue(currentUser, issue, EventDispatchOption.ISSUE_UPDATED, false  

 

0 votes
C Madhuri October 17, 2023

Hi @Volodymyr 

Using Automation rules, you can achieve this.
Provide details like when and what conditions do you want to execute this functionality.

 

Madhuri C

Volodymyr
Contributor
October 17, 2023

Hi @C Madhuri
As far as I remember, Automation is for Cloud, while I'm using Server and will be migrating to Data Center.

Chinthalapalli Madhuri
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
October 18, 2023

Hi @Volodymyr 

You can install automation for Jira and it is a free app in both server and datacenter.

It will be easier to achieve this functionality using automation 

Volodymyr
Contributor
October 18, 2023

Hi @Chinthalapalli Madhuri
We are currently migrating to Data Center version, so my team isn't installing any plugins. Also, as far as I remember, A4J will be out of the box in the DC.

Suggest an answer

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

Atlassian Community Events