- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-07-2022 01:29 AM
Hello Everyone,
Please help me with this usecase.
Create new variable type - Date,
Name - Implementation Date.
Validation - Date selected should be within 5 business days.
If not,
show error message and clear value.
note:If i select a date that is on saturday or sunday if should show error message and clear the value. The five business days should only get inserted in the catalog item.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-07-2022 02:00 AM
Hi
Please refer below Client script to get day of Week. Replace date with your date field.
You can use this in the UI policy also.
var df = g_form.getValue('u_date');
var op=df.split('/');
var ress=op[2]+'/'+op[1]+'/'+op[0];
var objdate=new Date(ress);
alert(objdate);
var selday=objdate.getDay();
var res = parseInt(selday);
alert(res);
if(res==0||res==6)
{
alert('select business day');
}
Thanks,
Please mark the answer as correct and helpful.. If answer is feasible.
Please mark the answer as helpful and correct.
Best Regards,
Rajat Choudhary
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-07-2022 04:07 AM
Is this possible without script can we use ui policies for this.
if yes can you please help me.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-07-2022 06:07 AM
Please mark the answer as helpful and correct.
Best Regards,
Rajat Choudhary
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-07-2022 06:48 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-07-2022 10:55 AM
Hi
I saw your query below as well , and I can say that you will need a Script here to achieve your Requirement.
Calculating Business Days is not possible without script as far as I know.
Please follow the steps below to achieve your requirement:
1) Create a Client Callable Script Include and use the script as below:
var ClientDateTimeUtils = Class.create();
ClientDateTimeUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
//Takes a Single Date/Time Field and returns its time difference from nowDateTime().
//params = sysparm_fdt (the first date/time field), sysparm_difftype (time based format to return result. See "_calcDateDiff" function comments)
getNowDateTimeDiff: function(){
var firstDT = this.getParameter('sysparm_fdt'); //First Date-Time Field
var diffTYPE = this.getParameter('sysparm_difftype'); // Date-Time Type to return the answer as. Can be second, minute, hour, day
var diff = gs.dateDiff(gs.nowDateTime(), firstDT, true);
var timediff = this._calcDateDiff(diffTYPE, diff);
//return "getNowDateTimeDiff: FIRST DT: " + firstDT + " -DIFFTYPE: " + diffTYPE + " -TIME DIFF: " + timediff;
return timediff;
},
//Private function to calculate the date difference return result in second, minute, hour, day.
_calcDateDiff: function(diffTYPE, seconds){
var thisdiff;
if (diffTYPE == "day"){thisdiff = seconds/86400;}
else if (diffTYPE == "hour"){thisdiff = seconds/3600;}
else if (diffTYPE == "minute"){thisdiff = seconds/60;}
else if (diffTYPE == "second"){thisdiff = seconds;}
else {thisdiff = seconds;}
return thisdiff;
},
getDateDifferenceExcWeekends: function(){
//Make Sure that start and end are GlideDateTime Objects
var firstDT = this.getParameter('sysparm_fdt');
var gdt = new GlideDateTime(firstDT);
var days = gdt.getDayOfWeekLocalTime();
//var days = firstDT.getGlideObject().getDayOfWeek();
gs.log('Days is: '+days);
var day;
if (days == 1) {
day = 'Monday';
} else if (days == 2) {
day = 'Tuesday';
} else if (days == 3) {
day = 'Wednesday';
} else if (days == 4) {
day = 'Thursday';
} else if (days == 5) {
day = 'Friday';
} else if (days == 6) {
day = 'Saturday';
} else {
day = "Sunday";
}
return day;
}
});
Client Script:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
//Type appropriate comment here, and begin script below
var cdt = newValue; //The date field
var dttype = 'day'; //this can be day, hour, minute, second. By default it will return seconds.
g_form.hideFieldMsg('Variable Name');
var day = new GlideAjax('Script Include Name');
day.addParam('sysparm_name','getDateDifferenceExcWeekends');
day.addParam('sysparm_fdt', cdt);
day.addParam('sysparm_difftype', dttype);
day.getXML(checkDay);
function checkDay(response){
var answer = response.responseXML.documentElement.getAttribute("answer");
// alert(answer);
if(answer == 'Sunday' || answer == 'Saturday'){
g_form.showFieldMsg('Variable name', "Date should not be on Weekends " , 'error');
// alert("Date should not be in Weekend");
g_form.clearValue('Variable name'); // Pass the variable name here
g_form.setMandatory('Variable name');
}
else
{
var ajax = new GlideAjax('Script Include Name');
ajax.addParam('sysparm_name','getNowDateTimeDiff');
ajax.addParam('sysparm_fdt', cdt);
ajax.addParam('sysparm_difftype', dttype);
ajax.getXML(doSomething);
function doSomething(response){
var answer = response.responseXML.documentElement.getAttribute("answer");
//alert(answer);
if(answer<0 || answer>5){
g_form.showFieldMsg('Variable name', "Date should be Less than 5 working days from today " , 'error');
//alert('Change value');
// alert("Date should be more than 5 working days from today.");
g_form.clearValue('Variable name'); // Pass the variable name here
g_form.setMandatory('Variable name');
}
}
}
}
Let me know if you are stuck.
Hope this helps. Please mark the answer as correct/helpful based on impact.
Regards,
Shloke
Regards,
Shloke
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-07-2022 09:06 PM
Hi Shloke,
Thank you for your script.
Its working.
Regards