client script is not working on service portal while it is working on console

Prashant Kumar
Tera Contributor

Kindly help me with the below code it is not working on service portal while it is working on ITIL console page

 

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

var buss_ser=g_form.getValue('u_business_services');

var bus_app_name=g_form.getReference('u_business_services').name;
if(buss_ser!=''){
var user_bd='';

var id = g_form.getValue('cmdb_ci');//replace 'u_first_field' with the name of your reference field.
var user = new GlideRecord('cmdb_ci_appl');
user.addQuery('sys_id',id);
user.query();
if ( user.next() ) {
if(user.u_functional_support_group != '')
{
g_form.setValue('assignment_group', user.u_functional_support_group);
}
else
{
if(bus_app_name=='Not Applicable'){
//alert('Not Applicable');
return;
}
user_bd = g_form.getReference('u_requested_for').u_bd___scope;
//alert("user_bd"+user_bd);
if (user_bd == "FCE BD SOUTHWEST MED" || user_bd == "FCE BD SOUTHWEST SP")
{
g_form.setValue('assignment_group',"3c09b8230f5bb54041dd715ce1050ef5");
}
else
{
g_form.setValue('assignment_group',"f2393c230f5bb54041dd715ce1050e62");
}
}

}
}
else{

alert('Please select a Business Service.');
var app_name=g_form.getReference('cmdb_ci').name;
if(app_name!='Not Applicable'){
g_form.setValue('assignment_group',"");
//alert('Not Applicable');
return;
}

g_form.setValue('cmdb_ci',"");
}
//Type appropriate comment here, and begin script below

}

9 REPLIES 9

Rahul RJ
Giga Sage
Giga Sage

@Prashant Kumar  There are a couple of things you need to update

It's not best practice use glide record  in client script instead of it you can use glide ajax to get the data from server

side.

 

You can refer this link 

Get reference 

 

Please mark the answer correct/helpful based on Impact.

 

Regards,

RJ

 

Sandeep Rajput
Tera Patron
Tera Patron

@Prashant Kumar  Service Portal doesn't support synchronous API calls hence your script is working on the backend and not on the Service Portal.

 

First of all, you need to make call g_from.getReference() calls asynchronous by providing a call back method. e.g.

 

var caller = g_form.getReference('caller_id', popCallerInfo);

//Nested 'getReference' callback function for variable access
   function popCallerInfo(caller){
      //Location setting section
      if(loc && !isLoading && newValue != oldValue){
         g_form.setValue('location', caller.location);
      }
}

Secondly, you are making a glide record query directly inside the client script

var user = new GlideRecord('cmdb_ci_appl');
user.addQuery('sys_id',id);
user.query();

 

Such GlideRecord queries can work on the backend view but since they are synchronous calls they are not supported on Service Portal. Ideally this should have been called using a script include. 

 

Here is a good tutorial on the Script Include https://www.learnnowlab.com/Script-Include/#:~:text=Script%20include%20is%20used%20to,and%20in%20oth....

 

Make these changes and your client script will work on the Service Portal too.

can you just suggest the script 

Do check UI type of your client script is set to 'All'.

If my answer has helped with your question, please mark my answer as accepted solution and give a thumb up.
Regards,Sushant Malsure