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

Omkar Mone
Mega Sage

Hi 

If you dont want to use getReference , Go with Script include and use GlideAjax to call it. 

 

Refer the below thread and follow :- 

https://community.servicenow.com/community?id=community_question&sys_id=94a60365db1cdbc01dcaf3231f96...

 

Mark Correct if it helps .

 

Regards,

Omkar Mone.

www.dxsherpa.com

Hi Omkar,

 

I have tried to write a client sript with script include but no luck.

Please take a look and help me.

 

Client script:

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");


if(answer)


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


}


}

 

script include:

var chkUserRole = Class.create();


chkUserRole.prototype = Object.extendsObject(AbstractAjaxProcessor, {


chkCallerUserRole: function() {


var caller = this.getParameter('sysparm_caller');


return gs.getUser().getUserByID(caller).hasRole('zTestVIP');


}


});

 

What should I write for the VIP to check the condition that if the caller is VIP then impact and urgency should set to 2 and 1 respectively.

 

Thanks

 

Hi 

In Script include GlideRecord to sys_user_has_role with the caller and get his role and return from script include.

Hi Omkar,

 

Could you please modify the script.

It will be very helpful.

Thanks