how to achive this requirement can any one help me

damodar
Tera Contributor

Desired Date Requested"

  • Mandatory
  • Date 
    • Users should not be able to add the current day or next day 2 days and should only be able to select business days only
  • Map to Due Date field
6 REPLIES 6

GlideFather
Tera Patron

Hi @damodar,

 

please provide more details, is it a variable on a form on a portal or backend form?

 

For backend you can achieve this with UI Policy and for Portals with Catalog UI Policy.

 

Example on my variable on a portal form:

GlideFather_0-1761144520841.png

 

or onSubmit Catalog Client Script.

 

Let me know how does it look and what you think about this.

_____
No AI was used in the writing of this post. Pure #GlideFather only

should only be able to select business days only

Hi @damodar,

 

no problem with that:

GlideFather_0-1761295806239.png

 

Thank you for accepting my response as solution

 

_____
No AI was used in the writing of this post. Pure #GlideFather only

damodar
Tera Contributor

This is the script include i wrote 

var ValidateCatalogDate = Class.create();
ValidateCatalogDate.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    isValidDate: function () {
        var inputDateStr = this.getParameter('sysparm_date');
        var scheduleName = this.getParameter('sysparm_schedule_name'); // remove this line
        if (!inputDateStr || !scheduleName){
        return 'false'; //remove || !scheduleName from this line
        }
        var inputDate = new GlideDate();
        inputDate.setValue(inputDateStr);

        var minDate = new GlideDate();
        minDate.addDaysUTC(3);
        if (inputDate.compareTo(minDate) < 0){
        return 'false';
        }
        var dayOfWeek = inputDate.getDayOfWeek();
        if (dayOfWeek == 1 || dayOfWeek == 7) {
        return 'false';
    }
        var scheduleId = '55ebbbf15f002000b12e3572f2b47714';

        var spanGR = new GlideRecord('cmn_schedule_span');
        spanGR.addQuery('schedule', scheduleId);
        spanGR.addQuery('start_date', '<=', inputDateStr + ' 23:59:59');
        spanGR.addQuery('end_date', '>=', inputDateStr + ' 00:00:00');
        spanGR.setLimit(1);
        spanGR.query();
        if (spanGR.next()){
        return 'false';
        }
        return 'true';
    },
    type: 'ValidateCatalogDate'
});

this is the client script  i wrote 
function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || !newValue) return;

    var dateField = 'desired_date_requested'; // This should be newValue instead, or g_form.getValue('desired_date_requested')

    var selectedDate = new Date(newValue + 'T00:00:00');
    var today = new Date();
    today.setHours(0, 0, 0, 0);

    var msPerDay = 24 * 60 * 60 * 1000;
    var diffDays = Math.floor((selectedDate - today) / msPerDay);

   
    if (diffDays <= 2) {
        g_form.showFieldMsg(dateField, 'You cannot select today or the next two days.', 'error');
        g_form.setValue(dateField, '');
       
        return;
    }

   
    var ga = new GlideAjax('ValidateCatalogDate');
    ga.addParam('sysparm_name', 'isValidDate');
    ga.addParam('sysparm_date', newValue);
    ga.addParam('sysparm_schedule_name', 'U.S. Holidays'); //remove this line
    ga.getXMLAnswer(function (response) {
        if (response === 'false') {
            g_form.showFieldMsg(dateField, 'Invalid date: Must be a business day (not a weekend or holiday).', 'error');
            g_form.setValue(dateField, '');
            g_form.setValue('due_date', '');
        } else {
            g_form.hideFieldMsg(dateField, true);
            g_form.setValue('due_date', newValue);
        }
    });

but it's not working