Run a BR in list view and form

Vikram3
Giga Guru

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. 

find_real_file.png

 

(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);
9 REPLIES 9

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.

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

Hi,

Still didn't get the business requirement around this.

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

suvro
Mega Sage
Mega Sage

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();
}

Abhijit4
Mega Sage

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

 

By marking my response as correct or helpful, you contribute to helping future readers with similar issues.
Regards,
Abhijit
ServiceNow MVP