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