0

Client Script

function onLoad() {
    //Type appropriate comment here, and begin script below
    var number = getParameterValue('number');
    if (number != undefined) {
        var ga = new GlideAjax('getRITMValues');
        ga.addParam('sysparm_name', 'getValues');
        ga.addParam('sysparm_number', number);
        ga.getXMLAnswer(getDetails);
    }
 }
 function getParameterValue(name) {
     var url = top.location.href;
    var value = new URLSearchParams(url).get(name);
    if (value) {
        return value;
    }
    if (value != null) {
        var gUrl = new GlideURL();
        gUrl.setFromCurrent();
        value = gUrl.getParam("sysparm_id");
        return value;
    }
 }
 function getDetails(answer) {
    var getData = JSON.parse(answer);
    g_form.setValue("u_first_name", getData.fname);
    g_form.setValue("u_surname", getData.lname);
    g_form.setValue("u_middle_name", getData.middlename);
    g_form.setValue("u_preferred_name", getData.pname);
    g_form.setValue("u_manager_name", getData.managername);
    g_form.setValue("u_location", getData.location);
    g_form.setValue("department", getData.department);
    g_form.setValue("u_newemp_status", getData.u_newemp_status);
    g_form.setValue("u_newemp_title", getData.u_newemp_title);
    g_form.setValue("add_details", getData.add_details);
  }

Script Include

var getRITMValues = Class.create();
getRITMValues.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    getValues: function() {
        var gr = new GlideRecord("sc_req_item");
        if (gr.get("number", this.getParameter('sysparm_number'))) {
             var jsonData = {
                 "fname": gr.variables.u_first_name.toString(),
                "lname": gr.variables.u_surname.toString(),
                "middlename": gr.variables.u_middle_name.toString(),
                "pname": gr.variables.u_preferred_name.toString(),
                "managername": gr.variables.u_manager_name.toString(),
                "location": gr.variables.u_location.toString(),
                "department": gr.variables.department.toString(),
                "u_newemp_status": gr.variables.u_newemp_status.toString(),
                "u_newemp_title": gr.variables.u_newemp_title.toString(),
                "add_details": gr.variables.add_details.toString()
            };
            return JSON.stringify(jsonData);
        }
     },
    type: 'getRITMValues'
});

Get Parameter from URL with Glideajax to auto-populate variables value ServiceNow
Working Code Edited question March 6, 2024