Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

getRowCount() in Business Rule Returns Incorrect Values When Querying Table With Loops

Izham
Kilo Explorer

Hi,

I created 2 custom tables (u_sint_requests & u_sint_impact_analysis). There is a list field (u_Impacted_functions) in the u_sint_requests table that references the departments table (cmn_deparment). In the u_sint_impact_analysis table, there is a reference field (u_team) that references the department table as well.

 

 

I'm utilizing an after & update business rule with certain filter conditions & writing a script  to check the u_sint_impact_analysis table for forms that have the u_team field populated with the same departments as in the list field (u_Impacted_functions) in the u_sint_requests table for a specified form in the u_sint_requests  table. When utilizing getRowCount(), it is not giving me the correct results - specifically when utilizing loops when querying the u_sint_impact_analysis table, however, when I put a specific sys_id of the department when querying the u_sint_impact_analysis table, the script returns the correct value (i.e. I cannot use  a dynamic variable, only static). The script is shown below:

 

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

    //SETTING GLOBAL VARIABLES    
    var departments = new GlideRecord ('cmn_department');
    var ia_form = new GlideRecord ('u_sint_impact_analysis');
    var req_sys_id = current.sys_id;
    
    //Saving functions in variables
    var current_functions = current.u_impacted_functions;
    
    //Splitting functions to individual values 
    var split_cur_func = current_functions.split(',');
    

//Script will run only if u_impacted_functions field changes
    if (current.u_impacted_functions.changes()) {
        
        var cur_func_array = [];
        
        for (i=0;i<split_cur_func.length;i++) {
            cur_func_array.push(split_cur_func[i]);
        }
        
        //gs.log ('This is the length: ' + cur_func_array.length + '\n This is the content: ' + cur_func_array);
        
        

        //UTILIZING THE getRowCount() TO QUERY THE u_sint_impact_analysis TABLE TO GET NUMBER OF FORMS THAT CONTAIN THE DEPARTMENTS IN THE u_team FIELD THAT ARE THE SAME AS THE DEPARTMENTS IN THE LIST FIELD (u_Impacted_functions) OF THE TABLE (u_sint_requests) FOR THE FORM THAT WAS UPDATED (I.E. FORM THAT TRIGGERED THE BUSINESS RULE WHEN UPDATED)
        for (j=0;j<cur_func_array.length;j++) {
            ia_form.addQuery('u_si_request_analyzed','=',req_sys_id);
            ia_form.addQuery('u_source_table','=','u_sint_requests');
            ia_form.addQuery('u_team','=',cur_func_array[j]);
            ia_form._query();
            var cur_func_ia_count = ia_form.getRowCount();
            gs.log ('This is the number of forms found: ' + cur_func_ia_count + '\n' + 'This is the function: ' + cur_func_array[j] + '\n This is the order: ' + j);
            
            //AS THE getRowCount() RETURNS INCORRECT VALUES, THE IF STATEMENT BELOW IS UNABLE TO FUNCTION PROPERLY, HENCE I AM UNABLE TO PROCEED WRITING MY CODE
            if (cur_func_ia_count == 0) {
                
                //CODE TO BE INSERTED FOR ANOTHER ACTION
                
                
            }
    
        
        }
        
     
        
    }
    
})(current, previous);

1 REPLY 1

Tony Chatfield1
Kilo Patron

Hi, unfortunately without details of your debugging\visibility of your data/references, identifying your issue is not possible from your code.

The use of getRowCount() is no longer ServiceNow best practice and they recommend using GlideAggregate.
GlideRecord | ServiceNow Developers
but based on the code supplied neither appears necessary and I would simple replace getRowCount() and your if (cur_func_ia_count == 0)  check with a test to see if ia_form returns a result IE

ia_form._query();

if(!ia_form.next()) {
 //CODE TO BE INSERTED FOR ANOTHER ACTION
}