- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-14-2022 04:16 AM
Hi,
I'm trying to achieve below-point using a business rule, but it's not working properly. Now it's creating record only when date field getting empty. Can someone tell me what went wrong with my script or conditions?
- Create a record if the date field changes from empty to any date.
- Update existing records if the date field changes from any date to any date.
(function executeRule(current, previous /*null when async*/ ) {
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.needs_attention = true;
grOrder.u_fixture_date = current.u_fixture_date;
grOrder.work_notes = "Fixture Date Has been Changed To" + ' - ' + grOrder.u_fixture_date;
grOrder.insert();
})(current, previous);
Thanks,
JRY
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-14-2022 08:30 AM
With the 2 Filter Conditions I suggested, the BR will not run if the current date changes to empty, which is what it sounded like you wanted from your 2 original criteria. If you want to create a new record if the current date is empty or the previous date was empty (which doesn't really make sense as then you will create another new record when the date is populated) you would want only one Filter Condition when Fixture Date changes, then no condition on the Advanced tab. The Script would look more like this:
(function executeRule(current, previous /*null when async*/ ) {
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.needs_attention = true;
grOrder.u_fixture_date = current.u_fixture_date;
grOrder.work_notes = "Fixture Date Has been Changed To" + ' - ' + grOrder.u_fixture_date;
grOrder.insert();
} else {
var grOrder = new GlideRecord('u_trac_orders');
grOrder.addQuery('u_store_number', current.sys_id);
grOrder.query();
if (grOrder.next()) {
grOrder.u_fixture_date = current.u_fixture_date;
grOrder.work_notes = "Fixture Date Has been Changed To" + ' - ' + grOrder.u_fixture_date;
grOrder.update();
}
})(current, previous);
If you don't want it to run when the current date is empty, leave both Filter Conditions, still get rid of the Condition on the Advanced tab, and just take the || current... out of the first if statement.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-14-2022 04:39 AM
Hi @JRY ,
It would be great if you apply the filter only date field changes and in the advance code just get the previous object to check if previous object value is empty then create a record or if previous object have value then update the records.
I hope this helps
Please mark my answer correct if this helps you
Thanks
Mohit
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-14-2022 07:46 AM
Hi Mohit,
Thanks for the reply, but if I change the condition to "changes," then it will create a new record every time I change the date, but I wanted a new record to be created only when the date field changes from empty to any date, and if there is already a date in the date field, then if I try to change the date to any other date, it should only update the record, with no need to create a new record.
I hope you got what I needed; if not, please let me know.
Thanks,
JRY
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-14-2022 05:21 AM
Change the When field to 'before' so that you have access to the previous object, then change the first Filter Condition to 'is not empty', then on the Advanced tab populate the Condition field with:
previous.u_fixture_date == ''
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-14-2022 07:50 AM
Hi Brad,
Thanks for your reply. I have tried your suggestion, but here on conditions, I'm a bit confused. If I check the filter condition that the fixture date is not empty, then it will not run when it's empty, right? If it's empty, it should run and create a new record when I try to add a date to the field, and if there is already a date in the field when I try to change it, it should only update the date on an existing record in the Order form.
Thanks,
JRY