The CreatorCon Call for Content is officially open! Get started here.

Background script to find the list of RITM for which active is true but all tasks are closed

Mansi roy
Tera Contributor

Could anyone please help me how to find the list of RITMS for which active is true but all task are closed using background script.

1 ACCEPTED SOLUTION

Gunjan Kiratkar
Kilo Patron
Kilo Patron

Hi @Mansi roy ,

If RITM have multiple task then you can use below script :-

var grRITM = new GlideRecord("sc_req_item");
grRITM.addQuery("active", "true");
grRITM.query();
while (grRITM.next()) {
    var closedTask = 0;
    var grTask = new GlideRecord("sc_task");
    grTask.addQuery("request_item", grRITM.sys_id);
    grTask.query();
    var rowCount = grTask.getRowCount();
    while (grTask.next()) {
        if (grTask.state == '3') {
            closedTask = closedTask + 1;
        }
    }
    if (rowCount == closedTask && rowCount!=0) {
        gs.info(grRITM.number);
    }

}

Please Mark My Response as Correct/Helpful based on Impact
Regards,
Gunjan Kiratkar
2X ServiceNow MVP
Community Rising Star 2022
Youtube : ServiceNow Guy

View solution in original post

2 REPLIES 2

Sagar Pagar
Tera Patron

Hi,

 

Try this sample scripts -

 

var request_item = new GlideRecord("sc_req_item");
request_item.addQuery("active=true");
request_item.query();
while (request_item.next()) {

	var sc_task_record = new GlideRecord("sc_task");
	sc_task_record.addQuery("request_item", request_item.getUniqueValue());
	sc_task_record.addQuery("state=3");
	sc_task_record.query();
	if (sc_task_record.next()) {
		gs.info(request_item.number);

	}
}

 

 

Thanks,

Sagar Pagar

The world works with ServiceNow

Gunjan Kiratkar
Kilo Patron
Kilo Patron

Hi @Mansi roy ,

If RITM have multiple task then you can use below script :-

var grRITM = new GlideRecord("sc_req_item");
grRITM.addQuery("active", "true");
grRITM.query();
while (grRITM.next()) {
    var closedTask = 0;
    var grTask = new GlideRecord("sc_task");
    grTask.addQuery("request_item", grRITM.sys_id);
    grTask.query();
    var rowCount = grTask.getRowCount();
    while (grTask.next()) {
        if (grTask.state == '3') {
            closedTask = closedTask + 1;
        }
    }
    if (rowCount == closedTask && rowCount!=0) {
        gs.info(grRITM.number);
    }

}

Please Mark My Response as Correct/Helpful based on Impact
Regards,
Gunjan Kiratkar
2X ServiceNow MVP
Community Rising Star 2022
Youtube : ServiceNow Guy