
- Post History
- Subscribe to RSS Feed
- Mark as New
- Mark as Read
- Bookmark
- Subscribe
- Printer Friendly Page
- Report Inappropriate Content
on ‎06-05-2021 03:14 PM
We can call script include from client script using Glideajax.
Calling a script include from the business rule or from any server-side scripting it is very easy and straightforward syntax is there for that.
Whereas the calling script included from the client script will be a little bit tricky, we need to use the GlideAjax approach to call script include.
Let's take a practical example::
Please refer to the full video tutorial from here:
Use case:
On selecting a user field we are trying to populate the user information on other fields.
To get the requirement done,
The first thing we need a client script to trigger on change functionality and then we required a script include to pull the data related to that user record.
Step 1:
Create a client script by selecting type as onchange and select the field as the user
Add the following script to your client script:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var user = g_form.getValue('u_user');
//Call script include
var ga = new GlideAjax('global.sampleUtils'); //Scriptinclude
ga.addParam('sysparm_name', 'getUserDetails'); //Method
ga.addParam('userId',user); //Parameters
ga.getXMLAnswer(getResponse);
function getResponse(response){
console.log(response);
var res = JSON.parse(response);
console.log(res);
g_form.setValue('u_phone',res.mobile_phone);
g_form.setValue('u_email',res.email);
}
}
Step 2:
Create a script include and make it client callable (select the client callable checkbox)
Add the following script to your script include:
var sampleUtils = Class.create();
sampleUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getUserDetails: function(){
gs.addInfoMessage('script include triggered');
var userId = this.getParameter('userId');
gs.addInfoMessage('user scr--'+userId);
obj = {};
var grSysUser = new GlideRecord('sys_user');
if (grSysUser.get(userId)) {
obj.mobile_phone = grSysUser.getValue('mobile_phone');
obj.email = grSysUser.getValue('email');
}
gs.addInfoMessage(obj+JSON.stringify(obj));
return JSON.stringify(obj);
},
type: 'sampleUtils'
});
That's it, now as soon as you select a user your fields will auto-populate based on that.
Thank you so much for watching the tutorial please hit the like button if you like the video and don't forget to subscribe for more interesting videos.
If you have any questions please drop them in the comments below.
- 82,315 Views
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
how to use Global application scope with another scope
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Hi, you can use EfficientGlideRecord function instead of GlideAjax and Script include.
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Not working
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
can we connect
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
What should be the type of the record producer variable?
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
not working
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
When trying to save the Script Include, a popup comes asking to select a user role, what role to select?
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
The music in the video is awful. Why is there music in a coding how to video?
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Would this work for a record producer on the service portal?
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Sharing this live example demo where I added a resuable class and made the AbastractAjaxProcessor become a base class to dynamically call parameters that can revceive any type of class, function and payload from the client towards the server side using GlideAjax.
Dynamic Script Include and GlideAjax in ServiceNow: Scalable & Reusable Code for Architects
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
i created this full course about script include and glide ajax. how to call script include from client script.