The Zurich release has arrived! Interested in new features and functionalities? Click here for more

Deletion of a certain cancelled survey

Anita12
Kilo Contributor

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

1 ACCEPTED SOLUTION

Mark Stanger
Giga Sage

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();
}

View solution in original post

6 REPLIES 6

Thank You Mark, Appreciate your help to delete the assessments records

michaelwatts
Kilo Contributor

@Anita

Can you share how you were able to delete the questions manually please.

 

Thanks.