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

Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,560,577
Community Members
 
Community Events
185
Community Groups

Using ScriptRunner Post Function to set issue custom date based on other custom date field

JIRA Version: v6.3.15 (JIRA Server)

Script Runner Version: 3.0.16

My Objective -

Add 1278 days (30 months) to a datepicker custom field "DateCF1" and store it to another custom date field, "DateCF2" with a post function. I don't want to change DateCF1, it is just the starting date for DateCF2.

}

I've "found" multiple solutions to this problem, none of which have worked. I don't know what I'm doing wrong.

I don't want to use a scripted field, as I'd like the project administrators to have the ability to edit DateCF2 after the transition, as needed.

My current approach has been "Add post function" -> "Script Post-Function" -> "Custom script post-function" -> "Inline script" ->

def dateField = getCustomFieldValue("DateCF1")
def number = 1278

result = dateField + number
setCustomFieldValue("DateCF2",new Date(result.getTime()))

Any help at all would be sincerely appreciated.

I don't have access to the log as I'm not a server admin, so my trial and error has been publishing the workflow and moving through the transition. 

Thank you in advance,

Craiu

4 answers

Another option I ended up to set date +N working days:

import com.atlassian.jira.component.ComponentAccessor
import java.sql.Timestamp
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.CustomFieldManager
import java.time.LocalDateTime
import com.atlassian.jira.timezone.TimeZoneManager
import com.atlassian.jira.util.DateFieldFormat
import java.time.DayOfWeek;
int businessDayToSkip = 5


def cfm = ComponentAccessor.getCustomFieldManager()

def currentDueDate = cfm.getCustomFieldObjectsByName("Current Due Date").getAt(0)


def timeZoneManager = ComponentAccessor.getComponent(TimeZoneManager)
def dateFieldFormat = ComponentAccessor.getComponent(DateFieldFormat)
def loggedUserZoneId = timeZoneManager.loggedInUserTimeZone.toZoneId()


LocalDateTime localDateTimeToSet = addWorkingDays(LocalDateTime.now(), businessDayToSkip)

issue.setCustomFieldValue(currentDueDate, Timestamp.valueOf(localDateTimeToSet))


LocalDateTime addWorkingDays(LocalDateTime date, int workdays) {
if (workdays < 1) {
return date;
}

LocalDateTime result = date;
int addedDays = 0;
while (addedDays < workdays) {
result = result.plusDays(1);
if (!(result.getDayOfWeek() == DayOfWeek.SATURDAY ||
result.getDayOfWeek() == DayOfWeek.SUNDAY)) {
++addedDays;
}
}

return result;
}

If anyone runs into a similar issue... I ended up just using the due date and not a custom field. I don't know what the syntax problem was but I gave up and just went a different direction.

This is something that finally works and relatively meets my needs:

import com.atlassian.jira.component.ComponentAccessor

def customFieldManager = ComponentAccessor.customFieldManager
def cfSD = customFieldManager.getCustomFieldObjectByName("Start Date")
def cfCT = customFieldManager.getCustomFieldObjectByName("Cycle Time")
def vCT = issue.getCustomFieldValue(cfCT) as Integer
def dateField = issue.getDueDate()

if (vCT != 0) {
if (dateField) {

result = dateField + 1
Calendar c1 = GregorianCalendar.getInstance();
c1.setTime(result)

while (vCT != 0) {
result = result - 1
c1.setTime(result)
while (c1.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY || c1.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
result = result - 1
c1.setTime(result)
}
vCT = vCT - 1
}

issue.setCustomFieldValue(cfSD, result)

}
}
0 votes
Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
Apr 09, 2018

I suspect it's working, but adding 1278 milliseconds to the date is not changing it enough for Jira to display any difference.

I still have found no solution to this.

I know there has to be some easy answer, but I can't seem to take a custom date field, add/subtract days to it, and then store that in another custom field.

Any help would be appreciated

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events