logo

NJP

Call Script Include from Client Script using GlideAjax

Import · Nov 14, 2019 · article

This video demonstrates how we can call a script include from a client script. To demonstrate the same, a custom form is created and user data is fetched from the user table and values are set for a few other fields on the custom form. OnChange client script contains below-mentioned code: function onChange(control, oldValue, newValue, isLoading, isTemplate) { if (isLoading || newValue === '') { return; } var userSelected = g_form.getValue('u_user'); var userDetails = new GlideAjax('UserDetailsUtil'); //script include UserDetailsUtil userDetails.addParam('sysparm_name', 'getUserDetails'); //calling function getUserDetails userDetails.addParam('sysparm_userSelect', userSelected); //passing the user detail as a parameter userDetails.getXMLAnswer(function(response){ var userObj = JSON.parse(response); //parsing the user object received from getUserDetails function g_form.setValue('u_contact_no', userObj.contactNo); //setting the value in necessary fields g_form.setValue('u_user_id', userObj.userId); }); } UserDetailsUtil is the name of Script Include and since it is being called from a client script, it should be client callable. Below code is part of script include: var UserDetailsUtil = Class.create(); UserDetailsUtil.prototype = Object.extendsObject(AbstractAjaxProcessor, { getUserDetails: function(){ //definition for getUserDetails function, called from client script var userObj = {}; //declaring user object var gr = GlideRecord('sys_user'); //gliding through the user object if(gr.get(this.getParameter('sysparm_userSelect'))){ //fetching user data from the parameter passed from client script userObj.contactNo = gr.getValue('mobile_phone'); //getting mobile_phone field from user record and setting in user defined object and similarly for user_name below userObj.userId = gr.getValue('user_name'); } return JSON.stringify(userObj); //returning the user defined object with details }, type: 'UserDetailsUtil' });

Labels:

View original source

https://www.servicenow.com/community/developer-blog/call-script-include-from-client-script-using-glideajax/ba-p/2333458