How to call script include from client script | #servicenow #GlideAjax (2024)

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 includevar ga = new GlideAjax('global.sampleUtils'); //Scriptincludega.addParam('sysparm_name', 'getUserDetails'); //Methodga.addParam('userId',user); //Parametersga.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);}}

How to call script include from client script | #servicenow #GlideAjax (1)

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'});

How to call script include from client script | #servicenow #GlideAjax (2)

That's it, now as soon as you select a user your fields will auto-populate based on that.

How to call script include from client script | #servicenow #GlideAjax (3)
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.

How to call script include from client script | #servicenow #GlideAjax (2024)
Top Articles
Latest Posts
Article information

Author: Fr. Dewey Fisher

Last Updated:

Views: 6261

Rating: 4.1 / 5 (62 voted)

Reviews: 93% of readers found this page helpful

Author information

Name: Fr. Dewey Fisher

Birthday: 1993-03-26

Address: 917 Hyun Views, Rogahnmouth, KY 91013-8827

Phone: +5938540192553

Job: Administration Developer

Hobby: Embroidery, Horseback riding, Juggling, Urban exploration, Skiing, Cycling, Handball

Introduction: My name is Fr. Dewey Fisher, I am a powerful, open, faithful, combative, spotless, faithful, fair person who loves writing and wants to share my knowledge and understanding with you.