Business Rule to find a record in one table and than go to other table to delet record.

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);  
 
 
3 REPLIES 3

Anurag Tripathi
Mega Patron
Mega Patron

Hi,

You have some variable names wrong,

Try this

(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', BpNumber );
gr.query();
if (gr.next()) {
     gr.deleteRecord();}
     }  }  
)(current, previous);  
 

What is the type of field business_partner on Table x_bgbsg_mdm_bank_details?

-Anurag

Viraj Hudlikar
Giga Sage

Hello @BuriB 

 

Below is revised version of your code.

  1. Ensure that the variable names are consistent.
  2. Use deleteMultiple() to delete all matching records in one go.

 

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

    var grMaster = new GlideRecord('u_business_partner'); // Retrieve the BP NR
    grMaster.addQuery('u_archive', true); // Search for records with the archive flag set
    grMaster.query();  

    while (grMaster.next()) {  
        // Get the customer number from the found record  
        var BpNumber = grMaster.u_number;

        var grBankDetails = new GlideRecord('x_bgbsg_mdm_bank_details');
        grBankDetails.addQuery('business_partner', BpNumber);
        grBankDetails.query();
        
        // Delete all matching bank details records
        while (grBankDetails.next()) {
            grBankDetails.deleteRecord();
        }
    }  
})(current, previous);

 


Add logging statements to track the progress and identify any issues during execution. If still not working do share what is not working or where are you stuck.

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.

 

Ankur Bawiskar
Tera Patron
Tera Patron

@BuriB 

are you sure your BR is triggering with correct condition

try the script shared by Anurag and let us know the updates.

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader