Sep 282009
 

Sometimes you need to be able to have preset values, and have the duration auto-update. In this example, I will make the actual durations auto-update when one of the fields is changed. If the duration is changed, then the end date will be changed. Special thanks to Stephan Bayer for thinking of the idea. We worked together to bring you this implementation of the Auto-Update Duration/End Time JScript.

Check out the video of how it works

Granted, in my implementation I added a field for time deductions, but I have commented it out in the code. If you add a duration, just change the script to use that field. Also, please make sure to add the fields to the required parameters for the event. I would hate for someone’s code to stop working because a field was removed from the form.

This code is for the Start, End, and Deduct (if you have it) on change events:

// Get End and Start
var start = crmForm.all.actualstart.DataValue;
var end = crmForm.all.actualend.DataValue;
// Can't calculate without any data
if (start==undefined || start==null || end==undefined || end==null) {
	return;
}

// Get 1 day in milliseconds
var one_minute=1000*60;

// Get time in days...
var time = (end-start)/one_minute;

/*
// Get the deduction
var deduct = crmForm.all.it_timeentry_deduct.DataValue
if (deduct!=undefined && deduct!=null)
{
  time = time - deduct;
}
*/

// Can't be less than 0.
if (time<0) time=0;

// Set the duration
crmForm.all.actualdurationminutes.DataValue = time;

This code is for the onchange event of the Duration field:

var start = crmForm.all.actualstart.DataValue;
var duration = crmForm.all.actualdurationminutes.DataValue;
// Can't calculate without any data
if (start==undefined || start==null || duration==undefined || duration==null) 
{
	return;
}

var end = start;
end.setMinutes(end.getMinutes()+duration);

/*
var deduct = crmForm.all.it_timeentry_deduct.DataValue
if (deduct!=undefined && deduct!=null)
{
  end.setMinutes(end.getMinutes()+deduct);
}
*/

crmForm.all.actualend.DataValue=end;

To preset the duration, start time and end time, I added the following script to the form’s onload event

var CRM_FORM_TYPE_CREATE = 1;
var CRM_FORM_TYPE_UPDATE = 2;

if (crmForm.FormType==CRM_FORM_TYPE_CREATE)
{
	var duration = 10;
	var start = new Date();
	crmForm.all.actualstart.DataValue = start;
	crmForm.all.actualdurationminutes.DataValue = duration;
	var start = crmForm.all.actualstart.DataValue;

	var end = start;
	end.setMinutes(end.getMinutes()+duration);

	crmForm.all.actualend.DataValue=end;
}

If you have any questions about this implementation or would like assistance implementing it somewhere, please post a comment and let us know.

  2 Responses to “Auto-Update Duration/End Time JScript”

Comments (2)
  1. Thanks for the great post – this is exactly what I am trying to create in my CRM however, we have CRM 2011. Do you have an updated script that can be used with the new version of CRM?

 Leave a Reply

(required)

(required)

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>