getRowCount - alm_license

Tungw
Tera Expert

I created a new field in the alm_license form called "u_usage" and executed a business rule to calculate the total count of devices and user entitlements. However, I do not see the value in my field. Could you please advise on what went wrong?

 

Table: alm_license
When to run: Insert and Update

 

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

    // Function to count usage
    function countUsage() {
        var gr = new GlideRecord('alm_entitlement');
        gr.addQuery('licensed_by', current.licensed_by);
        gr.query();
        return gr.getRowCount();  // Returns the count of matching records
    }

    // Set the value of u_usage field to the count of matching records
    current.u_usage = countUsage();

 })(current, previous);
 
**Solutions**
I have got it working in the field dictionary, the Business Rule above only appears when the form is loaded, but I can't populate it in another form or catalog item. I've included the working script below, go to configure Dictionary > Advance > Script.
 

(function calculatedFieldValue(current) {

    // Use GlideAggregate to count the number of matching records

    var ga = new GlideAggregate('alm_entitlement');

    ga.addQuery('licensed_by', current.sys_id);

    ga.addAggregate('COUNT');

    ga.query();

    if (ga.next()) {

        return ga.getAggregate('COUNT');

    }

    return 0;

})(current);

 
2 REPLIES 2

Elijah Aromola
Mega Sage

Is your business rule running before or after? 

I tried it all, but only Display works. Even though I obtained the value in the form view, it didn't set in the field. I tried manually saving it, but in the list view, it still shows zero. So I changed the approach to have it populated in the Dictionary calculated value script.