Set an Incident field as true or false via business rule
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-19-2023 10:14 AM - edited 12-19-2023 10:49 AM
Hello,
I have a field named 'Non Business' on the Incident table, and I am trying to set it to true when an Incident is created between 5 PM to 8 AM using a business rule. However, it is not working as expected.
(function executeRule(current, previous /*null when async*/) {
gs.info("Script is executing. Current time: " + new GlideDateTime());
// Check if incident creation time is after 5 PM and before 8 AM
var createdTime = new GlideDateTime(current.sys_created_on);
var fivePM = new GlideDateTime();
fivePM.setDisplayValue('17:00:00');
var eightAM = new GlideDateTime();
eightAM.setDisplayValue('08:00:00');
// Log the values for debugging
gs.info("Incident creation time: " + createdTime.getDisplayValueInternal());
gs.info("5 PM time: " + fivePM.getDisplayValueInternal());
gs.info("8 AM time: " + eightAM.getDisplayValueInternal());
if (createdTime.after(fivePM) || createdTime.before(eightAM)) {
// Set the Non Business field to true
current.u_non_business = true;
gs.info("Incident created during non-business hours. Setting u_non_business to true.");
} else {
// Set the Non Business field to false
current.u_non_business = false;
gs.info("Incident created during business hours. Setting u_non_business to false.");
}
gs.info("Script execution completed.");
})(current, previous);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-19-2023 11:33 AM
Implement this script and also match "when to run".
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-19-2023 01:15 PM
Try setting up the business rule as follow:
Code for "advance" tab:
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-19-2023 03:22 PM
@Umair_MalikNon-business field is set to true irrespective of the created time.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-20-2023 09:45 AM
Set "when to run" to "before" and check "Insert" field as in SS below.
Use This Code instead of the last one:
Note: Im using Instance time zone to get and compare the time of the incident created. You can test this BR by changing the time zone of the einstance from: System Admisnistration/ preferences/ display/ timezone.
Mark this as helpful if it works for you.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-19-2023 03:49 PM
@Reddy : Based on your screenshots, I see that you have configured the "After Insert" business rule, and in your script, you are just assigning the "u_non_business" value and not updating it. (Note: Using current.update() in an After business rule is not advisable as it re-triggers all the logic again.)
Make this a "Before Insert" business rule and check it once so that the flag also gets updated at the same time during insert.
Please mark this as correct answer and helpful if it resolved, or mark this helpful if this help you to reach towards solution.