Client Script Dot walk best Practice

mkdj12
Giga Contributor

Is it best practice to dot walk within an onChange catalog client as seen below: 

var company = g_form.getValue('model_manufacturer');
var contact = g_form.getDisplayValue('company_phone');
var gr = new GlideRecord("core_company");
gr.addQuery('sys_id', company);
gr.query();
if (gr.next()) {
g_form.setValue("company_phone", gr.getDisplayValue('phone'));
}

 

 

 

 

1 ACCEPTED SOLUTION

Prateek kumar
Mega Sage

Always use a call back function in client side scripting

https://www.servicenowguru.com/scripting/client-scripts-scripting/gform-getreference-callback/

Try this:

var company = g_form.getValue('model_manufacturer');
var gr = new GlideRecord('core_company');
gr.addQuery('sys_id', company)
gr.query(myCallbackFunction); //Execute the query with callback function

//After the server returns the query recordset, continue here
function myCallbackFunction(gr){
   while (gr.next()) { //While the recordset contains records, iterate through them
      alert(gr.phone.getDisplayValue());
	   g_form.setValue("company_phone", gr.phone.getDisplayValue());
   }
}

Please mark my response as correct and helpful if it helped solved your question.
-Thanks

View solution in original post

3 REPLIES 3

Prateek kumar
Mega Sage

Always use a call back function in client side scripting

https://www.servicenowguru.com/scripting/client-scripts-scripting/gform-getreference-callback/

Try this:

var company = g_form.getValue('model_manufacturer');
var gr = new GlideRecord('core_company');
gr.addQuery('sys_id', company)
gr.query(myCallbackFunction); //Execute the query with callback function

//After the server returns the query recordset, continue here
function myCallbackFunction(gr){
   while (gr.next()) { //While the recordset contains records, iterate through them
      alert(gr.phone.getDisplayValue());
	   g_form.setValue("company_phone", gr.phone.getDisplayValue());
   }
}

Please mark my response as correct and helpful if it helped solved your question.
-Thanks

if the GlideRecord gets the data why resort to ajax?

Ajax is asynchronous and provides a better user experience since it will not bog down the browser waiting for the server to return the information.  

 

For small server queries it really doesn't matter much, but it is a good habit to use the asynchronous method.