how to set customfield value post function javascript

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

This widget could not be displayed.
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

This widget could not be displayed.

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

This widget could not be displayed.

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()

Suggest an answer

Log in or Sign up to answer
Community showcase
Posted Tuesday in Jira

What modern development practices are at the heart of how your team delivers software?

Hey Community mates! Claire here from the Software Product Marketing team. We all know software development changes rapidly, and it's often tough to keep up. But from our research, we've found the h...

113 views 1 2
Join discussion

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