After Insert Business Rule triggering at the same time and updating same record multiple times

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-17-2023 02:39 PM - edited 02-17-2023 02:42 PM
Hi,
I have a custom table (Lets call custom_a) where records get inserted through datasource. Multiple Records are expected to get inserted at the same time.
I have a After Insert BR on custom_a to Update the "alm_hardware" table with data from the "custom_a" as below,
- It creates a new GlideRecord object for the "alm_hardware" table.
- It adds three queries to the GlideRecord object:
- one for the "serial_number" field (being blank),
- one for the "u_oracle_po" field matching the "po_number" value in the current "custom_a" record, and
- one for the "u_part_number" field matching the "edc_number" value in the current "custom_a" record.
- If a matching record is found alm_hardware table, it updates the "serial_number" field with data from the current "custom_a" record.
(function executeRule(current, previous /*null when async*/ ) {
// Add your code here
var asset = new GlideRecord("alm_hardware");
var query1 = asset.addQuery('serial_number', "SET BY API");
query1.addOrCondition('serial_number','');
var query2 = asset.addQuery("u_oracle_po", current.po_number);
var query3 = asset.addQuery("u_part_number", current.edc_number);
asset.query();
if (asset.next()) {
asset.serial_number = current.serial_number;
asset.setWorkflow(false);
asset.update();
}
})(current, previous);
Note: custom_a is expected to have multiple records with same po_number & edc_number and alm_hardware also has multiple records with same u_oracle_po and u_part_number. Since Im using if(next) it is expected to update alm_hardware one record at a time
All works well if the records inserted into custom_a has time gap (atleast in seconds).
If there are multiple records with same po_number and edc_number is inserted onto the custom_a table at exact same time , and given that the alm_hardware table has multiple records with that same u_oracle_po & u_part_number ,it is updating a single alm_hardware record again and again instead of updating the serial# for each matching alm_hardware record at a time and moving on to next.
seems like the BR is getting triggered at the same time and gets the first (same) alm_hardware record and updating it .
Do I need to change the BR as ASYNC to make it work as expected? or is there anything else I can do.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-17-2023 11:40 PM
Hi @Karthick Nagara ,
Please try before insert BR on "custom_a" table with the below code:
(function executeRule(current, previous /*null when async*/) {
gs.info("@@Custom_a Serial number:"+current.serial_number);
var asset = new GlideRecord("alm_hardware");
asset.addEncodedQuery("serial_number=SET BY API^ORserial_numberISEMPTY^u_oracle_po="+current.po_number+"^u_part_number="+current.edc_number);
asset.orderBy('sys_updated_on');
asset.query();
if(asset.next()){
asset.serial_number = current.serial_number;
asset.setWorkflow(false);
asset.update();
}
})(current, previous);
Please hit like button if my suggestion has helped you in any way.
Please mark correct if my response has solved your query.
Thanks & Regards
Jyoti Jadhav
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-20-2023 04:33 AM
Hi @Karthick Nagara ,
Were you able to try above code?
Please hit like button if my suggestion has helped you in any way.
Please mark correct if my response has solved your query.
Thanks & Regards
Jyoti Jadhav

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-20-2023 06:15 AM

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-28-2023 08:38 PM
I have changed the design with a Scheduled Script execution to overcome the issue. Thank you @Jyoti Jadhav9 @Basheer thanks for your responses.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-07-2024 10:03 PM
hello @Karthick Nagara kindly share your solution.
i'm facing the same issue BR triggering at the same time.