Delete related list records

Souvick6917
Tera Contributor

Hello All,

I have 2 tables, Table A and Table B. On Table A, I have a glide list field for users and the related table is Table B. When users are added on the field , same user is added on Table B. Now I want to have delete functionality from Table A on Table B . When I remove users from the glidelist field on Table A, the same should be removed from Table B. The related records are fetched from Table B.

Below is my business rule:

The business rule running on Table A.

When to Run : After, Delete checkbox checked

(function executeRule(current, previous /*null when async*/ ) {
 
var glideListArray = current.getValue('user').split(',');
gs.info('Souvick glide 1 :' +glideListArray); // This log is not getting registered even
    for (var i = 0; i < glideListArray.length; i++) {
        var relatedRecord = new GlideRecord('Table_B');
        if (relatedRecord.get(glideListArray[i])) {
            relatedRecord.deleteRecord();
            gs.addInfoMessage("I am deleting records");
        }
    }
})(current, previous);
 
I need help on solving this issue, 
 
 
Thanks in Advance
Souvick Adhikari
1 ACCEPTED SOLUTION

@Souvick6917 This is expected as Users is a Glide list field on Table_B and multiple records may contain the same sys_jds hence the deletion is random. If you wish to delete all the records then you can try the following.

(function executeRule(current, previous /*null when async*/ ) {
 
var glideListArray = current.getValue('user').split(',');
gs.info('Souvick glide 1 :' +glideListArray); // This log is not getting registered even
    for (var i = 0; i < glideListArray.length; i++) {                     // The delete operation is doing nothing, the records are removed from Table A glidelist field, but staying on the Table B
        var relatedRecord = new GlideRecord('Table_B');
        relatedRecord.addQuery('users','CONTAINS',glideListArray[i])
        relatedRecord.query();
        while (relatedRecord.next()) {
            relatedRecord.deleteRecord();
            gs.addInfoMessage("I am deleting records");
        }
    }
})(current, previous);

Please mark my responses helpful and accepted solution as I already addressed all of your issues.

View solution in original post

16 REPLIES 16

@Souvick6917 Please share the name of your user field on Table_B and I will share the code.

Name of the field is  'users' and on checking the xml on the records of Table B, the field 'users' is showing display value along with sys_ids.

Souvick6917_0-1724332068518.png

 

@Souvick6917 Please update your code as follows.

 

(function executeRule(current, previous /*null when async*/ ) {
 
var glideListArray = current.getValue('user').split(',');
gs.info('Souvick glide 1 :' +glideListArray); // This log is not getting registered even
    for (var i = 0; i < glideListArray.length; i++) {                     // The delete operation is doing nothing, the records are removed from Table A glidelist field, but staying on the Table B
        var relatedRecord = new GlideRecord('Table_B');
        relatedRecord.addQuery('users','CONTAINS',glideListArray[i])
        relatedRecord.query();
        if (relatedRecord.next()) {
            relatedRecord.deleteRecord();
            gs.addInfoMessage("I am deleting records");
        }
    }
})(current, previous);

@Sandeep Rajput Thank you for the code, one last thing,

The script is deleting records randomly, as for example initially I had 4 records and I try to add 1 more record, since the BR is on after update instead of adding it is deleting randomly. I have even set the order to 10000. The deletion should be done only when some records are deleted from Table A, not in insert or adding records. 

Can you please help on this, as this is something I am trying for long.

 

Regards

Souvick 

@Souvick6917 This is expected as Users is a Glide list field on Table_B and multiple records may contain the same sys_jds hence the deletion is random. If you wish to delete all the records then you can try the following.

(function executeRule(current, previous /*null when async*/ ) {
 
var glideListArray = current.getValue('user').split(',');
gs.info('Souvick glide 1 :' +glideListArray); // This log is not getting registered even
    for (var i = 0; i < glideListArray.length; i++) {                     // The delete operation is doing nothing, the records are removed from Table A glidelist field, but staying on the Table B
        var relatedRecord = new GlideRecord('Table_B');
        relatedRecord.addQuery('users','CONTAINS',glideListArray[i])
        relatedRecord.query();
        while (relatedRecord.next()) {
            relatedRecord.deleteRecord();
            gs.addInfoMessage("I am deleting records");
        }
    }
})(current, previous);

Please mark my responses helpful and accepted solution as I already addressed all of your issues.