OnChange Client script using a GlideRecord

Dan Brown2
Kilo Sage

Hi,

I may well not be doing this correctly, but I am trying to run an onChange client script to assign the ci to a field from the user record.

So I am 

var user = g_form.getValue('u_requestor'); //Get the current user

var gr = new GlideRecord('cmdb_ci_computer'); 

gr.addQuery("assigned_to",  user);

 

--- this works fine with gr.next and setting the value.

But when I just add a another addQuery it stops working.

var user = g_form.getValue('u_requestor'); //Get the current user

var gr = new GlideRecord('cmdb_ci_computer'); 

gr.addQuery("assigned_to",  user);

gr.addQuery("u_type", "!=", monitors);

Any ideas?

Dan

 

1 ACCEPTED SOLUTION

Community Alums
Not applicable

Hi Dan, 

As mentioned earlier "Glide Record" usage in client script is not a good practice.

You should easily be able to convert your code into a GlideAjax call and/or getReference(callback) function, depending on the need of the requirement. 

var ga = new GlideAjax('script_include_name');
ga.addParam('sysparm_name', 'function_name');
ga.addParam('sysparm_user', g_form.getValue('u_requestor'));
ga.getXML(mycallback);

function mycallback(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
if (answer == 'yes') { //assuming you are returning yes as answer from script include

//Do logic here for the form, based on the response

}

}

// Script Include

function_name: function() {
var answer ='';
var user = this.getParameter('sysparm_user');
var gr = new GlideRecord('cmdb_ci_computer');

gr.addQuery("assigned_to", user);

gr.addQuery("u_type", "!=", 'monitors');

gr.query();

while(gr.next()){

//do the logic here and set the answer variable to a value which you will use in the client slide. 
answer = //Do the logic here...
}

return answer;
}

 

Please mark the response to the query helpful or correct, based on the impact of the response. 

Thanks
IP

View solution in original post

6 REPLIES 6

Nitesh Balusu
Giga Guru

Please do not use Gliderecord on client scripts, for best practice purposes, use glideajax and move the code to a script include.

ChrisBurks
Mega Sage

Hi Dan,

A couple of things could be wrong with the script.

  1. I see in your example that user is defined, is that the same for monitors? Or should monitors be a string and have quotes around it?
  2. Adding the extra query in there, are you sure that there are records that match

 

Community Alums
Not applicable

Hi Dan, 

As mentioned earlier "Glide Record" usage in client script is not a good practice.

You should easily be able to convert your code into a GlideAjax call and/or getReference(callback) function, depending on the need of the requirement. 

var ga = new GlideAjax('script_include_name');
ga.addParam('sysparm_name', 'function_name');
ga.addParam('sysparm_user', g_form.getValue('u_requestor'));
ga.getXML(mycallback);

function mycallback(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
if (answer == 'yes') { //assuming you are returning yes as answer from script include

//Do logic here for the form, based on the response

}

}

// Script Include

function_name: function() {
var answer ='';
var user = this.getParameter('sysparm_user');
var gr = new GlideRecord('cmdb_ci_computer');

gr.addQuery("assigned_to", user);

gr.addQuery("u_type", "!=", 'monitors');

gr.query();

while(gr.next()){

//do the logic here and set the answer variable to a value which you will use in the client slide. 
answer = //Do the logic here...
}

return answer;
}

 

Please mark the response to the query helpful or correct, based on the impact of the response. 

Thanks
IP

reshmapatil
Tera Guru

Hi Dan,

First thing according to Best Practices never use GlideRecord() on Client Script.

Even though if you are using you have not defined monitors in this line

gr.addQuery("u_type", "!=", monitors);

and one more thing if u_type field is of type 'Choice' then monitors should be in ''(quotes) and also monitors should be same as value for label in choices table means you want to have one choice for type field which have value monitors.

And one more thing have you added gr.query(); after gr.AddQuery();

Also you can check your query is correct or not for that add filter to Computer(cmdb_ci_computer) table and copy query  by right clicking on applied filter(breadcrumb) and then put it in gr.addQuery();

your breadcrumb looks likes this: u_type!=monitors on table list layout but when you add this query in gr.addQuery() you can add like this

gr.addEncodedQuery('u_type!=monitors'); or gr.addQuery('u_type!=', 'monitors');