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

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

@Abhilasha G T 

Hope you are doing good.

Did my reply answer your question?

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

@Abhilasha G T 

Hope you are doing good.

Did my reply answer your question?

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

Najmuddin Mohd
Mega Sage

Hi @Abhilasha G T ,
You can use GlideDate() functionality using an On change client script and Script include.

If the above information helps you, Kindly mark it as Helpful and Accept the solution.
Regards,
Najmuddin.