How to set the Duration,Business duration, Closed and Closed by fields on RITM for historical data ?

raj99918
Tera Contributor

Hi 

 

How to set the Duration,Business duration, Closed and Closed by fields on RITM for historical data (Old Records)?

 

Help me with the Fix script

 

Thanks

3 REPLIES 3

SAI VENKATESH
Tera Sage
Tera Sage

Hi @raj99918 

 

You can try the below Script:

var grRitm = new GlideRecord('sc_req_item');
grRitm.addActiveQuery('sys_created_onRELATIVELT@month@ahead@1');
grRitm.addActiveQuery('stateIN1,2');
grRitm.setLimit(10); // Limit the processing to 10 records for testing
grRitm.query();

// Loop through each matching RITM record
while (grRitm.next()) {
    // Log the RITM being processed
    gs.print('Processing RITM: ' + grRitm.sys_id);
    var createdOn = new GlideDateTime(grRitm.sys_created_on);
    var updatedOn = new GlideDateTime(grRitm.sys_updated_on);
	var closedat=new GlideDateTime(grRitm.closed_at);
	var openedat=new GlideDateTime(grRitm.opened_at);
    var duration = (updatedOn.getNumericValue() - createdOn.getNumericValue()) / 86400000;
    var businessDuration = (closedat.getNumericValue() - openedat.getNumericValue()) / 86400000;

    gs.print(duration);
	gs.print('business duration'+businessDuration);
	grRitm.business_duration = businessDuration;
    grRitm.closed = closedat;
    grRitm.closed_by = gs.getUserID(); 
	grRitm.update();

}

 

Regards,
Sai Venkatesh 

Ankur Bawiskar
Tera Patron
Tera Patron

@raj99918 

for historical records you need to search in Audit table and know when the ticket got closed.

Audit table is huge and querying it for each closed INC will lead to performance issue.

I would suggest to discuss this requirement with your business team

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

@raj99918 

Hope you are doing good.

Did my reply answer your question?

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