Using a business rule, validate that a date in a date field is not more than 6 months old.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-19-2023 10:00 AM
Hello Team,
I'm trying to validate multiple conditions before automatically creating order records when the date field changes from empty or null to any date. But I'm not sure how to make the following points a reality. I've previously written about the business rule that states that when the fixture date "changes," the script should run; it's working fine, but there are a few points where I'm unsure how to proceed. Please help with the below requirements.
- When the fixture date on the store form changes from empty or null to any date, two records should be created with different service types (VSAT and Broadband) and field values updated from store to order. This one is working.
- Before creating records, it should validate the points below.
- If there is already a record, it should check what the service type is; if any of the service types is missing, it should create one more record with the service type (i.e., if there is a record with the service type VSAT and another record is missing, it should create a new record, setting the service type as "Broadband"; if it is missing both, it should create both records).
- How to ensure that the chosen date is not more than 6 months greater. . If it's more than 6 months, then it should make the "Review needed" field true or, if not, false.
(function executeRule(current, previous /*null when async*/ ) {
var days = gs.getProperty('glide.fixture_date.days');
var grTracOrder = new GlideRecord("u_trac_orders");
grTracOrder.addQuery('u_store_number', current.sys_id);
grTracOrder.query();
if (grTracOrder.next()) {
if (grTracOrder.u_service_type == 'Broadband' && grTracOrder.u_service_type != 'VSAT') {
grTracOrder.initialize();
grTracOrder.u_store_number = current.sys_id;
grTracOrder.u_order_type = 'New Install';
grTracOrder.u_service_type = 'Broadband';
grTracOrder.u_status = 'Planning';
grTracOrder.needs_attention = true;
grTracOrder.u_fixture_date = current.u_fixture_date;
grTracOrder.work_notes = "Fixture Date Has been Changed To" + ' - ' + current.u_fixture_date;
grTracOrder.insert();
var shtDesc = grTracOrder.u_store_number.u_full_name + ' - ' + grTracOrder.u_service_type;
grTracOrder.short_description = shtDesc;
grTracOrder.update();
} else if (grTracOrder.u_service_type == 'VSAT' && grTracOrder.u_service_type != 'Broadband') {
grTracOrder.u_store_number = current.sys_id;
grTracOrder.u_order_type = 'New Install';
grTracOrder.u_service_type = 'VSAT';
grTracOrder.u_status = 'Planning';
grTracOrder.needs_attention = true;
grTracOrder.u_fixture_date = current.u_fixture_date;
grTracOrder.work_notes = "Fixture Date Has been Changed To" + ' - ' + current.u_fixture_date;
grTracOrder.insert();
var shrtDesc = grTracOrder.u_store_number.u_full_name + ' - ' + grTracOrder.u_service_type;
grTracOrder.short_description = shrtDesc;
grTracOrder.update();
}
} else {
if (previous.u_fixture_date == '' || current.u_fixture_date == '') {
var grOrder = new GlideRecord('u_trac_orders');
grOrder.initialize();
grOrder.u_store_number = current.sys_id;
grOrder.u_order_type = 'New Install';
grOrder.u_service_type = 'Broadband';
grOrder.u_status = 'Planning';
grOrder.needs_attention = true;
grOrder.u_fixture_date = current.u_fixture_date;
grOrder.work_notes = "Fixture Date Has been Changed To" + ' - ' + current.u_fixture_date;
grOrder.insert();
var shrtDescp = grOrder.u_store_number.u_full_name + ' - ' + grOrder.u_service_type;
grOrder.short_description = shrtDescp;
grOrder.update();
grOrder.u_store_number = current.sys_id;
grOrder.u_order_type = 'New Install';
grOrder.u_service_type = 'VSAT';
grOrder.u_status = 'Planning';
grOrder.needs_attention = true;
grOrder.u_fixture_date = current.u_fixture_date;
grOrder.work_notes = "Fixture Date Has been Changed To" + ' - ' + current.u_fixture_date;
grOrder.insert();
var shtDescp = grOrder.u_store_number.u_full_name + ' - ' + grOrder.u_service_type;
grOrder.short_description = shtDescp;
grOrder.update();
} else {
var grOrders = new GlideRecord('u_trac_orders');
grOrders.addQuery('u_store_number', current.sys_id);
grOrders.query();
while (grOrders.next()) {
grOrders.u_fixture_date = current.u_fixture_date;
grOrders.work_notes = "Revised Fixture Date: " + ' - ' + current.u_fixture_date;
grOrders.needs_attention = true;
grOrders.update();
}
}
}
})(current, previous);
Thanks,
JRY
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-19-2023 10:36 AM
Too much to comprehend here, not gonna lie.
How to ensure that the chosen date is not more than 6 months greater.
I'll answer this part: You will have to use GlideDateTime API to resolve this. Either the `subtract()` function or a combination of `addMonthsUTC()` and `before()` function will work. Try this out and post. Then we can help better.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-23-2023 01:18 AM
Hi Aditya,
I have tried the below code for "How to ensure that the chosen date is not more than 6 months greater." However, the code below does not work. Can you please help me?
var days = gs.getProperty('glide.fixture_date.days');
var fxt = parseInt(days);
var fxtrDt = current.u_fixture_date;
var gDate = new GlideDate();
gDate.addDaysUTC(fxt);
var grTracOrder = new GlideRecord("u_trac_orders");
grTracOrder.addQuery('u_store_number', current.sys_id);
grTracOrder.query();
if (grTracOrder.next()) {
if (fxtrDt >= gDate) {
grTracOrder.u_review_needed = false;
grTracOrder.update();
} else {
grTracOrder.u_review_needed = true;
grTracOrder.update();
}
}
Thanks,
JRY