Home > Extensions > Web Resources > Scripts > Auto-Update Duration/End Time JScript

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.

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>

© 2011 MSCRM Blogger

This site does not necessarily represent the views of Microsoft. The material posted on this blog are provided AS-IS, and you are to use the content at your own risk. Some of the code and information on this site may use unsupported methods. If you experience any problems using the information provided through this website, please post a comment and we will do our best to assist you, however, we can in no way guarantee support or resolution.

Microsoft Dynamics®, Silverlight®, Visual Studio®, and the corresponding logos are a registered trademarks owned by Microsoft Corporation. MSCRM Blogger has made every effort to supply trademark information about company names, products, and services mentioned on this blog. All third party trademarks are the property of their respective owners. Any rights not expressly granted herein are reserved.