Catalog Client Script Service Portal Callback-function

Chris P_
Tera Expert

So it seems Service Portal forces us to use callback functions for GlideRecords.

That's all fine in my book, but I'm doubting at the moment if I've used the best method to achieve this.

In my next example I've got the need to compare my inputCompany with the name of the company which is attached to the costcentre of my user.

So in dot-walk terms it would be something like this (but this doesn't work in Catalog Client scripts):

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

  if (isLoading) return;

  var inputcompany = newValue;

  var inputuser = g_form.getValue("registeredfor");

  var grus = new GlideRecord('sys_user');

  grus.get(inputuser);

  if(grus.u_costcenter.u_company.u_companyname == inputcompany) alert(true);

}

So using callback functions to comply with Portal rules, this becomes:

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

  if (isLoading) return;

  var usercomp = '';

  var usercost = '';

  var usercompname = '';

  var inputcompany = newValue;

  var grus = new GlideRecord('sys_user');

  grus.addQuery('sys_id',g_form.getValue("registeredfor"));

  grus.query(getUser);

  function getUser(grus) {

  if (grus.next()) usercost = grus.u_costcenter;

  var grco = new GlideRecord('u_cost_center');

  grco.addQuery('sys_id', usercost);

  grco.query(getCostCentre);

  function getCostCentre(grco) {

  if(grco.next()) usercomp = grco.u_company;

  var grbe = new GlideRecord('u_company');

  grbe.addQuery('sys_id',usercomp);

  grbe.query(getCompany);

  function getCompany(grbe) {

  if(grbe.next()) usercompname = grbe.u_companyname;

  if (usercompname == inputcompany) alert(true);

  }

  }

  }

}

This is quite a bit of code and everything is nested...

So the question is: Can I write this cleaner/easier?

1 ACCEPTED SOLUTION

Brad Tilton
ServiceNow Employee
ServiceNow Employee

Hi Chris,



It looks like you've got a variable that is a user reference and you're looking to find the company associated with that user's cost center and compare it to the value of another variable. I think your best bet in this case is to use GlideAjax. You would send the value of the user from the client script to script include using glideajax then do all your dotwalking on the server where it's available. The script include would then return the company name back to the client script.



GlideAjax


https://developer.servicenow.com/app.do#!/api_doc?v=helsinki&id=c_GlideAjaxAPI


View solution in original post

5 REPLIES 5

Chris P_
Tera Expert

Hi, the best thing is to use a script include and call it with GlideAjax as referenced by Brad.

You converted a server side client script to the portal, which is always client side so that doesn't work.

 

Kind Regards,

Chris