Thank you for providing the format of the warranty field. With this information, we can ensure the date parsing is handled correctly to compare with the current date. Let's revise the script considering the warranty date format is YYYY-MM-DD.
Here's an updated version of the client script:
function onLoad() {
// Get the warranty date value from the field
var warrantyDateStr = g_form.getValue('warranty'); // Assuming your field name is 'warranty'
var warrantyDate = new GlideDateTime(warrantyDateStr + ' 00:00:00'); // Parsing with time for consistency
// Get the current date
var today = new GlideDateTime();
// Comparison to determine which options to show/hide
if (warrantyDate.getNumericValue() < today.getNumericValue()) {
// Warranty is expired, show only the first 2 options
g_form.removeOption('my_choice_field', '3'); // Hides the third option
} else {
// Warranty is not expired, show only the 3rd option
g_form.removeOption('my_choice_field', '1'); // Hides the first option
g_form.removeOption('my_choice_field', '2'); // Hides the second option
}
}
// Optional: Helper function to log values for debugging
function debugLogValues(warrantyDateStr, warrantyDate, today) {
console.log("Warranty Date String:", warrantyDateStr);
console.log("Warranty Date Time:", warrantyDate);
console.log("Today's Date Time:", today);
console.log("Comparison Result:", warrantyDate.getNumericValue() < today.getNumericValue());
}
// Call debug log (optional, for diagnostic purposes)
// Uncomment the following line if you want to use the debug log function
// debugLogValues(warrantyDateStr, warrantyDate, today);
Key Steps and Explanations:
-
Get Warranty Date:
var warrantyDateStr = g_form.getValue('warranty'); gets the raw string value from the read-only warranty field.
var warrantyDate = new GlideDateTime(warrantyDateStr + ' 00:00:00'); parses the string into a GlideDateTime object for exact date comparison.
-
Get Current Date:
var today = new GlideDateTime(); captures the current date and time.
-
Compare Dates:
warrantyDate.getNumericValue() < today.getNumericValue() checks if the warranty date is in the past.
- Depending on the comparison result, the script removes the appropriate options from the choice field
my_choice_field.
-
Optional Debug Logging:
- Included a helper function,
debugLogValues, to log key values for debugging. Uncomment the call to debugLogValues if needed for diagnostic purposes.
Implementation:
- Script Type: Ensure this script is set as a Catalog Client Script.
- OnLoad Execution: Make sure the script runs on load.
- Test the Script: Test the behavior on your form to ensure the options are displayed/hidden based on the warranty date.
Debugging:
- If the script doesn't work as expected, enable the debug logs by uncommenting the
debugLogValues function call.
- Check the console for logged values to verify the correctness of date parsing and comparison.
By following these steps, your dynamic option visibility based on a read-only warranty date should work correctly within the ServiceNow catalog item form.