Run a BR in list view and form
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-06-2022 02:09 AM
Hello,
I wanted to execute a BR when incidents are displayed in reports and when incidents are opened. I tried all types and conditions but nothing worked.
(function executeRule(current, previous /*null when async*/) {
var openedDate = new GlideDateTime(current.opened_at);
var endDate = new GlideDateTime();
var statusDate = openedDate.getDate();
var currentDate = endDate.getDate();
var diff = GlideDateTime.subtract(openedDate, currentDate);
var finalTime = new GlideDuration(diff);
//var days = diff.getDisplayValue();
current.u_days_open = finalTime;
current.update();
})(current, previous);
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-06-2022 02:35 AM
Hi Ankur,
Thanks for the reply. I'll apply the BR filter accordingly. i.e, the BR should be invoked only when the assignment group is X.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-06-2022 03:54 AM
Best is to use scheduled job, which will run everyday and update that field.
By doing this no matter whenever someone opens the list he/she will have the correct data.
If you run this script everytime someone opens or views this the script will run that many times but it is not required.
Once a day it should be set and it can done via scheduled job
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-06-2022 04:21 AM
Hi,
Still didn't get the business requirement around this.
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-06-2022 04:00 AM
Your schedule job could be something like this
var endDate = new GlideDateTime();
var inc = new GlideRecord('incident');
inc.addEncodedQuery('active=true^stateNOT IN6,7');
inc.query();
while (inc.next()){
var openedDate = new GlideDateTime(inc.opened_at);
var statusDate = openedDate.getDate();
var currentDate = endDate.getDate();
var diff = GlideDateTime.subtract(openedDate, currentDate);
var finalTime = new GlideDuration(diff);
//var days = diff.getDisplayValue();
inc.u_days_open = finalTime;
inc.update();
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-06-2022 04:34 AM
Hi Vikram,
The requirement where record should be updated when seen in report looks illogical and it is not possible as there is no parameter which will identify whether record was seen in report or in list view or any other form.
The second part where you want to update 'Days Open' whenever record is opened looks logical and possible.
Try your script in display BR, it should work probably. You would need to reload page to see the changes.
Let me know if you have any further queries.
Please mark this as Correct or Helpful if it helps.
Thanks and Regards,
Abhijit
Community Rising Star 2022
Regards,
Abhijit
ServiceNow MVP