Business Rule doesn't execute as expected when testing all parameters.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-20-2024 10:29 AM - edited 08-20-2024 11:24 AM
Hello guys!
I need help with a code review. What you see down here is a Business Rule script, which is meant to trigger an event "vgr_garantiarenden", which in turn would trigger a notification.
However, it is not working completely as expected. I've tried to log things. All the parts seem to be working except for the function with variable dueDate.
When I change "due_date" value on an "sc_task" record, the event doesn't trigger. I want it to trigger If a remove and re-apply a new date, or when I change the date.
The rest trigger the event. I've tested isStateClosed, by changing state value from anything to 3, the event gets triggered and it works as expected. But why doesn't the dueDate function work?
Do you see what I can't see?
(function executeRule(current, previous /*null when async*/) {
// Kontrollera om due_date inte är tomt
var dueDate = !current.due_date.nil() || current.due_date.changes();
// Kontrollera om short_description är 'Leveransplanering'
var isShortDescriptionMatch = current.short_description == 'Leveransplanering';
// Kontrollera om parent tabellens sc_req_item.short_description innehåller "Garanti"
var parent = new GlideRecord('sc_req_item');
var isParentShortDescMatch = false;
if (current.parent && parent.get(current.parent)) {
var parentShortDesc = parent.short_description;
isParentShortDescMatch = (parentShortDesc != null && parentShortDesc.toLowerCase().indexOf("garanti") >= 0);
}
// Kontrollera om state är 3 (Avslutad)
var isStateClosed = current.state == '3';
// Om alla villkor är uppfyllda, trigga notifikationen
if (dueDate && isShortDescriptionMatch && isParentShortDescMatch && isStateClosed) {
gs.eventQueue('vgr_garantiarenden', current, current.sys_id, current.sys_id);
}
})(current, previous);
Thanks in advance!

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-20-2024 10:42 AM
You have an if check here.
if (dueDate && isShortDescriptionMatch && isParentShortDescMatch && isStateClosed) {
gs.eventQueue('vgr_garantiarenden', current, current.sys_id, current.sys_id);
}
Unless all the bool variables are true the event will not trigger.
isClosed variable is set to true when state of the current record is 3.
var isStateClosed = current.state == '3';
the if condition will fail if the state is anything other than 3. This is the reason why your event is triggering when the state is 3 and not working with other states.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-20-2024 11:14 AM
This part is working as expected:
var isStateClosed = current.state == '3';
When I test the sc_task record and change from any other state to state 3. So this is as expected.
I'm however wondering why the dueDate variable isn't working when I remove date value from an sc_task record and reapply a date and save.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-20-2024 11:17 AM
@ronro2 use gs.addInfoMessage(dueDate); to print the value of dueDate variable and see if it is evaluating correctly when you make changes in the due date and save the changes.