Creating multiple records Instead of creating one.

Lucky1
Tera Guru

Hello all,

 

After we do service-mapping in CMDB, We will save the view and it will be saved in the Map Views (ngbsm_view)

So, once I save the view, usually I will also save the same for the owned by of the business application.

So, now I have written a BR to avoid doing this manually. But the BR is creating multiple records, that is more than 150. I have seen 186 has come up, when I open a Saved View, changed the version and right click on the form header then Insert and Stay.

So, can some one help me?

 

My BR:

Lucky1_0-1755253667475.png

 

Script:

Lucky1_1-1755253698686.png

 

 

 

 

 

Regards,

Lucky

6 REPLIES 6

Lucky1
Tera Guru

Hello Bhuvan and Raghu,

 

It's working because I have changed the decision and now went with Fix scripts.

 

 

 

Regards,

Lucky

AJ-TechTrek
Giga Sage
Giga Sage

Hi @Lucky1 ,

 

Thanks for sharing the screenshots. The reason your Business Rule (BR) is creating multiple (150+ records) is because

 

1. You’re running the BR after Insert on the ngbsm_view table. When you do "Insert and Stay", it re-triggers the BR multiple times due to dependencies (sub-records or related inserts happening behind the scenes when a Service Map is saved).
2. The script does not check for existing records, so each execution inserts a new duplicate record without validation.

 

Solution Approach might be helpful


You need to add a duplicate check and carefully define conditions to avoid multiple inserts. Here’s how

 

1. Modify your BR Script (I am not good in Scripting, Please check carefully)

(function executeRule(current, previous /*null when async*/) {
gs.log("Inside BR for CI: " + current.configuration_item);

// Ensure CI and owner exist
if (!current.configuration_item || !current.configuration_item.owned_by) {
gs.log("Skipping BR as CI or owner is missing");
return;
}

var gr = new GlideRecord('ngbsm_view');
gr.addQuery('configuration_item', current.configuration_item);
gr.addQuery('version', current.version);
gr.addQuery('user', current.configuration_item.owned_by);
gr.query();

if (!gr.next()) {
// Only insert if a record does not exist
var newView = new GlideRecord('ngbsm_view');
newView.initialize();
newView.configuration_item = current.configuration_item;
newView.version = current.version;
newView.user = current.configuration_item.owned_by;
newView.insert();

gs.log("New Map View created for Owner: " + current.configuration_item.owned_by);
} else {
gs.log("Skipping insert - record already exists for this CI, version, and owner.");
}

})(current, previous);

 

2. Change BR Conditions
* Keep it as After Insert only.
* DO NOT run it on Update (to prevent recursion when version changes).
* Add a Filter Condition like:
* configuration_itemISNOTEMPTY
* configuration_item.owned_byISNOTEMPTY

 

3. Alternative: Use Async BR
If still multiple inserts are happening because the ngbsm_view insert triggers related actions, convert your BR to Async (delayed execution after the insert is committed).

 

Please appreciate the efforts of community contributors by marking appropriate response as Mark my Answer Helpful or Accept Solution this may help other community users to follow correct solution in future.
 

Thank You
AJ - TechTrek with AJ - ITOM Trainer
LinkedIn:- https://www.linkedin.com/in/ajay-kumar-66a91385/
YouTube:- https://www.youtube.com/@learnitomwithaj
Topmate:- https://topmate.io/aj_techtrekwithaj (Connect for 1-1 Session)
ServiceNow Community MVP 2025