Rich Text Label not hidden using catalogue script

JayAdmin_16
Mega Sage

Hello! I'm still quite new to ServiceNow and hoping someone a bit more seasoned in this space can help me. 

I have 3 fields on my selected form, with the variables Start_Date_form, End_Date_form & sixty_days.

In this form, the user has requested if the days between start and end date > 60 days, for the sixty_days rich text label variable to appear. If the days between start and end date < 60 days, for the sixy_days text to not appear.

I’ve gotten this to work by creating two onChange Catalog Client Scripts. One for start_date and one for end_date, using the following script below:

function onChange(control, oldValue, newValue, isLoading) { 
    g_form.setDisplay('sixty_days', false); 

    if (isLoading || newValue === '') { 
        return; // Do nothing if the form is loading or the new value is empty 
    } 

    // Get the values of the start and end dates 
    var startDate = g_form.getValue('Start_Date_form'); 
    var endDate = g_form.getValue('End_Date_form'); 

    // Proceed only if both dates are available 
    if (startDate && endDate) { 
        // Parse the dates from dd-mm-yyyy format 
        var startParts = startDate.split('-'); 
        var endParts = endDate.split('-'); 

        // Create JavaScript Date objects 
        var start = new Date(startParts[2], startParts[1] - 1, startParts[0]); // year, month (0-indexed), day 
        var end = new Date(endParts[2], endParts[1] - 1, endParts[0]); 

        // Check if both dates are valid 
        if (!isNaN(start.getTime()) && !isNaN(end.getTime())) { 
            // Calculate the difference in milliseconds 
            var diffMilliseconds = end.getTime() - start.getTime(); 
            // Convert milliseconds to days 
            var diffInDays = diffMilliseconds / (1000 * 60 * 60 * 24); // Convert to days 

            // Show or hide the sixty_days field based on the difference 
            if (diffInDays > 60) { 
                g_form.setDisplay('sixty_days', true); // Show the sixty_days field 
            } else { 
                g_form.setDisplay('sixty_days', false); // Hide the sixty_days field 
            } 
        } else { 
            // Optionally hide the sixty_days field if dates are invalid 
            g_form.setDisplay('sixty_days', false); 
        } 
    } else { 
        // Optionally hide the sixty_days field if dates are not provided 
        g_form.setDisplay('sixty_days', false); 
    } 
}

 

I also have implemented two Catalog UI Policies:
The first one is: "Hide Label Field Based on Date Difference" which has a Catalog UI Policy Action that sets the sixty_days variable to Visble is false. 

The second one is: "Show Label Field Based on Date Difference" which has a Catalog UI Policy Action that sets the sixty_days variable to Visble is true. 

The issue I'm having is hiding the sixty_days variable when the form loads. If I start to modify the start/end date values on the form, the sixty_days variable will be hidden given the correct input is < than 60 days, but I'd like it to stay hidden until the user's start/end date values > 60 days. 

Let me know if further explanation is required. Any advice and gentle guidance would be greatly appreciated,  

0 REPLIES 0