Business Rule Script - Find record and go to other table and delete

BuriB
Tera Expert

Hi All, 

 

we have a table where master data are available. And we have another table where Bank Details are available. Both matching with the customer number.

 

Now we need a BR as follow: If the master data has the archive flag, the customer number should be determined via a script. Than in the same script should go to the other table where bank details are available. Based on the determined customer number, the bank details should be deleted in the bank table. 

 

My script is not working: 

 

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

var gr = new GlideRecord('u_business_partner'); // Ermittelle die BP NR
 

gr.addQuery('u_archive', true); // Suche nach Datensätzen, bei denen das Archiv-Flag gesetzt ist  
gr.query();  
 
while (gr.next()) {  
    // Hole die Kundennummer aus dem gefundenen Datensatz  
    var BpNumber = gr.u_number;




var gr = new GlideRecord('x_bgbsg_mdm_bank_details');
gr.addQuery('business_partner', BP_Number);
gr.query();
if (gr.next()) {
     gr.deleteRecord();}

     }  }  
)(current, previous);  
 
 
2 REPLIES 2

Viraj Hudlikar
Giga Sage

Hello @BuriB 

This seems to be duplicate question asked, please check my response over Business Rule to find a record in one table and th... - ServiceNow Community


If my response has helped you hit helpful button and if your concern is solved do mark my response as correct.

 

Thanks & Regards
Viraj Hudlikar.

Community Alums
Not applicable

Hi @BuriB ,

Can we try following script:

(function executeRule(current, previous /*null when async*/) {
// Check if the archive flag is set
if (current.archive_flag == true) {

var customerNumber = current.customer_number;


var bankDetailsGR = new GlideRecord('x_bgbsg_mdm_bank_details'); 
bankDetailsGR.addQuery('customer_number', customerNumber); 
bankDetailsGR.query();


while (bankDetailsGR.next()) {
bankDetailsGR.deleteRecord(); // This deletes the matching bank details record
}

gs.info('Bank details deleted for customer number: ' + customerNumber);
}
})(current, previous);