business duration calculation through background script

harshinielath
Tera Expert

We have old incidents with business duration values 'zero'. So I wrote a background script to update values. In our business scenario we need to calculate the business duration as time between opened_at and resolved_at instead of closed_at. The problem is it gives same zero values if calculated between opened_at and resolved_at and gives correct values with closed_at.I don't understand this issue.?Anyone has any thoughts.

This is the background script

var result = 0, count = 0, opened, resolved;

var gr = new GlideRecord( 'incident' );

gr.addQuery("business_duration" , "CONTAINS", '1970-01-01 00:00:00' );

gr.addQuery('opened_at', '>=', '2015-04-02 00:00:00');

gr.addQuery('opened_at', '<=', '2015-05-31 23:59:59');

gr.query();

while(gr.next()) {

opened = gr.opened_at;

resolved= gr.resolved_at;

count++;

result = gs.calDateDiff(opened, resolved, false);

gs.print(gr.number + " " + result);

gr.business_duration=result;

gr.setWorkflow(false);

//gr.update();

}

gs.print("Total records : " + count);

1 ACCEPTED SOLUTION

Harshini take a look at the resolved and closed dates for INC***572, Closed_at is more than 24hrs or 1day. That is why it is returning the value. Look at INC**571 it still returns 0 for both resolved and closed



Please mark my response as correct and helpful if it helped solved your question.
-Thanks

View solution in original post

10 REPLIES 10

Prateek kumar
Mega Sage

Hello harshini


Try this:


var result = 0, count = 0, opened, resolved;


var gr = new GlideRecord( 'incident' );


//gr.addQuery("business_duration" , "CONTAINS", '1970-01-01 00:00:00' );// what is this line of code for?


gr.addEncodedQuery("opened_atBETWEENjavascript:gs.dateGenerate('2015-04-02','00:00:00')@javascript:gs.dateGenerate('2015-05-31','23:59:59')");


gr.query();


while(gr.next()) {


opened = gr.opened_at;


resolved = gr.resolved_at;


count++;


//gs.print(gr.number + " " + opened+ " "+ resolved);


result = gs.calDateDiff(opened, resolved, false);


gs.print(gr.number + " " + result);


gr.business_duration=result;


gr.setWorkflow(false);


gr.update();


}


gs.print("Total records : " + count);



Please mark my response as correct and helpful if it helped solved your question.
-Thanks

That line is to filter the records for business duration values of 'zero'.


And it is not the problem in fetching records,as I see it gives the correct number of records but not updating values.


Can you try to print opened and resolved date of these incidents and gs.print(gr.number + " " + result);(what is this line of code returning??)



Please mark my response as correct and helpful if it helped solved your question.
-Thanks

Screenshot of some output


gs.print(gr.number + " " + result);


gs.print(gr.opened_at);


gs.print(gr.resolved_at);


c1.PNG