BR or background script to set record duration

chrisn_
Mega Guru

I have created a custom table with task records on it that I want to create certain statistics for that we have in incident. One big one is the duration of the record from start to finish. I am currently trying to run a job to do this for existing records, but I also want to set this up going forward. I have tried emulating the mark for closure BR that runs on incident but I couldn't get it to work, can someone please help me identify where I have gone wrong with this background script to start? 

var gr = new GlideRecord("x_unoci_graduate_d_graduate_division"); <--(my custom table)
gr.addEncodedQuery('active=false^calendar_durationISEMPTY');
gr.query();
var dur = new DurationCalculator();
dur.setSchedule('090eecae0a0a0b260077e1dfa71da828');  <---(the schedule our instance uses)
gr.calendar_duration = dur.calcScheduleDuration(startdatetime, enddatetime);
while(gr.next()) {
    gr.calendar_duration = dur;
    gr.setWorkflow(false); 
    gr.update();
}
5 REPLIES 5

Jon23
Mega Sage

-> To help others, please mark this as Correct or Helpful if this response has been of any use. <-

The following should get you what you need once you update the startdatetime and enddatetime with the relevant fields from your table.

var dur = new DurationCalculator();
dur.setSchedule('090eecae0a0a0b260077e1dfa71da828');  // <---(the schedule our instance uses)

var gr = new GlideRecord("x_unoci_graduate_d_graduate_division"); // <--(my custom table)
gr.addEncodedQuery('active=false^calendar_durationISEMPTY');
gr.query();

while(gr.next()) {
    gr.setValue('calendar_duration', dur.calcScheduleDuration(gr.opened_at, gr.closed_at));  // <---(change these to the date/time fields on your table)
    gr.setWorkflow(false); 
    gr.update();
}

-> To help others, please mark this as Correct or Helpful if this response has been of any use. <-

Hello,

I tried that, but unfortunately I am getting an error regarding the definition of the duration. I didn't really need to change anything as the two fields I specified in the example are the actual fields on the record.

This is what the log is showing.

om.glide.script.RhinoEcmaError: "DurationCalculator" is not defined.
<refname> : Line(1) column(0)
==> 1: var dur = new DurationCalculator();
2: dur.setSchedule('090eecae0a0a0b260077e1dfa71da828'); // <---(the schedule our instance uses)
3:
4: var gr = new GlideRecord("x_unoci_graduate_d_graduate_division"); // <--(my custom table)

I think that error is because you're running it from a scoped app and the script include is in the Global app.

The quickest fix is to add 'global.' to:

var dur = new global.DurationCalculator();

and update the script include to be accessible from 'All application scopes':

find_real_file.png

 

If you don't want to modify an OOB script include record then there are other methods to calculate the duration in a scoped app.

Hello,

If there is a better way to do this I am all ears!