Find your people. Pick a challenge. Ship something real. The CreatorCon Hackathon is coming to the Community Pavilion for one epic night. Every skill level, every role welcome. Join us on May 5th and learn more here.

Variable Date no less than 5 business days

booher04
Tera Guru

We have a need to require a requested_completion_date to be at least 5 business days, if you select a date less than 5 business days you get an error and it clears the field.  My question is do I have to write a script include with this or can I write an onChange client script to accomplish this?  The schedule we use is '8-8 weekdays excluding holidays'.  

 

1 ACCEPTED SOLUTION

booher04
Tera Guru

Resolved:

onChange Client Script:

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}

// Call the GA function
var ga = new GlideAjax('u_userInfoAjax');
ga.addParam('sysparm_name', "getInfo");
ga.addParam('sysparm_date', g_form.getValue('requested_completion_date'));
ga.getXMLAnswer(function(answer){
if(answer == 'true'){
alert('Please select date after 5 business days');
g_form.clearValue('requested_completion_date');
}

});

}

 

Script Include:

var u_userInfoAjax = Class.create();
u_userInfoAjax.prototype = Object.extendsObject(AbstractAjaxProcessor, {

getInfo: function(){

var dateSelected = this.getParameter('sysparm_date');

var nowDateTime = new GlideDateTime();

var days = 5;

//var dur = new GlideDuration(60*60*24*days*1000);
var dur = new GlideDuration(days*43200*1000);

// paste the sys_id of the 8*8 weekday schedule excluding holidays

var schedule = new GlideSchedule('2218ff1bdba8eb40fb6e753a8c96198d'); //8-8 weekdays excluding holidays

var finalTime = schedule.add(nowDateTime, dur,'');

var updatedGdt = new GlideDateTime(dateSelected);

var finalTimeGdt = new GlideDateTime(finalTime);

if(updatedGdt < finalTimeGdt){

return 'true';

}
return 'false';
},

type: 'u_userInfoAjax'

});

View solution in original post

19 REPLIES 19

Added that script and script include... now it does nothing.  I dont get the alert at all. 

booher04
Tera Guru

Resolved:

onChange Client Script:

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}

// Call the GA function
var ga = new GlideAjax('u_userInfoAjax');
ga.addParam('sysparm_name', "getInfo");
ga.addParam('sysparm_date', g_form.getValue('requested_completion_date'));
ga.getXMLAnswer(function(answer){
if(answer == 'true'){
alert('Please select date after 5 business days');
g_form.clearValue('requested_completion_date');
}

});

}

 

Script Include:

var u_userInfoAjax = Class.create();
u_userInfoAjax.prototype = Object.extendsObject(AbstractAjaxProcessor, {

getInfo: function(){

var dateSelected = this.getParameter('sysparm_date');

var nowDateTime = new GlideDateTime();

var days = 5;

//var dur = new GlideDuration(60*60*24*days*1000);
var dur = new GlideDuration(days*43200*1000);

// paste the sys_id of the 8*8 weekday schedule excluding holidays

var schedule = new GlideSchedule('2218ff1bdba8eb40fb6e753a8c96198d'); //8-8 weekdays excluding holidays

var finalTime = schedule.add(nowDateTime, dur,'');

var updatedGdt = new GlideDateTime(dateSelected);

var finalTimeGdt = new GlideDateTime(finalTime);

if(updatedGdt < finalTimeGdt){

return 'true';

}
return 'false';
},

type: 'u_userInfoAjax'

});

Hi,

I think you have taken 8-8 means 12 hours and hence

60*60*12 = 43200

Did you accidentally mark your own answer as correct as compared to the one which helped in scripting

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 10x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Hi Ankur,

 I tried the proposed solution, But script include always return true only.

I observe date formate difference.

Could you please help me how to resolve this issue.

Uma2_0-1672067604421.png

 

getInfo: function(){

var dateSelected = this.getParameter('sysparm_date');
var nowDateTime = new GlideDateTime();

var days = 5;

var dur = new GlideDuration(60*60*24*days*1000);
// paste the sys_id of the 8*5 weekday schedule excluding holidays and weekends 090eecae0a0a0b260077e1dfa71da828

var schedule = new GlideSchedule('090eecae0a0a0b260077e1dfa71da828');
var finalTim = schedule.add(nowDateTime, dur,'');
gs.info("check dates"+"---"+date2);

var updatedGdt = new GlideDateTime(dateSelected);
var finalTimeGdt = new GlideDateTime(finalTim);
gs.info("check dates"+'---==='+updatedGdt+'---'+dateSelected+"---=="+finalTimeGdt+"----"+finalTim(2));
if(dateSelected < finalTimeGdt){
return 'true';
}
return 'false';

}

 

Hi Ankur,

 Colud you please help me how to convert the date formates.

I have similar requirement, I used same code but slected date and current date has different date formate.

Uma2_0-1672126882775.png