Date and time configuration in change form

Abhilasha G T
Tera Contributor

Hi Team,

 

As per my requirement,

when the priority is changing on the change form,

 

1. condition = when priority is 1

planned start date and planned end date  only allows past date 

 

2. condition= when priority is 2, 3 or 4

planned start date and planned end date  only allows future date 

 

how to achieve this?

 

Regards,

Abhilasha G T

 

 

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@Abhilasha G T 

you will require onChange client script on Priority

Something like this, please enhance it further as per your requirement

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

    g_form.hideFieldMsg('planned_start_date');
    g_form.hideFieldMsg('planned_end_date');

    // Get the planned start and end date fields
    var plannedStartDate = g_form.getValue('planned_start_date');
    var plannedEndDate = g_form.getValue('planned_end_date');

    // Convert dates to JavaScript Date objects
    var startDate = new Date(plannedStartDate);
    var endDate = new Date(plannedEndDate);
    var currentDate = new Date();

    // Condition for Priority 1
    if (newValue == '1') {
        if (startDate > currentDate || endDate > currentDate) {
            g_form.showFieldMsg('planned_start_date', 'Planned start date must be in the past for Priority 1', 'error');
            g_form.showFieldMsg('planned_end_date', 'Planned end date must be in the past for Priority 1', 'error');
            g_form.clearValue('planned_start_date', '');
            g_form.clearValue('planned_end_date', '');
        }
    }

    // Condition for Priority 2, 3, or 4
    if (newValue == '2' || newValue == '3' || newValue == '4') {
        if (startDate < currentDate || endDate < currentDate) {
            g_form.showFieldMsg('planned_start_date', 'Planned start date must be in the future for Priority 2, 3, or 4', 'error');
            g_form.showFieldMsg('planned_end_date', 'Planned end date must be in the future for Priority 2, 3, or 4', 'error');
            g_form.clearValue('planned_start_date', '');
            g_form.clearValue('planned_end_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

View solution in original post

5 REPLIES 5

Sarthak Kashyap
Tera Expert

Hi @Abhilasha G T ,

I tried your problem in my PDI please check below code 

Create onChange Client script in Priority field and add below code 

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


    // Get the planned start and end date fields
    var plannedStartDate = g_form.getValue('planned_start_date');
    var plannedEndDate = g_form.getValue('planned_end_date');

    var startDate = new Date(plannedStartDate);
    var endDate = new Date(plannedEndDate);
    var currentDate = new Date();

    if (newValue == '1') {
        if (startDate > currentDate || endDate > currentDate) {
            alert("Please select any date in past");
            g_form.clearValue('planned_start_date', '');
            g_form.clearValue('planned_end_date', '');
        }
    }

    // Condition for Priority 2, 3, or 4
    if (newValue == '2' || newValue == '3' || newValue == '4') {
        if (startDate < currentDate || endDate < currentDate) {
            alert("Please select any date in future");
            g_form.clearValue('planned_start_date', '');
            g_form.clearValue('planned_end_date', '');
        }
    }
}

 

Please mark my answer correct and helpful if this works for you

Thanks and Regards 

Sarthak