Are you in the loop? Keep up with the latest by making sure you're subscribed to Community Announcements. Just click Watch and select Articles.

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

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

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

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
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
Oct 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

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  

 

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

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!
Oct 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 

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