How to delete Cancelled Survey Instances?

Bogdan18
Tera Guru

Hi everyone,

I have a scheduled job that looks for any survey instances older than 7 days and cancel them if they are in the status Ready to take. The Scheduled job script can be seen below.

autoCancelassessments();

function autoCancelassessments() {

  var value = gs.getProperty('glide.assessment.autoclose.time'); // create a system property in sys_properties table., Type = integer and value = 7 in                                                

  var valueinInt = parseInt(value);

  if (valueinInt > 0) {

  var gr = new GlideRecord('asmt_assessment_instance'); // query the table

 

  var other = gr.addQuery('metric_type', '277b148b4f5106007825b3318110c796');

  other.addOrCondition('metric_type', '8c3a2c44d7211100158ba6859e6103fe');

  other.addOrCondition('metric_type', '87186844d7211100158ba6859e610378');

  other.addOrCondition('metric_type', '502a2c44d7211100158ba6859e6103a3');

  other.addOrCondition('metric_type', '6826bdbf4fa54a40acbe01b28110c762');//edit your assessment names  

  gr.addQuery('state', 'ready');

  gr.addQuery('sys_created_on', '<', gs.daysAgoStart(7));

  gr.query();

  while(gr.next()) {

  gr.state = 'canceled'; //value of cancelled state.

  gr.active = false;

  gr.update();

  }

  }

}

In addition to cancel the survey instance I would also like to delete the once Cancelled Survey Instance. I found some helpful script in the OOTB scheduled job "Remove Old Assessment Data" but this is deleting all survey instances not just the cancelled ones. The Remove Old Assessment Data schedule job script can be seen below. I am now wondering how to combine there 2 scripts and have a single one which cancel the survey instances older than 7 days and then immediately deletes the survey instance as well. Any ideas?

var gdt = new GlideDateTime();

gdt.addYears(-1);

var gr = new GlideRecord("asmt_assessment_instance_question");

gr.addQuery("instance.assessment_group.sys_created_on", "<", gdt);

gr.query();

while (gr.next())

    gr.deleteRecord();

var gr = new GlideRecord("asmt_assessment_instance");

gr.addQuery("assessment_group.sys_created_on", "<", gdt);

gr.query();

while (gr.next())

    gr.deleteRecord();

var gr = new GlideRecord("asmt_metric_result");

gr.addQuery("assessment_group.sys_created_on", "<", gdt);

gr.query();

while (gr.next())

    gr.deleteRecord();

Thanks,

Bogdan

7 REPLIES 7

Bogdan,



Were you able to get this working?


Hi Tonya,



The cancellation part, yes I got it to work just fine but not the deletion one. If you need assistance with the cancellation part I would be happy to share the script i used to make it work. Just let me know if this is the case.



Thanks,



Bogdan


Hi Bodgan,

 

Can you please share the cancelled script?

 

 

thanks,

Kamal