GlideAjax and usage of getRowCount()

majumderarka26
Kilo Contributor


Hi,

I am having one Assignment group reference field in Incident page.

I want to display how many members are there in that particular assignment group.

To achieve this, i created a client callable Script Include

var number = Class.create();

number.prototype = Object.extendsObject(AbstractAjaxProcessor, {

      alert: function() {

  var names = this.getParameter('sysparm_user_name');

  var gr = new GlideRecord('sys_user_grmember');gs.log("I am starting"+names);

  gr.addQuery('group',names);

  gr.query();

  gs.log("I am in");

  var x = gr.getRowCount().toString();

  gs.log(x);

  return x;

 

 

                  }

                 

});

And i have used the following client script :

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue == '') {
          return;
    }

    //Type appropriate comment here, and begin script below
    var group = g_form.getReference('assignment_group');

var group_name = group.name;
//Type appropriate comment here, and begin script below
    var ga = new GlideAjax('number');
ga.addParam('sysparm_name','alert');
ga.addParam('sysparm_user_name',group_name);
ga.getXML(HelloWorldParse);

function HelloWorldParse(response) {
    var answer = response.responseXML.documentElement.getAttribute("answer");
    alert(answer);
}
}

But the problem arises when I am changing the assignment group it is giving a value as 0 in alert.

Please help

5 REPLIES 5

solutioningnow
Giga Guru

Hi,



For getting getRowCount, best practise says do not use GlideRecord instead use GlideAggregate. Please use below script for your reference:



var count = new GlideAggregate('sys_user_grmember');


count.addQuery('group',name);


count.addAggregate('COUNT');


count.query();


if (count.next())


    return count.getAggregate('COUNT');



You can refer below wiki url also for more information on GlideAggreate:


GlideAggregate - ServiceNow Wiki



Regards,


Solutioner


Bhavesh Jain1
Giga Guru

A small change is needed in your client script.



Current : var group_name = group.name;



Correct code: var group_name = group;



group_name should be sys_id and not name of the group because in your script inclide you are comparing with sys_id/reference field.




Regards,


Bhavesh


IGate-logo.png


http://www.igate.com


You can also update this line :




Current : var group = g_form.getReference('assignment_group');



New : var group = g_form.getValue('assignment_group');



In case your onChange client script in on Assignment group field, just write newValue.


majumderarka26
Kilo Contributor

Hi ,



I tried with GlideAggregate but still its not giving the correct output...