- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-13-2020 04:53 AM
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.
Solved! Go to Solution.
- Labels:
-
Finance Service Management
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-13-2020 05:10 AM
You can use Post-clone cleanup scripts and do your scripting
Cleanup scripts automatically run on the target instance after the cloning process finishes.
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);
}
}
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-13-2020 04:59 AM
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();
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-13-2020 05:44 AM
Thank you for the quick solution. But we have multiple catalog items. How can i achieve that.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-13-2020 05:48 AM
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
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-13-2020 05:10 AM
You can use Post-clone cleanup scripts and do your scripting
Cleanup scripts automatically run on the target instance after the cloning process finishes.
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);
}
}
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader