Client side code should not use g_form.getReference()

Bindhu1
Tera Contributor

Hi All,

Service now has recommended that Client side code should not use g_form.getReference().

Steps to resolve issue: Use AJAX calls to make trips to the server and only return the field values that are required.

Script:

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

if (g_form.getValue('u_forecast_owner') == '') {

var port = g_form.getReference('portfolio', callback);
function callback(port) {
var portfolioManager = port.portfolio_manager;
g_form.setValue('u_forecast_owner', portfolioManager);
}
}
//Type appropriate comment here, and begin script below

}

 

Please help me how this can be improved by using AJAX calls.

 

Thanks

brunda

2 REPLIES 2

Community Alums
Not applicable

Hi @brunda ,

g_form.getReference just ends up calling the client-side GlideRecord API. In my opinion, client-side GlideRecord should be avoided as much as possible, since it usually results in a lot of data being transferred needlessly. If you are going to use it, use a callback (g_form.getReference accepts a callback as the second parameter). Client-side GlideRecord can also take a callback (passed in to the query method).

If you are going to use client-side GlideRecord (either through getReference, or directly), do so with a callback so it's non-blocking. However, you rarely need to get the entire record back when doing lookups, so a GlideAjax script which can return only the necessary information is typically a better choice.

 

Mark my answer correct & Helpful, if Applicable.

Thanks,

Sandeep

Musab Rasheed
Tera Sage
Tera Sage

Hi Brunda,

Please go through below.

1) No code approach article

https://community.servicenow.com/community?id=community_article&sys_id=49deac8ddb92a010ab0202d5ca961967

2) Second is sample GlideAjax approach which servicenow recommends in place of GlideRecord or Glidereference, please tweak code accordingly for your learning.

Script include:

Create a new client callable script include with following function 

var UserDetails = Class.create();
UserDetails.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {

getUserInfo: function() {
var details = {};
var userId = this.getParameter('sysparm_user_id');
var userObj = new GlideRecord('sys_user');
userObj.addQuery('sys_id', userId);
userObj.query();
if (userObj.next()) {
details.email= userObj.email_id.toString();
}
return JSON.stringify(details);
},

type: 'UserDetails'

});

 

Client script :

Create a new catalog client script on the record producer/request form

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}

var ajax = new GlideAjax('UserDetails');
ajax.addParam('sysparm_name', 'getUserInfo');
ajax.addParam('sysparm_user_id', g_form.getValue('employee_name')); // change variable name here
ajax.getXML(doSomething);

function doSomething(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
var answers = JSON.parse(answer);
g_form.setValue('var_email_id', answers.email.toString()); // change variable name here
}
}
Please hit like and mark my response as correct if that helps
Regards,
Musab