- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-31-2018 06:03 AM
Hi
I have over 2000 cancelled surveys that I want to delete, however I only want to delete a certain survey but retain all other cancelled ones. On trying to delete I get an error saying the below:
Delete of AINST0010021 not allowed because of a reference in record Was your issue resolved satisfactorily? within the Assessment Instance Question file
I have worked out manually how to delete the questions in the assessment instance question section. However I don't want to sit and do this with over 2000 surveys manually as it will take me a long time!
The cancelled survey's I want to delete are for the Service Desk Satisfaction Survey.
I'm not new to ServiceNow however I'm quite new to scripting in ServiceNow.
Thanks in advanced.
Anita
Solved! Go to Solution.
- Labels:
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-31-2018 06:51 AM
You can run this script from the 'Scripts -> Background' module in your left nav. Note that I've commented out the 2 lines that actually do anything to your database (the ones that have 'deleteMultiple' and 'deleteRecord' at the end. As long as those lines are commented out you can safely run this script as many times as you need to test and make sure you're getting the right counts before you actually start deleting.
The script will output the number of assessment instances it will delete as well as the question responses per instance so you can validate. Once you're comfortable with those counts you can un-comment those 2 lines by removing the leading slashes and execute the script to actually delete the records.
You can change the 'assessmentName' for whatever survey you want to delete records for.
var assessmentName = 'Service Desk Satisfaction Survey';
var assessment = new GlideRecord('asmt_assessment_instance');
assessment.addQuery('metric_type.name', assessmentName);
assessment.addQuery('state', 'canceled');
assessment.query();
gs.print('Assessments to delete: ' + assessment.getRowCount());
while (assessment.next()) {
// Query for the answers and delete them first
var aiq = new GlideRecord('asmt_assessment_instance_question');
aiq.addQuery('instance', assessment.sys_id);
aiq.query();
gs.print('Deleting ' + aiq.getRowCount() + ' answers for ' + assessment.metric_type.name + ' assessment.');
//aiq.deleteMultiple();
// Delete the assessment instance
//assessment.deleteRecord();
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-31-2018 06:51 AM
You can run this script from the 'Scripts -> Background' module in your left nav. Note that I've commented out the 2 lines that actually do anything to your database (the ones that have 'deleteMultiple' and 'deleteRecord' at the end. As long as those lines are commented out you can safely run this script as many times as you need to test and make sure you're getting the right counts before you actually start deleting.
The script will output the number of assessment instances it will delete as well as the question responses per instance so you can validate. Once you're comfortable with those counts you can un-comment those 2 lines by removing the leading slashes and execute the script to actually delete the records.
You can change the 'assessmentName' for whatever survey you want to delete records for.
var assessmentName = 'Service Desk Satisfaction Survey';
var assessment = new GlideRecord('asmt_assessment_instance');
assessment.addQuery('metric_type.name', assessmentName);
assessment.addQuery('state', 'canceled');
assessment.query();
gs.print('Assessments to delete: ' + assessment.getRowCount());
while (assessment.next()) {
// Query for the answers and delete them first
var aiq = new GlideRecord('asmt_assessment_instance_question');
aiq.addQuery('instance', assessment.sys_id);
aiq.query();
gs.print('Deleting ' + aiq.getRowCount() + ' answers for ' + assessment.metric_type.name + ' assessment.');
//aiq.deleteMultiple();
// Delete the assessment instance
//assessment.deleteRecord();
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-31-2018 07:45 AM
Thank you so much Mark, it worked. You are a star!!

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-31-2018 08:02 AM
You're welcome! I'm glad it helped!

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-10-2021 02:42 PM
Hi Mark
I have a similar problem and like Anita, I'm new to scripting. I have a few thousand assessment instances in the "Ready to Take" that were created in a two hour period that I can isolate that I would like to delete. I guess that the script above needs some changes to cater for different criteria.
Are you able to assist?
Regards
Kevin