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

Thank you very much Sandeep for your help.  So it is known behavior for glide list field .

 

Regards

Souvick

@Souvick6917 It is not the problem of GlideList but the way your records in Table_A and Table_B are associated.

 

You have a GlideList field on Table_A which contains a comma separated list of Users. On Table_B you also have another GlideList field which is also contain the comma separated list of users. 

 

Now while removing a record from Table_A, you try to search the same record in users column of Table_B. This is classic case of many to many mapping where the user_sys_id may be part of users field on multiple records. Since you are using 

       
 if (relatedRecord.get(glideListArray[i])) {

This picks a random record and deletes. If you really wish to delete the exact record then you need to have the reference of (in form of a reference field) Table_A on the records of Table_B to delete the correct records.

 

Hope this helps.

Writing again.

The field on Table A is glide list while user field on Table B is reference field. The requirement is for each record of the glidelist field on Table A there should be separate row/records on Table B.

Table A  Field User: User field(glidelist) contains 4 values comma separated.

Table B contains 4 rows with each user and their details.

For each insert/ update of field User , there will be a separate record on Table B not for duplicates. Then if a record is removed form Table A User field, the same row/record should be deleted from Table B.

@Souvick6917 If this is the case then the following script should delete the exact records.

 

(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',glideListArray[i])
        relatedRecord.query();
        if (relatedRecord.next()) {
            relatedRecord.deleteRecord();
            gs.addInfoMessage("I am deleting records");
        }
    }
})(current, previous);

Thanks alot for your help, but this exact delete is not working. It is deleting randomly.