how to hide/show fields based on date variable value changes in service catalog form

Naresh_5120
Tera Contributor

Hi Community, I have build the service catalog form, which as has 3 checkbox variables mentioned below

 

1. Legal Transfer

2. Location Transfer

3. Department transfer

 

Based on selection of these, it shows the questions below

 

Example if Legal transfer is selected it shows question like 

 

Q1 -Transfer effective date

Q2 - New manager

Q3 - Previous manager

 

I created ui policy if transfer date value is at today or before , it should hide the new manager field and show only preivous mananger , otherwise it should should new manager fields, 

 

However this is not working , when date value is before today, it shows previous manager field but does not hide the new manager fields, can someone help to to achieve this through catalog client script 

1 ACCEPTED SOLUTION

@Naresh_5120 

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

View solution in original post

9 REPLIES 9

@Naresh_5120 

I will suggest to use onChange client scripts rather than UI policy as it's sometimes difficult to debug and maintain when UI policy condition is configured based on multiple variables/fields

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

@Naresh_5120 

something like this if you want to show/hide the manager variable based on date

you can enhance it to check if the checbox variables are checked or not

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

    var transferDate = g_form.getValue('transfer_effective_date');
    var today = new Date();
    today.setHours(0, 0, 0, 0); // Set time to midnight for accurate comparison

    var transferDateObj = new Date(transferDate);

    if (transferDateObj <= today) {
        // Transfer date is today or before
        g_form.setDisplay('new_manager', false);
        g_form.setDisplay('previous_manager', true);
    } else {
        // Transfer date is after today
        g_form.setDisplay('new_manager', true);
        g_form.setDisplay('previous_manager', true);
    }
}

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

@Naresh_5120 

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

@Naresh_5120 

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

GopikaP
Mega Sage

Hi @Naresh_5120 , This is happening because, I assume, you have two catalog UI Policies -

  1. First one to show the following when Legal transfer is selected :   

    Q1 -Transfer effective date

    Q2 - New manager

    Q3 - Previous manager

  2. Second one the screenshot you shared, 

which will be a conflict, when the conditions are not met, the reverse is happening, thus the new manager field will be visible. 

You can keep the first UI Policy and then you can use client script to achieve the rest : 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
		g_form.setVisible('pre',false) //previous manager field
		g_form.setVisible('new',false); //new manager field
        return;
    }
    //Type appropriate comment here, and begin script below
    var selectedDateNum = getDateFromFormat(newValue, g_user_date_time_format);
    var today_date = new Date();
    var today_dateStr = formatDate(today_date, g_user_date_time_format);
    var todayDateNum = getDateFromFormat(today_dateStr, g_user_date_time_format);
    if (selectedDateNum < todayDateNum) {
		g_form.setVisible('pre',true)
		g_form.setVisible('new',false);
	}
	else{
		g_form.setVisible('pre',true)
		g_form.setVisible('new',true);
	}
}