how to set customfield value post function javascript

tom webb September 9, 2015

Hi,

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) {
workingDays++;
}
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;
}
</script>

3 answers

0 votes
Mark McCormack _Adaptavist_
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 5, 2015

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.
}else{
// 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();
cal1.setTime(StartDate);
cal2.setTime(EndDate);
  
 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))) {
 NumberOfDays++;
 cal1.add(Calendar.DATE,1);
 }else {
 cal1.add(Calendar.DATE,1);
 }
 }
 
 // Add on 1 day to include the start date which the calculation discards
 TotalNumberOfDays = NumberOfDays + 1
}
return TotalNumberOfDays.toString()
0 votes
JamieA
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.
September 9, 2015

+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.

0 votes
Steven F Behnke
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.
September 9, 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

Suggest an answer

Log in or Sign up to answer