diff --git a/Client-Side Components/Catalog Client Script/Autopopulate user information fields/ClientCallableScriptInclude.js b/Client-Side Components/Catalog Client Script/Autopopulate user information fields/ClientCallableScriptInclude.js new file mode 100644 index 0000000000..0e17bbe045 --- /dev/null +++ b/Client-Side Components/Catalog Client Script/Autopopulate user information fields/ClientCallableScriptInclude.js @@ -0,0 +1,25 @@ +/* +* The following is a client callable script include. This can be used with the onChange Client script to be able to gather the data on the server side +*/ + +var ReferenceQualifierAjaxHelper = Class.create(); +ReferenceQualifierAjaxHelper.prototype = Object.extendsObject(AbstractAjaxProcessor, { + getUserInformation : function() { + var userID = this.getParameter('sysparm_user'); + var userRec = new GlideRecord('sys_user'); + + if(userRec.get(userID)) { + var results = { + "email" : userRec.getValue('email'), + "department" : userRec.getValue('department'), + "title" : userRec.getValue('title'), + "phone" : userRec.getValue('phone') + }; + + return JSON.stringify(results) + } + + }, + + type: 'ReferenceQualifierAjaxHelper' +}); diff --git a/Client-Side Components/Catalog Client Script/Autopopulate user information fields/README.md b/Client-Side Components/Catalog Client Script/Autopopulate user information fields/README.md new file mode 100644 index 0000000000..0614b60044 --- /dev/null +++ b/Client-Side Components/Catalog Client Script/Autopopulate user information fields/README.md @@ -0,0 +1,6 @@ +## Overview +This onchange catalog client script and script inlcude work together autopopulate the user fields that might show up on a catalog item. In the +global scope you will have to create the client callable script include to be able to use the Ajax call that is in the on change client script. +In this example we use the OOB Requested For field that already auto populates the user that is logged in then we go to the server to get that +users information. The fields that are brough back are the ones that are in the code but you can modify to bring back more or less fields if needed. + diff --git a/Client-Side Components/Catalog Client Script/Autopopulate user information fields/onChangeClientScript.js b/Client-Side Components/Catalog Client Script/Autopopulate user information fields/onChangeClientScript.js new file mode 100644 index 0000000000..805547fe44 --- /dev/null +++ b/Client-Side Components/Catalog Client Script/Autopopulate user information fields/onChangeClientScript.js @@ -0,0 +1,23 @@ +/* +* In order for this to work make sure to have an onChange catalog client script on a variable that is type Requested For. This variable +* already autopopulates the logged in user with its OOB functionality. In the updateUserFields function you can add any other user fields +* that you might need. +*/ + +function onChange(control, oldValue, newValue, isLoading) { + //This variable will store the sys_id of the user that populates in your requested for variable + var userID = newValue; + + var ga = new GlideAjax(ReferenceQualifierAjaxHelper); + ga.addParam('sysparm_name', 'getUserInformation'); + ga.addParam('sysparm_user', userID); + ga.getXMLAnswer(updateUserFields); + + function updateUserFields(response) { + var returnedData = JSON.parse(response); + g_form.setValue("email", returnedData.email); + g_form.setValue("department", returnedData.department); + g_form.setValue("title", returnedData.title); + g_form.setValue("phone", returnedData.phone); + } +}