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

Sandeep Rajput
Tera Patron
Tera Patron

@Souvick6917 Since your business rule runs on Table A which has the glide list filed. The type of this business rule should be After Update and not After Delete. Delete operation is actually taking place on Table B, only update is taking place on Table A.

 

Solution: Change the business rule type on Table A to After Update and the business rule script will trigger.

 

Please don't forget to mark the response helpful and accepted solution if it addresses your question.

Hi Sandeep

I have changed the rule type to After update , the log is triggering with the sys_ids of the users, but the delete operation is not working. Only getting the list of the users in system logs.

How to delete the users using the code.

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

@Souvick6917 Your issue lies in the following line 

 

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

 

The glideListArray contain the list of user sys_ids. The get operation will only work if the Table_B is sys_user table, otherwise it won't work. To make it work, either change the table to sys_user. Or provide the correct field name inside get() method call to fetch the correct record.

 

Hope this helps.

 

 

@Sandeep Rajput That is correct, I am getting the sys_ids on the logs, but as I search with the sys_ids on Table B it is returning null value. 

As I am badly stuck with this, can you help me with the code. 

On table B, I have another user field which is a reference type. So I want to connect these 2 fields and delete, and sys_user table cannot be used, as I want to delete only from Table B.

If possible can you please give me the code ,

 

Thanks and Regards

Souvick