Calculating a duration and setting that value using a client script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-22-2013 10:30 AM
I saw this script on the community and thought I would give it a try - but it doesn't seem to work.
We are looking to have the duration field automatically populate based on what is entered on the "planned start time" and "planned end time" field.
--Create an 'onChange' client script that includes the following code.
function onChange(control, oldValue, newValue, isLoading) {
var strt = g_form.getValue('');
var end = g_form.getValue('');
var ajax = new GlideAjax('AjaxDurCalc');
ajax.addParam('sysparm_name','durCalc');
ajax.addParam('sysparm_strt',strt);
ajax.addParam('sysparm_end',end);
ajax.getXMLWait();
var answer = ajax.getAnswer();
g_form.setValue('', answer);
}
--Create a system script include file called AjaxDurCalc that handles the request.
var AjaxDurCalc = Class.create();
AjaxDurCalc.prototype = Object.extendsObject(AbstractAjaxProcessor, {
durCalc: function() {
return gs.dateDiff(this.getParameter('sysparm_strt'),this.getParameter('sysparm_end'), false);
}
});
http://wiki.servicenow.com/index.php?title=Setting_the_Duration_Field_Value#Calculating_a_duration_a...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-22-2013 04:36 PM
So, I'll start with the fix. The script you have pasted is missing the field names in 3 places. When you use g_form.getValue() you have to pass in the field name that it should lookup the value for. In this example it will probably be start_date and end_date like below but you can verify by right clicking on the field label and hitting the "Show - " menu item.
var strt = g_form.getValue('start_date');
var end = g_form.getValue('end_date');
The third line is the setValue line. This is also missing the field name. It should read something like the ex below where u_duration is the field you want to populate the value of the duration to.
g_form.setValue('u_duration', answer);
All that said I wouldn't set it up this way. I had it ground in by a member of our implementation team that client scripts should be a last resort. The only time they should be used is if you NEED the form to change based on user input. In this case I would guess you would be fine with the duration field getting populated when they hit save/submit.
to do that I would run a business rule on insert/update like the one below. (I'm making the assumption that your duration field is a type of duration)
var diff = gs.dateDiff(current.start_date.getDisplayValue(), current.end_date.getDisplayValue(), false);
current.u_duration = diff;
**edit attached screenshot of the business rule setup
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-23-2013 08:44 AM
Hi John,
Thanks for the feedback and providing your script.
We would like the duration field to be automatically calculated and populated when the Start and End Date is entered. With a Business Rule, the field only gets populated after the record is saved.
I just copied what was in the wiki article for this comment post, but I did account for the g_form.getValue() field value, but the script does not work. This is the error msg that I get:
----
onChange script error: TypeError: Cannot call method 'indexOf' of null function onChange_change_request_end_date_0(control, oldValue, newValue, isLoading, isTemplate) { if (isLoading || newValue == '') { return; } var strt = g_form.getValue('start_date'); var end = g_form.getValue('end_date'); var ajax = new GlideAjax('AjaxDurCalc'); ajax.addParam('sysparm_name','durCalc'); ajax.addParam('sysparm_strt',strt); ajax.addParam('sysparm_end',end); ajax.getXMLWait(); var answer = ajax.getAnswer(); g_form.setValue('u_estimated_duration', answer); }
----