Business Rule to find a record in one table and than go to other table to delet record.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-21-2025 02:05 AM
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:
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-21-2025 02:15 AM
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?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-21-2025 02:20 AM
Hello @BuriB
Below is revised version of your code.
- Ensure that the variable names are consistent.
- 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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-21-2025 02:22 AM
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.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader