The CreatorCon Call for Content is officially open! Get started here.

Requirement

yaswanthkumar
Tera Contributor

Whenever we are updating the record in Incident table---

1. Appear the field called Row count on the form when it is not new record
2. Get the category, and No. of incidents with the same category print the value on the field called Row Count from incident table

 

I solved the requirement, please guide me if is there any better way to solve this requirement

 

1 ACCEPTED SOLUTION

James Schwab
Tera Guru

Hi, 

This looks like a good solution - but if you are looking for some feedback there is some improvements you can make to align your solution to ServiceNow best practices.

 

  1. When showing or hiding a field in ServiceNow, I would recommend using a UI Policy unless absolutely necessary to use a Client Script. You could create a simple UI Policy that has a condition of "Row Count is not empty". Then create a UI Policy Action for the Row Count field, where Visible is "True".

  2. When scripting in business rules, it is best practice to avoid using current.update() completely. 

In a "before" Business Rule, an update of the current record should not be necessary as the record will be saved upon completion of the highest ordered Business Rule.  Furthermore, in certain cases, the save may need to be stopped in the chain of Before business rules (with use of the setAbortAction function), and if the record has already been updated, data may have already been saved which would normally be an invalid save by the logic of the Business Rule.

 

In an "after" Business Rule current.update() should also not be used.  Any action that might be performed in an After Business Rule can usually have been added in a high ordered Before Business Rule, eliminating any need for an explicit update() function call.  In addition, updating in an After Business Rule will cause all Before Business Rules to run again, which, as mentioned previously could cause performance processing issues.

 

Instead, try turning your Business Rule into a "before" Business Rule and run the code as follows:

(function executeRule(current, previous /*null when async*/) {

    //Query Table
    var gr = new GlideRecord('incident');
    gr.addQuery('category', current.category);
    gr.query();

    //Set the row count, we don't need the gr.next if condition because if it is zero we can still set it to zero
    current.u_row_count = gr.getRowCount();

    //If you want to include the current record, increment the row count like this instead
    //current.u_row_count = gr.getRowCount() + 1;

})(current, previous);

 

The only other question is if your requirement needs this value to update if more incidents are added to this category in the future? - If that is the case you may want to consider an Update or Display Business Rule as well.

 

Good luck!

View solution in original post

1 REPLY 1

James Schwab
Tera Guru

Hi, 

This looks like a good solution - but if you are looking for some feedback there is some improvements you can make to align your solution to ServiceNow best practices.

 

  1. When showing or hiding a field in ServiceNow, I would recommend using a UI Policy unless absolutely necessary to use a Client Script. You could create a simple UI Policy that has a condition of "Row Count is not empty". Then create a UI Policy Action for the Row Count field, where Visible is "True".

  2. When scripting in business rules, it is best practice to avoid using current.update() completely. 

In a "before" Business Rule, an update of the current record should not be necessary as the record will be saved upon completion of the highest ordered Business Rule.  Furthermore, in certain cases, the save may need to be stopped in the chain of Before business rules (with use of the setAbortAction function), and if the record has already been updated, data may have already been saved which would normally be an invalid save by the logic of the Business Rule.

 

In an "after" Business Rule current.update() should also not be used.  Any action that might be performed in an After Business Rule can usually have been added in a high ordered Before Business Rule, eliminating any need for an explicit update() function call.  In addition, updating in an After Business Rule will cause all Before Business Rules to run again, which, as mentioned previously could cause performance processing issues.

 

Instead, try turning your Business Rule into a "before" Business Rule and run the code as follows:

(function executeRule(current, previous /*null when async*/) {

    //Query Table
    var gr = new GlideRecord('incident');
    gr.addQuery('category', current.category);
    gr.query();

    //Set the row count, we don't need the gr.next if condition because if it is zero we can still set it to zero
    current.u_row_count = gr.getRowCount();

    //If you want to include the current record, increment the row count like this instead
    //current.u_row_count = gr.getRowCount() + 1;

})(current, previous);

 

The only other question is if your requirement needs this value to update if more incidents are added to this category in the future? - If that is the case you may want to consider an Update or Display Business Rule as well.

 

Good luck!