Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Client-side code should not use synchronous AJAX methods (e.g., g_form.getReference, getXMLWait)

app
Mega Expert
created 2mo ago in IT Service Management
 

Hi,

Need a solution that for client side code,  synchronous AJAX methods should not be used then how we can use some other thing like script include for the requirement.

Script: function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var caller = g_form.getReference('caller_id');
if (caller.vip == 'true')

{
g_form.setValue('urgency','1');
g_form.setValue('impact','2');
}
else
return;
//Type appropriate comment here, and begin script below

}

 

Please suggest me the solution that how this can be improved by not using g_form.getReference().

I have also tried this by using UI policy but still not getting any appropriate result.

 

Thanks

1 ACCEPTED SOLUTION

Shweta KHAJAPUR
Tera Guru

Hi,

Try to add some logs,

var chkUserRole = Class.create();



chkUserRole.prototype = Object.extendsObject(AbstractAjaxProcessor, {



 chkCallerUserRole: function() {



 var caller = this.getParameter('sysparm_caller');
gs.addInfoMessage('caller_sys_id...'+caller);

var vip = gs.getUser().getUserByID(caller).getRecord().getValue('vip');

gs.addInfoMessage('vip...'+vip);

 return vip;


 }



});

 

 

Client script as below,

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


//Type appropriate comment here, and begin script below
 var ga = new GlideAjax('chkUserRole');



ga.addParam('sysparm_name','chkCallerUserRole');



ga.addParam('sysparm_caller',g_form.getValue('caller_id'));



ga.getXML(HelloWorldParse);


 


function HelloWorldParse(response) {



 var answer = response.responseXML.documentElement.getAttribute("answer");

g_form.addInfoMessage('answer...'+answer);

 if(answer=='1')

{

 g_form.setValue('impact','2');
 g_form.setValue('urgency','1');



}

}

}

 

View solution in original post

26 REPLIES 26

Hi Simon,

 

Ace Report came and servicenow say no to use g_form.getReference in client scripts.

 

Thanks

var caller = g_form.getReference('caller_id')
caller.vip;

That is synchronous and shouldnt be used but with a callback its async can can be used.
Taken from:

https://docs.servicenow.com/bundle/london-application-development/page/script/client-scripts/referen...

Do not make synchronous JavaScript calls	
The mobile platform does not allow synchronous JavaScript calls. The g_form.getReference() method must now have the callback parameter defined. For example:

g_form.getReference(fieldName, callback)
Be sure that all g_form.getReference() calls include the callback parameter. For example, the following script does not include a callback and is incompatible with the mobile platform:

 var userName = g_form.getReference('assigned_to').user_name;
 g_form.setValue('u_assigned_user_name', userName);
The following script has been updated to include the callback and is compatible with the mobile platform:

 g_form.getReference('assigned_to', function(gr) {
     g_form.setValue('u_assigned_user_name', gr.user_name);
 });

Hi,

Your script include name must be same as object. Please create script include by name chkUser
Role

chkUserRole 

Hi 

As i mentioned earlier. Create a new Script Include. You have messed it up.

Please refer below screenshot,

find_real_file.png