how to set customfield value post function javascript


I have a custom calculated field (Turn Around Time) which i use across different issue types and is calculated differently depending on which issue type it is. I have a javascript function to calculate turn around excluding weekends and i am wanting to add this to post function so the turn around is calculated when issue resolved.

I am having trouble finding out how to set the value of the custom field. I am also not sure if this script will even work (noob to javascript and how it works with JIRA's post function)

Is this right?

<script type="text/javascript">
//script calculator !
if (issue.get("issuetype") == "Specials") {
  issue.getelementbyid("customfield_10264").val(calculateworkingdays(issue.get("resolutiondate").getDate(), issue.get("customfield_10007").getDate()))
    } else {
issue.getelementbyid("customfield_10264").val(calculateworkingdays(issue.get("resolutiondate").getDate(), issue.get("customfield_10002").getDate()))   
// function
function calculateWorkingdays(stDate,enDate) {
var startDateYear = stDate.getFullYear();
var startDateMonth = stDate.getMonth();
var startDateDat = stDate.getDate();
var startDateDay = stDate.getDay();
var endDateYear = enDate.getFullYear();
var endDateMonth = enDate.getMonth();
var endDateDat = enDate.getDate();
var endDateDay = enDate.getDay();
ssDate = new Date(startDateYear,startDateMonth,startDateDat);
eeDate = new Date(endDateYear,endDateMonth,endDateDat);
if (ssDate > eeDate) {
workingDays = 0;
return workingDays;
workingDays = 0;
while (ssDate <= eeDate) {
if(ssDate.getDay()!=0 && ssDate.getDay()!=6) {
ssDate=new Date(ssDate.valueOf()+86400000);
// to make days between *** exclusive   *** of start/end dates uncomment the three lines below
//if ((startDateDay != 0) && (startDateDay != 6)) { workingDays -- }
//if ((endDateDay != 0) &&  (endDateDay != 6)) { workingDays -- }
//if (workingDays <0) {workingDays = 0}
return workingDays;

3 answers

0 votes
Steven Behnke Community Champion Sep 09, 2015

You've labeled this as Script Runner, but you used the verbage Calculated Field. Are you using fields of the 'Script Field' type (scriptrunner) or of the Calculated Date Field (misc calculated fields)? I cannot help you at all with Javascript in scriptrunner. Instead of this (the idea seems very convoluted and complicated) can you please describe what the value you're attempting to render in these fields, as a feature request or user story? I'm much better at that. :p

+1 to what Steven said. If you looking at calculating a period between two days in working days, there are quite a few examples for that on stackoverflow.

Here's an example we have used ourselves at Adaptavist:

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
// Get the Values of the Start date, End Date and Half Day fields and their values
def StartDate = getCustomFieldValue("Start date")
def EndDate = getCustomFieldValue("End date")
def halfday = getCustomFieldValue("Half Day")
//initialise some variables
float NumberOfDays = 0;
float TotalNumberOfDays = 0;
// Check if the user has requested a half day and if so set the time requested to half a day.
if (halfday != null) {
 TotalNumberOfDays = 0.5
 // If the user has not requested a half day calculate how many full days they have requested.
// Calculate the number of days that a user has requested
DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
Calendar cal1 = Calendar.getInstance();
Calendar cal2 = Calendar.getInstance();
 while (cal1.before(cal2)) {
 // If the days include weekends skip those days and do not count them
 if ((Calendar.SATURDAY != cal1.get(Calendar.DAY_OF_WEEK))
 &&(Calendar.SUNDAY != cal1.get(Calendar.DAY_OF_WEEK))) {
 }else {
 // Add on 1 day to include the start date which the calculation discards
 TotalNumberOfDays = NumberOfDays + 1
return TotalNumberOfDays.toString()

Suggest an answer

Log in or Sign up to answer
Community showcase
Published Nov 27, 2018 in Portfolio for Jira

Introducing a new planning experience in Portfolio for Jira (Server/DC)

In the past, Portfolio for Jira required a high degree of detail–foresight that was unrealistic for many businesses to   have–in   order to produce a reliable long-term roadmap. We're tur...

2,815 views 18 22
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you