Delete records on a custom table, when the record is deleted on the main table

Facundo Prado
Tera Expert

Hello everyone!
I have a main table whith some records, and I have to create a new custom table to populate some records of the main.
This custom table has to be updated (When a record is inserted, updated or deleted).
I´ve just created a Business Rule with a SWITCH statement. (Is an AFTER business rule).

FacundoPrado_1-1726752226946.png


The BR is working for the Insert and for the update, but not for the DELETE.
If I delete a record on the main table, in the custom table appears like this:

FacundoPrado_0-1726752080862.png


I will share the code with you, to see if you can see what is wrong.

 

(function executeRule(current, previous /*null when async*/ ) {

        var prevAppId = previous.getValue('u_application_id');
        var prevGroup = previous.getValue('u_assigment_group');
        var appId = current.getValue('u_application_id');
        var appName = current.getUniqueValue();
        var group = current.getValue('u_assigment_group');

        var customTable = new GlideRecord('u_sn_app_air_ids');
        customTable.addQuery('u_app_name', appName);
        customTable.query();


            switch (current.operation()) {

                //--------------INSERT IS WORKING----------
                case 'insert':
                    customTable.initialize();
                    customTable.u_air_id = appId;
                    customTable.u_app_name = appName;
                    customTable.u_group = group;
                    customTable.insert();
                    break;

                    //--------------UPDATE IS WORKING----------
                case 'update':
                    //if (current.u_assignment_group.changes() || current.u_application_id.changes()) {
                    //customTable.addEncodedQuery('u_application_id=' + prevAppId + '^u_group.sys_id=' + prevGroup);
                    //customTable.query();

                    if (customTable.next()) {
                        customTable.u_air_id = appId;
                        customTable.u_app_name = appName;
                        customTable.u_group = group;
                        customTable.update();
                    }
                    // }
                    break;

                    //--------------DELETE IS NOT WORKING----------
                case 'delete':
                    //customTable.addEncodedQuery('u_application_id=' + appId + '^u_group.sys_id=' + group);
                    //customTable.query();
                    if (customTable.next()) {
                        customTable.setWorkflow(false);
                        customTable.deleteRecord();
						customTable.update();
                    }
                    break;
            }
        })(current, previous);


Thanks!!!!

5 REPLIES 5

Isaac Vicentini
Mega Sage
Mega Sage

Hi @Facundo Prado!

 

Try removing the update() function after deleting the record, it is not necessary when using the deleteRecord() function.

 

 

 case 'delete':
                    //customTable.addEncodedQuery('u_application_id=' + appId + '^u_group.sys_id=' + group);
                    //customTable.query();
                    if (customTable.next()) {
                        customTable.setWorkflow(false);
                        customTable.deleteRecord();
			// customTable.update();  ****** REMOVE THIS LINE ******
                    }
                    break;

 

 


 

If my answer helped you in any way, please mark it as correct/helpful 🙂

Regards,

Isaac Vicentini.


Best regards,

Isaac Vicentini
MVP 2025


If my answer was helpful, mark it as Helpful or Accept as Solution.

Yeap, I´ve just commented this line (I forgot to update the code before sharing).
But still have the same issue.

Is the "App Name" column of type String or Reference? If this is Reference, it may be deleting the record, but from another table.


Best regards,

Isaac Vicentini
MVP 2025


If my answer was helpful, mark it as Helpful or Accept as Solution.

ViswaSunny
Tera Contributor

Hi,

Could you pleas remove update and try it..