I want To Delete the Records in Specific Table with Script

samba siva redd
Tera Contributor

I Need to Delete the duplicate records in Pot Call Survey Table. In this we have to Delete based on Epic Id and QuestionId. In Each Epicid I need to Maintain  1 pair of Questions only. The Questionids will be 1 and 2. In that table some epicids are having morethan 1 pair Questionids1 and  Questionid2. I need to Delete those records which are having more than 1pair. I need to Script For this and In Future we are creating any post call records  it should not take morethan 1pair of records in each epicid.

Below Screen Shot will understnad.

3 REPLIES 3

Manoj89
Giga Sage

Try this code, replace with the field names as you got

var count = 0,
    questionId = [];
var gr = new GlideAggregate("Post call survey response table");
gr.groupBy('epicId');
gr.addAggregate('COUNT');
gr.query();
while (gr.next()) {
    count = 0;
    questionId = [];

    if (gr.getAggregate('COUNT') > 2) {
        var dr = new GlideRecord("Post call survey response table");
        dr.addQuery('epicId', gr.getValue('epicId'));
        dr.orderBy('sys_created_on');
        dr.query();
        while (dr.next()) {
            if (questionId.indexOf(dr.questionId) == -1) {
                questionId.push(dr.questionId);
                dr.deleteRecord();
            }
        }
    }
}

javiercarpe
Giga Contributor

 

You can use a GlideRecord to find and delete duplicates. Here's a basic script example (test thoroughly in a dev environment!):

 

 

var gr = new GlideRecord('pot_call_survey');
gr.addQuery('epic_id', 'CONTAINS', yourEpicID); // Replace with your actual Epic ID
gr.query();

var count = 0;
while (gr.next()) {
  if (count > 0) {
    gr.deleteRecord();
  }
  count++;
}

 

This script loops through records with your specified Epic ID and deletes all but the first one.

We have multiple Epicids. I need only 1 pair of Questionid requeired in each epicid.