How to set Business Duration field on RITM?

damodharan1
Tera Contributor

I'm not able to set business duration field on old ritms and i have used below codes.
var grRITM = new GlideRecord('sc_req_item');
grRITM.addEncodedQuery("number=RITM18884753^business_duration=javascript:gs.getDurationDate('0 0:0:0')");
grRITM.query();

if (grRITM.next()) {
var opened = grRITM.opened_at.getValue();
var closed = grRITM.closed_at.getValue();

if (grRITM.business_duration == '') {
grRITM.business_duration = gs.calDateDiff(opened, closed, false);
grRITM.update();
}
}


Is there any alternative method for calDateDiff.

16 REPLIES 16

Try below script in Background script first.:

var grRITM = new GlideRecord('sc_req_item');
grRITM.addEncodedQuery("number=RITM18884753^business_duration=javascript:gs.getDurationDate('0 0:0:0')");
grRITM.query();

if (grRITM.next()) {
gs.info(grRITM.getValue('number'));                 //To confirm the query is valid. Remove this in actual script.
var opened = grRITM.getValue('opened_at');
var closed = grRITM.getValue('closed_at');

if (grRITM.business_duration == '') {
grRITM.business_duration = gs.dateDiff(opened, closed, false);
grRITM.update();
gs.info(grRITM.business_duration);                //Verify the duration needed. remove in actual script
}
}

If this solution helped resolve your issue, please consider marking it as helpful or correct.
This will assist others in finding the solution faster and close the thread.

Hi @Shree_G 
    dateDiff method return the difference between two dates and will not consider the business hours.

Thanks,

Ankur Bawiskar
Tera Patron
Tera Patron

@damodharan1 

you can refer the OOB business rule for incident and use the same for your case

This is the BR "mark_resolved"

check this link

How the Duration and Business Duration calculation works 

this will work if closed_at is populated

var grRITM = new GlideRecord('sc_req_item');
grRITM.addEncodedQuery("number=RITM18884753^business_durationISEMPTY");
grRITM.query();
if (grRITM.next()) {
    grRITM.business_duration = gs.calDateDiff(grRITM.opened_at.getDisplayValue(), grRITM.closed_at.getDisplayValue(), false);
    grRITM.update();
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

Hi @Ankur Bawiskar 
    We had a custom BR on RITM table,which was having same code but for some reason it's not working and bulk of RITM's,SCTasks & Incident's having wrong value as "0 seconds" . So we had raised the case the vendor but get to know that only in Yokohama  release it will be fixed and there is no workaround provided.

Hello @damodharan1 

 

Did you try the script I shared above ? 

 

Kindly mark my answer as helpful and accept solution if it helped you in anyway. This will help me be recognized for the efforts and also move this questions from unsolved to solved bucket. 

 

Regards,

 

Shivalika 

 

My LinkedIn - https://www.linkedin.com/in/shivalika-gupta-540346194

 

My youtube - https://youtube.com/playlist?list=PLsHuNzTdkE5Cn4PyS7HdV0Vg8JsfdgQlA&si=0WynLcOwNeEISQCY