- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-21-2025 12:37 PM
Hi,
I need to allow user to select date after 7 days but it should calculate only business days not weekend. In our project as per client we follow Friday, Saturday as weekend and I need to exclude Friday, Saturday. When user select date then it should calculate for Sunday, Monday, Tuesday, Wednesday, Thursday as business day.
Can you please help me with that.
Regards,
Nivedita
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-25-2025 04:57 AM
it worked see
Script Include:
var ValidationUtils = Class.create();
ValidationUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
validateDate: function() {
var selected_date = new GlideDateTime(this.getParameter('sysparm_expectedDate'));
var requiredDate = new GlideDateTime(); // Todays Date
var businessDays = 7;
var daysAdded = 0;
while (daysAdded < businessDays) {
requiredDate.addDays(1); // Move to the next day
var dayOfWeek = requiredDate.getDayOfWeek(); // 1 = Sunday, 2 = Monday, ..., 7 = Saturday
if (dayOfWeek != 6 && dayOfWeek != 7) { // Exclude Friday (6) and Saturday (7)
daysAdded++;
}
}
var output = {};
if (selected_date.getDate() > requiredDate.getDate()) {
output.correctDateSelected = 'Yes';
} else {
output.correctDateSelected = 'No';
output.requiredDate = requiredDate.getDate().toString();
}
return JSON.stringify(output);
},
type: 'ValidationUtils'
});
Client Script:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
g_form.hideFieldMsg('my_datetime');
var validateSelectedDate = new GlideAjax('global.ValidationUtils');
validateSelectedDate.addParam('sysparm_name', 'validateDate');
validateSelectedDate.addParam('sysparm_expectedDate', g_form.getValue('my_datetime')); //Replace your date time variable
validateSelectedDate.getXMLAnswer(function getExpectedDate(answer) {
var validatedOutput = JSON.parse(answer);
if (validatedOutput.correctDateSelected == 'No') {
g_form.showFieldMsg('my_datetime', 'kindly select date after ' + validatedOutput.requiredDate.toString(), 'error');
}
});
//Type appropriate comment here, and begin script below
}
Output:
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-23-2025 11:56 AM
Hello @niveditakumari
The script provided above by me is working.
Kindly mark my answer as helpful and accept solution if it helped you in anyway. This will help me be recognized for my efforts and also it can move from unsolved bucket to solved bucket
Regards,
Shivalika
My LinkedIn - https://www.linkedin.com/in/shivalika-gupta-540346194
My youtube - https://youtube.com/playlist?list=PLsHuNzTdkE5Cn4PyS7HdV0Vg8JsfdgQlA&si=0WynLcOwNeE
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-23-2025 12:00 PM
And @niveditakumari for what you are asking you can just use on submit catalog client script with ajax.
With business rule you will need to use Display business rule and scratchpad object, and then set abort section, but passing it to catalog. You will ultimately need to use something that's provided for catalog - like on submit client script, return false and it will do the job. If it was some records business rule wouldn't be an issue. That's the reason I asked requirement. Even now BR is not issue, but it's not optimal solution.
However you can go by only business rule as you prefer or as Ankur says.
Kindly mark my answer as helpful and accept solution if it helped you in anyway. This will help me be recognized for the efforts and also move this questions from unsolved to solved bucket.
Regards,
Shivalika
My LinkedIn - https://www.linkedin.com/in/shivalika-gupta-540346194
My youtube - https://youtube.com/playlist?list=PLsHuNzTdkE5Cn4PyS7HdV0Vg8JsfdgQlA&si=0WynLcOwNeEISQCY
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-23-2025 01:00 PM
Hi @Shivalika,
Thank you for your reply.
I got your solution but you hasve mentioned in your previous previous use onload or onchange client script becausde of that I got confused and in other community post I saw that to use business rule. I just need clarification which will be best approach.
Now you are saying I can use onsubmit and abort action by return false.
Can you please see attached screenshot and confirm that it is correct.
Regards,
Nivedita
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-24-2025 12:38 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-24-2025 12:43 AM
Hello @niveditakumari
Actually all the client scripts will work. And as I said business rule would be best option if it wasnt catalog. Because with catalog you only have client scripts and UI policies that will directly work on it. So to interact with server side, you need Ajax. There's no way out.
So, rhe script is already here, now to tailor it to your requirement exactly. What do you wanna do ? Do you want to restrict the form submission of user hasn't selected a date that is within the range ? What is the exact steps you want to take - error field message? Can you share your catalog form screenshot ?
Kindly mark my answer as helpful and accept solution if it helped you in anyway. This will help me be recognized for the efforts and also move this questions from unsolved to solved bucket.
Regards,
Shivalika
My LinkedIn - https://www.linkedin.com/in/shivalika-gupta-540346194
My youtube - https://youtube.com/playlist?list=PLsHuNzTdkE5Cn4PyS7HdV0Vg8JsfdgQlA&si=0WynLcOwNeEISQCY