The client script is not working in service portal but working in try it option in catalog form

Ponnada
Tera Contributor

I created a on change catalog client script such that based on requested for's country and the options he chose in domain ,category and service fields(reference) will make a particular value be set and read only in service desk filed. Please take a look at the below script and help me out. I did select UI type as ALL and cannot change the whole script but can add to the existing one I added my script after the MX country. I would like the answer ASAP ,as I am running out of time:

 

function onChange(control, oldValue, newValue, isLoading) {
 
// Not setting field when form loads with the Requested For set as the logged in user - removing this
//if (isLoading) {
// return;
//}
 
if (newValue == '') {
g_form.clearValue('service_desk');
g_form.setReadOnly('service_desk',  false);
return;
}
 
// Type appropriate comment here, and begin script below
 
// When the Requested For changes, set the Service Desk based on location
// OU Code = OU20018698 --> URUGUAY - TI - PRONTO MESA DE SERVICIO (c957007a479fdd10ea4d0bde536d437b)
// CR --> Costa Rica - ITBS - Centro de Servicio Costa Rica (8c4e1131db693308a0c054195e961916)
// PA --> Panama - ITBS - Centro de Servicio Panama (15a41b77db65bf0095c83e4f9d9619f3)
// MX --> GTS - EUS - End User Support MX (34723e041b0fcd10ca0b32a61a4bcb95)
// UY --> URUGUAY - TI - PRONTO MESA DE SERVICIO (c957007a479fdd10ea4d0bde536d437b)
//  UY --> URUGUAY,DOMAIN -->PRONTO,CATEGORY-->"Backoffice IT - Data Operations"/"Backoffice IT - Production" -URUGUAY - PRONTO - IT - PRODUCCION
//  UY --> URUGUAY,DOMAIN -->PRONTO,CATEGORY-->"Backoffice IT - Technology" -URUGUAY - PRONTO - IT - TECHNOLOGIA
//  UY --> URUGUAY,DOMAIN -->PRONTO,CATEGORY-->"Technology Support" - URUGUAY - PRONTO - IT - SERVICIO TECNICO
 
 
 
var requested_for = g_form.getReference('requested_for', setFields);
 
 function setFields(requested_for) {
var ou_code = requested_for.u_org_unit.toString();
var country = requested_for.country.toString();
g_form.setDisabled('service_desk',false);
g_form.setReadOnly('service_desk',false);
if (ou_code == 'OU20018698') {
 
g_form.setValue('service_desk', 'c957007a479fdd10ea4d0bde536d437b');
//g_form.setReadOnly('service_desk', true);
}
 
else if (country == 'CR' || country == 'PA' || country == 'MX' || country == 'UY') {
 
if (country == 'CR') {
g_form.setValue('service_desk', '8c4e1131db693308a0c054195e961916');
}
 
else if (country == 'PA') {
g_form.setValue('service_desk', '15a41b77db65bf0095c83e4f9d9619f3');
}
 
else if (country == "MX") {
g_form.setValue('service_desk', '34723e041b0fcd10ca0b32a61a4bcb95');
}
 
else if (country == "UY") {
var domain = g_form.getValue('domain').toString();
var domainLabel=getDomain(domain);
var category = g_form.getValue('category').toString();
var categoryLabel=getCategory(category);
var service = g_form.getValue('service').toString();
var serviceLabel=getService(service);
 
 
if (country == "UY" && domainLabel == "PRONTO" && categoryLabel =="Backoffice IT" && (serviceLabel =="Data Operations"||serviceLabel =="Data Operations"|| serviceLabel =="Production")) {
g_form.setValue('service_desk', '75c0151d97279990840c77100153af4d');
}
else if (country == "UY" && domainLabel == "PRONTO" && categoryLabel =="Backoffice IT" && serviceLabel =="Technology") {
g_form.setValue('service_desk', '9618e012476f11d0ea4d0bde536d4316');
//9618e012476f11d0ea4d0bde536d4316
else if(country == "UY" && domainLabel == "PRONTO" && categoryLabel =="Technical Support"){
g_form.setValue('service_desk', '36686492476f11d0ea4d0bde536d4374');
}
else { 
g_form.setValue('service_desk', 'c957007a479fdd10ea4d0bde536d437b');
}
 
}
 
}
          else {
          
g_form.clearValue('service_desk');
g_form.setReadOnly('service_desk',  false);
 
 
// g_form.setValue('service_desk', 'c957007a479fdd10ea4d0bde536d437b');
}
 
function getDomain(domain)
{
var gr = new GlideRecord('sys_choice');
gr.addQuery('name', 'question_choice');
gr.addQuery('element','eus_latam_domain');
gr.addQuery('inactive','false');
gr.addQuery('sys_id',domain.toString());
gr.query();
 
while (gr.next()) {
return gr.label;
}
}
 
function getCategory(category)
{
var gr = new GlideRecord('sys_choice');
gr.addQuery('name', 'question_choice');
gr.addQuery('element','eus_latam_category');
gr.addQuery('inactive','false');
gr.addQuery('sys_id',category.toString());
gr.query();
 
while (gr.next()) {
return gr.label;
}
}
 
function getService(service)
{
var gr = new GlideRecord('sys_choice');
gr.addQuery('name', 'question_choice');
gr.addQuery('element','eus_latam_service');
gr.addQuery('inactive','false');
gr.addQuery('sys_id',service.toString());
gr.query();
 
while (gr.next()) {
return gr.label;
}
}
}
9 REPLIES 9

Hi @Ponnada,

Yes, that could work. Are you running into issues with it?

Of course, you could also follow @Sandeep Rajput's option, if you feel comfortable enough to create the GlideAjax call. This would be better for performance.


Help others to find a correct solution by marking the appropriate response as accepted solution and helpful.

Hi @Peter Bodelier ,

 

Yes, It is still not working.I am not sure where I am going wrong.

 

Best

Kasthuri

Hi @Ponnada,

 

Place debugger; as first line in your function. This way you can check in your browser console where it's going wrong.

See Debugging in the browser (javascript.info) on how to use that.


Help others to find a correct solution by marking the appropriate response as accepted solution and helpful.

Sandeep Rajput
Tera Patron
Tera Patron

@Ponnada Instead of making the synchronous GlideRecord calls from he client script, you can replace them with GlideAjax call by creating a script include and making the API call asynchronous. 

 

Please refer to this URL to know more about script includes https://developer.servicenow.com/dev.do#!/learn/learning-plans/utah/new_to_servicenow/app_store_lear...

That is also a possibility, but overcomplicates things for many people.

 

Using asynchronous query does the job as well, and is easier to understand.


Help others to find a correct solution by marking the appropriate response as accepted solution and helpful.