How to compare sys_created_on date with another field using client script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-10-2024 08:19 AM
Hi,
We have logic to implement that for the records whose created date is greater than 2 Oct,2024 , when user selects a field as Contract , another field is updated as valid.
If created date is lesser than 2 Oct,2024 and user selects as Contract, field should get updated as invalid.
Please suggest, how do I do these date comparison on an 'On Change' client script.
I have tried below code but it displays null value for created date.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-10-2024 08:36 AM
This approach should get you closer:
function onLoad() {
var created_date1 = getDateFromFormat(g_form.getValue('sys_created_on'), g_user_date_time_format);
var old_date = getDateFromFormat('2024-10-02 00:00:00', g_user_date_time_format);
if (newValue == 'Contract') {
if (created_date1 > old_date) {
alert ('valid');
} else {
alert('invalid');
}
}
}
I changed the if block to make the possibility of created_date1 = old_date 'invalid', rather than not accounted for.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-10-2024 10:22 AM
Hi,
I recommend utilizing a ServiceNow 'Database View' to filter your records instead of creating a script. This approach will facilitate the preparation of reports and dashboards.
Suresh.