The CreatorCon Call for Content is officially open! Get started here.

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

Rahul Kumar17
Tera Guru
Hi Don't use GliedRecord in client script. This question solved without scripting When to run and which type of action perforn only This is helpfull so Please mark correct
If my response helped please mark it correct and close the thread.

Thanks,
Rahul Kumar

Dan Brown2
Kilo Sage

Thanks all - really helpful.