How to run business rules?

zsquared
Tera Contributor

Hello,

I am trying to run the following business rule to update or insert a record into my table: u_lol_databases

I placed the following script in the u_lol_databases business rules.   I also used the condition onDisplay.. assuming the table will update the correct count if I view the u_lol_databases table.

//count the number of different sys_class_names in the cmdb_ci_db_ora_instance table

var agg = new GlideAggregate('cmdb_ci_db_ora_instance');

agg.addAggregate('COUNT', 'sys_class_name');

agg.groupBy('sys_class_name');

agg.query();

while (agg.next()) {

  var databaseType = agg.sys_class_name.getDisplayValue();

  var hierarchy = new GlideRecord('u_lol_databases');

  hierarchy.addQuery('u_database_type',databaseType);

  hierarchy.query();

  var getCount = agg.getAggregate('COUNT', 'sys_class_name');

  if (hierarchy.next()) {

  hierarchy.u_count = getCount;

  hierarchy.update();

  } else {

  hierarchy.u_database_type = databaseType;

  hierarchy.u_count = getCount;

  hierarchy.insert();

  }

}

I'm not really sure where my logic is wrong.   I am fairly new to ServiceNow.   Capture.PNGAny advice?

1 ACCEPTED SOLUTION

Chuck Tomasi
Tera Patron

Hi Zhen,



Business rules are triggered from database operations (insert, update, delete, query). They can happen at various stages when a record is displayed or updated.



A Display business rule is probably not the right place to run an insert/update as it runs every time a form is loaded. When you see the record, the business rule runs. You likely want an after BR used to affect other records after you update the current record. See the video in the second line of when to run business rules and I think you'll get the idea.



Reference:


Business Rules - ServiceNow Wiki


Business Rules Best Practices - ServiceNow Wiki  


View solution in original post

6 REPLIES 6

Chuck Tomasi
Tera Patron

Hi Zhen,



Business rules are triggered from database operations (insert, update, delete, query). They can happen at various stages when a record is displayed or updated.



A Display business rule is probably not the right place to run an insert/update as it runs every time a form is loaded. When you see the record, the business rule runs. You likely want an after BR used to affect other records after you update the current record. See the video in the second line of when to run business rules and I think you'll get the idea.



Reference:


Business Rules - ServiceNow Wiki


Business Rules Best Practices - ServiceNow Wiki  


I see.   Well, I tried changing the WHEN to before and then I changed it to after.   I still don't see any inserts when I refresh my u_lol_databases table.   I'm not sure if I placed the script in the right business rules table.   Should it be in the cmdb_ci_db_ra_instance table?


The business rule Table field identifies which records you want to watch for the specified conditions. If you put it on cmdb_ci_db_ra_instance, then you need to update records and trigger the right conditions (e.g. a field change, or something else specified in the conditions) that make the business rule run. I cannot tell you if that is the right table or not based on what I know so far.


Gotcha! Now I understand! I didn't know that business rules only run a trigger based on the type of change to the database.. no wonder.. I got it to work !! Thank you Chuck!