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

Karthick Nagara
Tera Expert

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,

  1. It creates a new GlideRecord object for the "alm_hardware" table.
  2. 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.
  3. 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.

9 REPLIES 9

Bert_c1
Kilo Patron

Change the "if (asset.next()) {" to "while (asset.next()) {" ? that will process all records found by the query, not just the first.

Hi ,

"if (asset.next())" statement was written on purpose to update one record at a time .

Basheer
Mega Sage

Hi @Karthick Nagara ,

The mistake which is being done here is you are not differentiating the records, due to which the alm_hardware table is finding it difficult to match the record which has multiple same values.

 

Use asset.orderBy(); before asset.query(); and try.

It will order the records and whenever there are multiple same value records the record gets changed based on the order.

 

Please hit like button if my suggestion has helped you in any way.
Please mark correct if my response has solved your query.

Cheers,
Mohammed Basheer Ahmed.

Thanks for the suggestion. I shall try and let you know.