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

tanumoy
Tera Guru

onChange Client Script on table u_new_call which will run on change of u_requester field.



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


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


          return;


    }


 


  var name = g_form.getValue('u_requester');




  var ajax = new GlideAjax('GetDetails');


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


  ajax.addParam('sysparm_fdt', name);


  ajax.getXML(doSomething);



  function doSomething(response){


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


    if (answer)


  {


      alert ('Number is " +answer) //Use the number as you want here.


  }


 


  }


}



Script Include:



Name: GetDetails


Client Callable: True



var GetDetails = Class.create();


GetDetails.prototype = Object.extendsObject(AbstractAjaxProcessor, {




  getUserDetails: function(){


  var username = this.getParameter('sysparm_fdt');



var gr = new GlideAggregate('task');


gr.addQuery('opened_by', username);


gr.addAggregate('COUNT');


gr.query();


var number = 0;


if(gr.next())


{


number = gr.getAggregate('COUNT');


return number;


}


},


      type: 'GetDetails'


});



Note: This is an untested code. Please provide the correct field names. This will give you the count. Use the number as you want.


thiraj
Tera Contributor

Hi Tanumoy,



It is not clear what you are trying to do.


I got something like this, but it does not show the count.



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


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


  return;


  }



  //Type appropriate comment here, and begin script below



  var id = g_form.getUniqueValue('u_requestor');


  var ref = new GlideRecord('task');


  ref.addQuery('sys_id', id);


  ref.query();


  var count = 0;



  while(ref.next() && u_new_call.state == 'new')


  {



  if( id == task.u_requestor)


  {



  count = count + 1;



  }


  else


  {


  return;


  }


  }



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



}


It is not best practice to do server side calculatations in client scripts.



So basically what I was doing is, I was calling a script include from the client script using GlideAjax. var ajax = new GlideAjax('GetDetails');



And in the Script Include I was calculating the count using GlideAggregate. var gr = new GlideAggregate('task');



Then I am returning the count from the Script Include to Client Script and using that count to work further in client side. (Like Alert, in my script)



Please let me know if you still have a confusion. Also let me know if you make it work using my scripts.


thiraj
Tera Contributor

Hi Tanumoy,



I am new to servicenow and i am still in the learning phase. The little confusion i have is where should i write the second script?



Thank you