Getting sys_id in client script

thiraj
Tera Contributor

Hi Team,

I want to create a client script, to run a check on the task table from a form field. I have a field called 'u_requester' in a form (new call form) and it is stored in a table called 'u_new_call'. When the requester is filled in by the user, i want the client script to take the sys_id of the requester in the form (new call form) and run it through the task table ('task').

I want the script to check if there are any existing incidents or requests of the requester and the script should be able to COUNT the number of incidents or requests which are OPENED ONLY and display it as :

g_form.showFieldMsg('u_requestor','The requester has' + count + 'opened tickets','error');

All help is appreciated

Thank you Team

9 REPLIES 9

thiraj
Tera Contributor

Hi Tanumoy,



I tried your code and it works partly!! The only issue is that it does not retrieve the exact number of opened incidents/requests. Every requester gets the same number. When i tested it out, i think the code does not get the object through. I used the sys_id of different users in the ajax.getParam and it works then. So that is where the mistake is. But i tried different things to get it working but i had no luck. Do you think you can help me with that?



Thank you!


The code should be an onChange Client Script on table u_new_call which will run on change of u_requester field.


If you put the hardcoded sys_id of the user and it is working then the code should also work. Make sure the field name (not field label) is u_requester. If not, replace it with the correct field name.



thiraj
Tera Contributor

I am not getting any calculation out of this. The showfieldmessage shows ZERO for all users. Do you know what can be causing this?



Currently my code is


Client Script -



function onChange(control, oldValue, newValue, isLoading, isTemplate) {


    if (isLoading || newValue === '') {


          return;


    }



var username = g_form.getValue('u_requestor');




  var ajax = new GlideAjax('GetDetails');


  ajax.addParam('sysparm_name','getUserDetails');


  ajax.addParam('sysparm_req', 'username');


  ajax.getXML(ref);



  function ref(response){


  var answer = response.responseXML.documentElement.getAttribute("answer");


    if (answer)


  {


      g_form.showFieldMsg('u_requestor','The requester has ' + answer + ' opened tickets','error');


      //Use the number as you want here.


  }



  }


}






SERVER SIDE SCRIPT -



var GetDetails = Class.create();


GetDetails.prototype = Object.extendsObject(AbstractAjaxProcessor, {



  getUserDetails: function(){


  var name = this.getParameter('sysparm_req');


  var gr = new GlideAggregate('task');


  gr.addQuery('u_requestor', 'name');


  gr.addQuery('active', true);


  gr.addAggregate('COUNT');


  gr.query();


  var number = 0;


  if(gr.next())


  {


  number = gr.getAggregate('COUNT');


  return number;


  }


  },


  type: 'GetDetails'


});


First change this



ajax.addParam('sysparm_req', 'username');



to



ajax.addParam('sysparm_req', username);


Kalaiarasan Pus
Giga Sage

Performing such calculation on the client using glide record is not best practice. Also, for such purpose, you should use GlideAggregate instead of GlideRecord for better performance.


Also, for such purpose, you should use GlideAggregate instead of GlideRecord for better performance.



Add your logic to a script include to get the count required. Call the script using glideajax.