Lock calendar day based on condition in record producer

Rohit_kolate
Tera Contributor

Hi Everyone,

 

We have a requirement in our current project, that for one of the record producers, we have two variables i.e. Start date and end date. 

We want those fields to be greyed out on basis of certain conditions.

 

For example: If A is selected from the dropdown (conditional third variable) in the above field then the Start date should have only Wednesday and Thursday selectable to user (lock other days). 

 

Is it possible, if yes. please suggest how?

Thank you!

8 REPLIES 8

Hi @Ankur Bawiskar,

Thank you, response!

I have requirement like in record producer 3 variable (work schedule, start date and end date). work schedule is a dropdown variable (A, B, C). 

If A is selected in this variable, then start date should be only Wednesday and Thursday selectable,

If B is selected, then start date should be only Sunday selectable,

if C is selected then start date should be only Tuesday and Wednesday selectable.

 

I really thank you in advance for your suggestions!

 

 

Regards,

Rohit

 

@Rohit_kolate 

you can use onChange on Start and end date and based on work schedule validate if the date selected is correct or not

what did you start with and where are you stuck?

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

Hello @Ankur Bawiskar 

Can you please provide examples or script if you have, that check day of selected date and work like this.

 

@Rohit_kolate 

Something like this but please enhance

Script Include: It should be client callable

var SelectableDays = Class.create();
SelectableDays.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    getSelectableDays: function() {
        var workSchedule = this.getParameter('work_schedule');
        var startDate = this.getParameter('start_date');
        var date = new GlideDateTime(startDate);
        var dayOfWeek = date.getDayOfWeekLocalTime(); // 1 = Monday, 7 = Sunday

        var validDays = [];

        switch (workSchedule) {
            case 'A':
                validDays = [4, 5]; // Wednesday (4) and Thursday (5)
                break;
            case 'B':
                validDays = [1]; // Sunday (1)
                break;
            case 'C':
                validDays = [3, 4]; // Tuesday (3) and Wednesday (4)
                break;
            default:
                break;
        }

        return validDays.includes(dayOfWeek).toString();
    }
});

AnkurBawiskar_0-1741692346908.png

 

Client Script: Have similar for End Date

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

    var workSchedule = g_form.getValue('work_schedule');
    var startDate = g_form.getValue('start_date');

    // Call GlideAjax to get the selectable days
    var ga = new GlideAjax('SelectableDays');
    ga.addParam('sysparm_name', 'getSelectableDays');
    ga.addParam('work_schedule', workSchedule);
    ga.addParam('start_date', startDate);
    ga.getXMLAnswer(function(response) {
        var isValidDay = response == 'true';
        if (!isValidDay) {
            g_form.showFieldMsg('start_date', 'Selected date is not valid for the chosen work schedule.', 'error');
            g_form.clearValue('start_date');
        }
    });
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

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