- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-02-2022 11:50 PM
Hello Guys,please help me to achieve this,
For my custom table, i have created two date/time type of fields which are Start Date and End Date.
I have created third field also named as Duration which is of duration type
Now, whenever value of End Date field changes on the form, my duration field should populate the difference between Start Date and End Date in duration form( days and difference in time)
To achieve this i have written a script include and called it from onchange type of client script(Field name= End Date),
still my Duration field is not getting populated as i desired
please see my script and help me out, Thanks in advance!!
Script Include:
var DurationCalculation = Class.create();
DurationCalculation.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getDuration: function() {
var dateTimediff;
var date1= new GlideDateTime();
var date2= new GlideDateTime();
var fromdate= this.getParameter('sysparm_from_date');
var todate= this.getParameter('sysparm_to_date');
date1.setValue(fromdate);
date2.setValue(todate);
dateTimediff=GlideDateTime.substract(date1,date2);
return dateTimediff.getDisplayvalue();
},
type: 'DurationCalculation'
});
Client script:(Type- onChange and field name-End Date)
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var fromdate = g_form.getValue('u_start_date');
var todate = g_form.getValue('u_end_date');
var ga= new GlideAjax('DurationCalculation');
ga.addParam('sysparm_name','getDuration');
ga.addParam('sysparm_from_date',fromdate);
ga.addParam('sysparm_to_date',todate);
ga.getXML(CBF);
function CBF(response){
var answer= response.responseXML.documentElement.getAttribute("answer");
g_form.setValue('u_duration',answer);
}
}
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-03-2022 12:07 AM
Hi,
update as this
var DurationCalculation = Class.create();
DurationCalculation.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getDuration: function() {
var dateTimediff;
var date1= new GlideDateTime();
var date2= new GlideDateTime();
var fromdate= this.getParameter('sysparm_from_date');
var todate= this.getParameter('sysparm_to_date');
date1.setValue(fromdate);
date2.setValue(todate);
dateTimediff = GlideDateTime.substract(date1,date2);
return dateTimediff.getDurationValue();
},
type: 'DurationCalculation'
});
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-03-2022 12:18 AM
Did I answer your original question?
If yes then please mark my response as correct and helpful to close the thread.
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-03-2022 12:19 AM
i have market it as correct already
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-03-2022 12:22 AM
Hi,
yes no script include required
it would be like this
var date1= new GlideDateTime();
var date2= new GlideDateTime();
var fromdate = current.u_start_date;
var todate = current.u_end_date;
date1.setValue(fromdate);
date2.setValue(todate);
current.u_duration = gs.dateDiff(date1, date2, false);
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-03-2022 01:02 AM
i have written after business rule on insert, still not getting duration field value ,please see this code
(function executeRule(current, previous /*null when async*/ ) {
var fromdate = new GlideDateTime(current.getValue('u_start_date')); //to get start date value
var todate = new GlideDateTime(current.getValue('u_end_date')); //to get end date value
var duration = new GlideDateTime.subtract(fromdate,todate); //difference between start date and end date
current.u_duration = duration.getDurationValue(); //to update duration field
})(current, previous);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-04-2022 07:57 PM
Hi,
update as this
(function executeRule(current, previous /*null when async*/ ) {
var fromdate = new GlideDateTime(current.getValue('u_start_date')); //to get start date value
var todate = new GlideDateTime(current.getValue('u_end_date')); //to get end date value
var duration = GlideDateTime.subtract(fromdate,todate); //difference between start date and end date
current.u_duration = duration; //to update duration field
})(current, previous);
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
