Deleting tags not deleting from the associated table

Community Alums
Not applicable

Hi Team,

 

There is a requirement that when we create tag in Order Task table, it should copy the same to Customer Project Task table. And when we delete the tag from Order Task, it should also delete the same same from Project task.

Copying part is working fine but deleting is not deleting the record. Below is the BR written after Insert and Delete

In Condition I am checking the table is one of 

"sn_ind_tmt_orm_order_task
customer_project_task"

 

 
    var ord_task = new GlideRecord('sn_ind_tmt_orm_order_task');
    ord_task.addQuery('sys_id', current.table_key);
    ord_task.query();
    if (ord_task.next()) {
        var c_task = new GlideRecord('label_entry');
        if (current.operation() == 'insert') {
            gs.info('Record found:' + ord_task.getRowCount());
           
            c_task.initialize();
            c_task.table_key = ord_task.u_customer_project_task.sys_id;
            c_task.table = 'customer_project_task';
            c_task.label = current.label;
            c_task.title = current.title;
            c_task.read = 'yes';
            var t = c_task.insert();
            gs.info('project record inserted:' + t);

        }

        if (current.operation == 'delete') {
            gs.info('Will perform delete');
            c_task.addQuery('table_key', ord_task.sys_id);
            c_task.addQuery('label',ord_task.label);
            c_task.query();
            gs.info('Rowcount to be deleted:'+ c_task.getRowCount());
            if(c_task.next()){
gs.info('Got the record to delete');
                c_task.deleteRecord();
            }

        }



    }
2 ACCEPTED SOLUTIONS

Anil Lande
Kilo Patron

Hi,

Create separate BR for Delete operation and use "Before" BR for delete Operation.

 

 

Please appreciate the efforts of community contributors by marking appropriate response as correct answer and helpful, this may help other community users to follow correct solution in future.
Thanks
Anil Lande

View solution in original post

If your current BR when to Run condition is "After" then your script part will not be able to read the current record values as it is already deleted and no longer available in database.

In such cases we can use "Before" BR and with highest order and perform the related cleanup operations.

 

Please appreciate the efforts of community contributors by marking appropriate response as correct answer and helpful, this may help other community users to follow correct solution in future.
Thanks
Anil Lande

View solution in original post

4 REPLIES 4

Iraj Shaikh
Mega Sage
Mega Sage

Hi Ankur,

 

It looks like there might be an issue in your delete logic. Specifically, the addQuery for deleting records might not be filtering the records correctly. Instead of using 'ord_task.sys_id' in the addQuery, you should use 'current.table_key', as it is the key you're trying to match when deleting the record. Additionally, it's good to make sure that the label condition is also included in the delete query.

 

Here's the modified delete logic:

if (current.operation() == 'delete') {
    gs.info('Will perform delete');
    c_task.addQuery('table_key', current.table_key);
    c_task.addQuery('label', current.label); // Make sure to include label in the delete query
    c_task.query();
    gs.info('Rowcount to be deleted:' + c_task.getRowCount());
    
    while (c_task.next()) {
        gs.info('Got the record to delete');
        c_task.deleteRecord();
    }
}

 

This modification ensures that the delete query is filtering records based on the current Order Task's key ('current.table_key') and label ('current.label'). Also, note the use of a while loop to delete all matching records if there are multiple records with the same key and label.

Please mark it helpful.

Community Alums
Not applicable

Hi Iraj,

 

It seems the deletion part is not working still, I am not able to see the info message which I put to test for delete.

I have written this BR is scoped application for OMT , is there any issue because of that or do we need to write this BR in Global scope? or there is some other issue,

Anil Lande
Kilo Patron

Hi,

Create separate BR for Delete operation and use "Before" BR for delete Operation.

 

 

Please appreciate the efforts of community contributors by marking appropriate response as correct answer and helpful, this may help other community users to follow correct solution in future.
Thanks
Anil Lande

If your current BR when to Run condition is "After" then your script part will not be able to read the current record values as it is already deleted and no longer available in database.

In such cases we can use "Before" BR and with highest order and perform the related cleanup operations.

 

Please appreciate the efforts of community contributors by marking appropriate response as correct answer and helpful, this may help other community users to follow correct solution in future.
Thanks
Anil Lande