CLINT SCRIPT : function onLoad() { //Type appropriate comment here, and begin script below var usrInfo = new GlideAjax('getuserInfo'); usrInfo.addParam('sysparm_name', 'getUserPersonalInfo'); usrInfo.addParam('sysparm_user', g_user.userID); usrInfo.getXML(getBase); function getBase(response) { var answer = response.responseXML.documentElement.getAttribute("answer"); var result = JSON.parse(answer); for (var fieldName in result) { g_form.setValue(fieldName, result[fieldName]); } } SCRIPT INCLUDE FUNCTION : getInfo: function() { var data = {}; var id = this.getParameter("sysparm_user"); var gr = new GlideRecord('sys_user'); gr.addEncodedQuery('sys_id=' + id); gr.query(); if (gr.next()) { data.mobile = gr.getValue('mobile_phone'); data.user_name = gr.getValue('user_name'); //Data.User_name - backend name of variable of Record producer or catlog item var gr1 = new GlideRecord('cmn_cost_center'); gr1.addEncodedQuery('sys_id=' + gr.getValue('cost_center')); gr1.query(); if (gr1.next()) { data.cost_center = gr1.getValue('code'); } var hrProf = new GlideRecord('sn_hr_core_profile'); hrProf.addEncodedQuery('user=' + id); hrProf.query(); if (hrProf.next()) { data.title_name = hrProf.getDisplayValue('position'); } data.union_code = gr.getDisplayValue('u_union_code'); if (gr.company == '56884ceadbcc5380051eff971d961977' || gr.company == '595892a4db381380051eff971d9619da') { data.company = true; } else { data.company = false; } } var json = new global.JSON(); var obj = json.encode(data); return obj; }, getUserPersonalInfo: function() { try { var data = {}; var id = this.getParameter("sysparm_user") || gs.getUserID(); var userRec = new GlideRecord('sys_user'); if (userRec.get(id)) { //User Fields data.first_name = userRec.getValue("first_name") || ""; data.middle_name = userRec.getValue("middle_name") || ""; //HR Profile Fields var hrProfileRec = new GlideRecord("sn_hr_core_profile"); hrProfileRec.addQuery("user", userRec.getUniqueValue()); hrProfileRec.query(); if (hrProfileRec.next()) { data.ght_id = hrProfileRec.getValue("sn_y"); var address = hrProfileRec.getValue("address") || ""; if (address.toString().indexOf(",") > 0) { data.home_address_1 = address.toString().split(",")[0]; data.home_address_2 = address.toString().split(",")[1]; } else { data.home_address_1 = address.toString(); data.home_address_2 = ""; } var city = hrProfileRec.getValue("city") || ""; if (city != "") { if (hrProfileRec.getValue("city").toString().indexOf(",") > 0) { var arr = hrProfileRec.getValue("city").toString().split(","); data.home_city = arr[0] || ""; data.home_state = arr[1] || ""; } else { data.home_city = hrProfileRec.getValue("city") || ""; data.home_state = hrProfileRec.getValue("state") || ""; } }
Script Include Example – Get User Data
ControlF5 Answered question April 12, 2024
Great job, ControlF5! Your script demonstrates excellent use of GlideAjax and GlideRecord to fetch user data and populate fields. Keep up the fantastic work!
Script Include Example – Get User Data
ControlF5 Answered question April 12, 2024