In servicenow, i wrote a client script that checks readonly date and then hide the fields, previous date format is 2024-06-10 now date format is 10-Jun-2024, please modify my script for this format use. Else script is working only need to run for this format. function onLoad() f //Type appropriate comment here, and begin script below //var choiceField " g_form.getValue("evaluation_status'); var warrantyDate = g_form.getValue('asset.warranty_expiration'); var warrDate = warrantyDate.split("-"); var fieldDate = new Date(warrDate[0], warrDate[1] - 1, warrDate[2]); var currentDate = new Date(); OSWNFO if (fieldDate < currentDate) ( g_form.removeOption('evaluation_status', 'redeployable'); g_form.removeOption('evaluation_status', 'needs_repair'); ) else g_form.removeOption('evaluation_status', 'non_deployable');
Assistant
To modify your script to handle the new date format 10-Jun-2024, you will need to parse the date string accordingly before converting it into a JavaScript object. You can utilize the JavaScript object itself to handle parsing and comparisons. Below is the modified script:
User
Date is different whenever a new form created
Assistant
Date
Date
javascript
function onLoad() {
//Type appropriate comment here, and begin script below
var choiceField = g_form.getValue('evaluation_status');
var warrantyDate = g_form.getValue('asset.warranty_expiration');
// Split the date into day, month, and year
var dateParts = warrantyDate.split("-");
var day = parseInt(dateParts[0], 10); // Day is the first part
var month = dateParts[1]; // Month is the second part
var year = parseInt(dateParts[2], 10); // Year is the third part
// Convert month from name to number (Jan = 0, Feb = 1, ..., Dec = 11)
var monthNames = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
var monthIndex = monthNames.indexOf(month);
// Create a Date object for the warranty date and current date
var fieldDate = new Date(year, monthIndex, day);
var currentDate = new Date();
if (fieldDate < currentDate) {
g_form.removeOption('evaluation_status', 'redeployable');
g_form.removeOption('evaluation_status', 'needs_repair');
} else {
g_form.removeOption('evaluation_status', 'non_deployable');
}
}
Explanation:
Date Parsing:
The date string 10-Jun-2024 is split into three parts using split("-").
The day and year are directly parsed as integers.
The month is stored as a string and converted to an index suitable for the Date object.
Handling Month:
An array of month names (monthNames) is used to find the index of the given month string.
JavaScript Date object requires month as an index from 0 (Jan) to 11 (Dec), hence the use of monthNames.indexOf(month).
Date Comparison:
The fieldDate and currentDate objects are created, and the comparison is made using JavaScript's < operator.
Make sure that the script is attached to the correct client event and the field names used (asset.warranty_expiration and evaluation_status) match the fields in your ServiceNow instance.
If the date format is inconsistent each time a new form is created, you should ensure that the date parsing in your script is robust enough to handle different formats. For instance, you might encounter formats like 2024-06-10, 10-Jun-2024, or 10/06/2024.
Here’s an improved version of your script that includes checks for multiple date formats and processes them accordingly:
javascript
function onLoad() {
//Type appropriate comment here, and begin script below
var choiceField = g_form.getValue('evaluation_status');
var warrantyDate = g_form.getValue('asset.warranty_expiration');
var fieldDate = parseDate(warrantyDate);
var currentDate = new Date();
if (fieldDate && fieldDate < currentDate) {
g_form.removeOption('evaluation_status', 'redeployable');
g_form.removeOption('evaluation_status', 'needs_repair');
} else {
g_form.removeOption('evaluation_status', 'non_deployable');
}
}
// Function to parse date in various formats
function parseDate(dateStr) {
var monthsMap = {
"Jan": 0, "Feb": 1, "Mar": 2, "Apr": 3, "May": 4, "Jun": 5,
"Jul": 6, "Aug": 7, "Sep": 8, "Oct": 9, "Nov": 10, "Dec": 11
};
if (/^\d{4}-\d{2}-\d{2}$/.test(dateStr)) {
// Format: YYYY-MM-DD
var parts = dateStr.split("-");
return new Date(parts[0], parts[1] - 1, parts[2]);
} else if (/^\d{2}-[A-Za-z]{3}-\d{4}$/.test(dateStr)) {
// Format: DD-MMM-YYYY
var parts = dateStr.split("-");
var day = parseInt(parts[0], 10);
var month = monthsMap[parts[1]];
var year = parseInt(parts[2], 10);
return new Date(year, month, day);
} else if (/^\d{2}\/\d{2}\/\d{4}$/.test(dateStr)) {
// Format: DD/MM/YYYY
var parts = dateStr.split("/");
return new Date(parts[2], parts[1] - 1, parts[0]);
} else {
return null; // Unrecognized date format
}
}
Explanation:
Regex-based Format Detection:
The script uses regular expressions (/^\d{4}-\d{2}-\d{2}$/, /^\d{2}-[A-Za-z]{3}-\d{4}$/, and /^\d{2}\/\d{2}\/\d{4}$/) to check the format of the date.
Parsing Based on Format:
YYYY-MM-DD: This format is parsed by splitting on - and creating a Date object accordingly.
DD-MMM-YYYY: This format involves parsing day, month (by name, mapped to its index via monthsMap), and year.
DD/MM/YYYY: This format splits on / and correctly positions day, month, and year for the Date object.
Fallback:
If the date format does not match any recognized pattern, the script returns null, indicating an unrecognized date format.
This approach ensures flexibility in handling multiple date formats, reducing the risk of errors due to inconsistencies in date string formats. Attach this script to the appropriate client events in ServiceNow to ensure it runs correctly.