script include is only calculating Date but not Time

surajwipro
Tera Contributor
Hi All, I have 2 date field & requirement is my 2nd date field should be post 48 hours of 1st date field I have written script include & On change catalog client script But this Script include is only checking days & not hour for me. SI: getDifference: function(){ var startDate = new GlideDateTime(this.getParameter('sysparm_poweroff')); var endDate = new GlideDateTime(this.getParameter('sysparm_decommision')); var diffSeconds = gs.dateDiff(startDate.getDisplayValue(), endDate.getDisplayValue(), true); //var diffSeconds = gs.dateDiff(startDate, endDate, true); //var days = parseInt(diffSeconds)*60*24; //return days; var dur_hours = Math.round(diffSeconds / 3600); return dur_hours; }, CI: function onChange(control, oldValue, newValue, isLoading) { if (isLoading ) { return; } var ga = new GlideAjax('DecommissionServer'); ga.addParam('sysparm_name', 'getDifference'); ga.addParam('sysparm_poweroff', g_form.getValue('poweroff_time')); ga.addParam('sysparm_decommision', g_form.getValue('decommision_time')); ga.getXML(updateCampus); function updateCampus(response) { alert('tst3'); var answer = response.responseXML.documentElement.getAttribute("answer"); alert('tst4'); var test = answer; alert(test); if(answer < 49){ alert('tst5'); g_form.clearValue('decommision_time'); // give proper field name here } } }
1 ACCEPTED SOLUTION

palanikumar
Mega Sage

I suggest you to use UI Policy instead of this script

Create UI Policy with condition as below:

"startdate" "is less than" "48" "Hours" "before" "enddate"

 

Create UI Policy action with field as enddate and Clear value as checked (Keep Mandatory, Visible and Read Only as leave alone)

Go to Script tab and do the following

1) Check Run script check box

2) Write below script under Execute if true

function onCondition() {
g_form.showFieldMsg("fieldname","End date should be 48 hours after start data","error");

Thank you,
Palani

View solution in original post

1 REPLY 1

palanikumar
Mega Sage

I suggest you to use UI Policy instead of this script

Create UI Policy with condition as below:

"startdate" "is less than" "48" "Hours" "before" "enddate"

 

Create UI Policy action with field as enddate and Clear value as checked (Keep Mandatory, Visible and Read Only as leave alone)

Go to Script tab and do the following

1) Check Run script check box

2) Write below script under Execute if true

function onCondition() {
g_form.showFieldMsg("fieldname","End date should be 48 hours after start data","error");

Thank you,
Palani