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

Hi @Ankur, The given solution worked but impacting the database performance. Do we have any alternative to delete the SC Tasks in bulk and then RITM based on catalog item name??