Unable to call the script include in scoped app

Rakesh50
Mega Sage

Hi,

 

We have 'nums' script include and it has few table sys_id's so which we used whenever it required. We have scoped application and' nums' SI also in same scope. We also have 'practice' reference field and it has 3 records and we stored this sys_id's in 'nums' SI. Now we have a requirement that if 'practice' field is 'service' we have to populate some fields.

for that i tried below script but that is throwing this below error.

onChange script error: ReferenceError: nums is not defined function () { [native code] }

 

 

 

ClientScript:- 
if(newValue.length > 0) {
		var isServices = newValue == nums.practice.Services;
		alert(isServices);
   g_form.setDisplay('requested', true);
}
Script Include:-
 var nums = {
  
    country : {
        other: "661fdeaddbb8c0106982e2e3ca9619d2"
    },
    practice: {            
        Services: "da56ad1f474ae11060475a37e26d43c8",                  
        Security: "9e60b9c5970a2d504dc2f5efe153af4b",         
        Social: "2abddab1871969103208113e3fbb3537",
    },
};

 

 

@AnveshKumar M 

 

10 REPLIES 10

Ankit Rawat
Giga Guru

Hi @Rakesh50 ,

 

For calling  nums function you have to use GlideAjax API.

 

https://developer.servicenow.com/dev.do#!/reference/api/tokyo/client/c_GlideAjaxAPI 

 

Go through this documentation, here you will find how to call Scrip Include Functions from Client script.

 

Regards,

Ankit

Ratnakar7
Mega Sage
Mega Sage

Hi @Rakesh50 ,

 

To access a script include from a client script, you need to use the "glideAjax" API or the "server" object in the client script to make a server-side call to the script include.

Here's an example of how to use glideAjax to get the "nums" object in your client script:

 

->>Define a glideAjax function in your client script:

  var ga = new GlideAjax('myScopedAppName.myScriptIncludeName');
  ga.addParam('sysparm_name', 'getNums');
  ga.getXML(callback);

Note that you need to replace "myScopedAppName" with the name of your scoped application, and "myScriptIncludeName" with the name of your script include.

 

->> In your client script, call the callback function and use the response to access the "nums" object:

function callback(response) {
  var nums = JSON.parse(response.responseXML.documentElement.getAttribute('answer'));
  var isServices = newValue == nums.practice.Services;
  alert(isServices);
  g_form.setDisplay('requested', true);
}

Note that the response from glideAjax is in XML format, so you need to parse it into JSON to access the "nums" object. Also, make sure that the "getNums" function is defined in your script include and returns the "nums" object.

 

Thanks,

Ratnakar

 

Hi @Ratnakar7 ,

Thanks for the reply. In the question which I mentioned script which is working fine in Global scope but not sure why it is not working in scoped application. I'm getting nums is not defined function error. I didn't see they didn't mentioned "server" object

Client script:- 

if(newValue.length > 0) {
		var isManagedServices = newValue == Enums.practice.Services;
		if(isManagedServices) {
			var queue = g_form.getValue('u_queue');
			if(newValue.length > 0 && queue.length > 0) {
				var queues = Enums.queues.assignee.Queue(newValue);
				g_form.setMandatory('requested, (queues.L1_CAT == queue || queues.L2 == queue));	
			}
		} else {
			g_form.setMandatory('requested', false);
		}

Script include :-


var Enums = {
  
    country : {
        otherCountry: "661fdeaddbb8c0106982e2e3ca9619d2"
    },
    practice: {            
        Services: "da56ad1f474ae11060475a37e26d43c8",            
        Social: "615b9b9497c2a9504dc2f5efe153af42",            
        Security: "2abddab1871969103208113e3fbb3537",
 }
 -------etc
};

@AnveshKumar M @Ratnakar7 the actual script is like above but it is in Global scope and legacy instance.

Now we are trying to use it same as in new instance but it is in scoped application.  But we are getting mentioned errors