- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-10-2016 05:51 AM
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?
Solved! Go to Solution.
- Labels:
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-10-2016 06:13 AM
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.
https://developer.servicenow.com/app.do#!/api_doc?v=helsinki&id=c_GlideAjaxAPI

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-10-2016 06:13 AM
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.
https://developer.servicenow.com/app.do#!/api_doc?v=helsinki&id=c_GlideAjaxAPI
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-10-2016 06:53 AM
Hmmm you make a valid point, I'll test this tomorrow!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-10-2016 07:39 AM
I ended up testing it right away
Thanks a lot, works like a charm!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-22-2019 12:43 AM
Hi have this Catalog Client Script working on the technical view, but not on SP, looks like javscript error, Could be necesary a callback function for the GLideRecord To work on the SP, could anybody give me an example of a callback function.
Thanks for your time ¡
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
// Nos devuelve el id del usuario current ejm: gx5537jb
var User = g_user.userName;
var company = '';
var gr= new GlideRecord('sys_user');
// Filtramos por el usuario en la tabla sys_user
gr.addQuery('user_name', User);
alert('User returned as:'+User);
gr.query();
//sacamos de la tabala sys_user con el usuario ya filtrado la company,
//nos devuelve el sys id de la compañia que luego comparamos
while(gr.next()){
company =gr.getValue('company');
g_form.addErrorMessage(getMessage(company));
}
//id de bankinter = e30e8e4fdb4983003839f7b31d9619529
//id de Redsys = 87376c5edbd576403839f7b31d96197f
if (company == 'e30e8e4fdb4983003839f7b31d9619529'||company == '87376c5edbd576403839f7b31d96197f' ){
//si es bankiter aplicamos a la variable tarjeta un restrccion solo a 16 caracteres
g_form.addErrorMessage(getMessage('Es bankinter'));
if(newValue != ''){
if((newValue.toString().length!=16)){
g_form.addErrorMessage(getMessage("El Número de Tarjeta sólo debe contener números y tener 16 dÃgitos"));
g_form.clearValue('tarjeta_ospr');
}
}
}else{
//si no es bankiter restringimos a 16 carácteres numéricos
g_form.addErrorMessage(getMessage('No Es bankinter'));
var regexp = /^\d+$/;
if(newValue != ''){
if(!regexp.test(newValue)||(newValue.toString().length!=16)){
g_form.addErrorMessage(getMessage("El Número de Tarjeta sólo debe contener números y tener 16 dÃgitos"));
g_form.clearValue('tarjeta_ospr');
}
}
}
}