How to delete the Requests created for the catalog item

manikanta1258
Tera Contributor

We have a clone scheduled in near future. So when we clone prod instance to QA. The requests will also be migrated. So we need to delete few requests related to particular catalog items. Please help me with the script to delete the requests after clone.

 

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@manikanta1258 

You can use Post-clone cleanup scripts and do your scripting

Cleanup scripts automatically run on the target instance after the cloning process finishes.

Post-clone cleanup scripts

Sample script below

You might have to delete sc_task records for that RITM as well

deleteRecords();

function deleteRecords(){
	try{
		var itemName = 'Apple iPhone5';
		var ritm = new GlideRecord('sc_req_item');
		ritm.addQuery('cat_item.name', itemName);
		ritm.query();
		while(ritm.next()){

			// find and delete REQ
			var req = new GlideRecord('sc_request');
			req.get(ritm.request);
			req.deleteRecord();

			// find and delete sc_task
			var scTask = new GlideRecord('sc_task');
			scTask.addQuery('request_item', ritm.sys_id);
			scTask.query();
			scTask.deleteMultiple();

			// then delete RITM
			ritm.deleteRecord();
		}
	}
	catch(ex){
		gs.info('Exception'+ex);
	}
}

find_real_file.png

Regards
Ankur

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

View solution in original post

5 REPLIES 5

Kieran Anson
Kilo Patron

You should be able to do something like the following and take advantage of deletes cascading where tables reference another table.

 

var req = [];
var item = new GlideRecord('sc_req_item');
item.addQuery('item','sys_id of item');
item.query();

while(item.next()){
	req.push(item.request.sys_id + '');
}

var reqs = new GlideRecord('sc_request');
reqs.addQuery('sys_id','IN',req.toString());
reqs.deleteMultiple();

Thank you for the quick solution. But we have multiple catalog items. How can i achieve that.

@manikanta1258 

please refer my script for Post-clone scripts shared below and for multiple catalog items do this enhancement

deleteRecords();

function deleteRecords(){
    try{
        var itemName = ['Apple iPhone5','Raise Hardware Request']; // array to hold catalog item names
        var ritm = new GlideRecord('sc_req_item');
        ritm.addQuery('cat_item.name', 'IN' , itemName);
        ritm.query();
        while(ritm.next()){

            // find and delete REQ
            var req = new GlideRecord('sc_request');
            req.get(ritm.request);
            req.deleteRecord();

            // find and delete sc_task
            var scTask = new GlideRecord('sc_task');
            scTask.addQuery('request_item', ritm.sys_id);
            scTask.query();
            scTask.deleteMultiple();

            // then delete RITM
            ritm.deleteRecord();
        }
    }
    catch(ex){
        gs.info('Exception'+ex);
    }
}

Regards
Ankur

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

Ankur Bawiskar
Tera Patron
Tera Patron

@manikanta1258 

You can use Post-clone cleanup scripts and do your scripting

Cleanup scripts automatically run on the target instance after the cloning process finishes.

Post-clone cleanup scripts

Sample script below

You might have to delete sc_task records for that RITM as well

deleteRecords();

function deleteRecords(){
	try{
		var itemName = 'Apple iPhone5';
		var ritm = new GlideRecord('sc_req_item');
		ritm.addQuery('cat_item.name', itemName);
		ritm.query();
		while(ritm.next()){

			// find and delete REQ
			var req = new GlideRecord('sc_request');
			req.get(ritm.request);
			req.deleteRecord();

			// find and delete sc_task
			var scTask = new GlideRecord('sc_task');
			scTask.addQuery('request_item', ritm.sys_id);
			scTask.query();
			scTask.deleteMultiple();

			// then delete RITM
			ritm.deleteRecord();
		}
	}
	catch(ex){
		gs.info('Exception'+ex);
	}
}

find_real_file.png

Regards
Ankur

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