Autopopulating fields in Backend but not in Service Portal

alt
Tera Contributor

Hi, I need help in auto populate as it is working on the backend but not in service portal

 

Client Script:

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }

    var ga = new GlideAjax('GetVariableDetails');
    ga.addParam('sysparm_name', 'GetVariableDetails');
    ga.addParam('sysparm_id', newValue);
    ga.getXML(RequestorParse);

    function RequestorParse(response) {
        var answer = response.responseXML.documentElement.getAttribute('answer');
        if (answer) {
            try {
                var reqItemDetails = JSON.parse(answer);
                g_form.setValue('dmr_cost_center', reqItemDetails.dmr_cost_center || '');
                g_form.setValue('cost_center', reqItemDetails.cost_center || '');
                g_form.setValue('dmr_department', reqItemDetails.dmr_department || '');
                g_form.setValue('department', reqItemDetails.department || '');
                g_form.setValue('dmr_location', reqItemDetails.dmr_location || '');
                g_form.setValue('location', reqItemDetails.location || '');
                g_form.setValue('meeting_room_name_or_number', reqItemDetails.meeting_room_name_or_number || '');
                g_form.setValue('purpose_of_installation', reqItemDetails.purpose_of_installation || '');
            } catch (error) {
                g_form.addErrorMessage("Error parsing JSON response: " + error.message);
            }
        } else {
            g_form.addErrorMessage("Failed to get a valid response from the server.");
        }
    }
}
 
Script Include:
//script Include dmr

var GetVariableDetails = Class.create();
GetVariableDetails.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    GetVariableDetails: function() {
        var requestId = this.getParameter('sysparm_id');
        gs.log("Request ID: " + requestId);

        var response = {};
        var gr = new GlideRecord('sc_req_item');

        if (gr.get(requestId)) {
            response.dmr_cost_center = gr.variables.dmr_cost_center.getDisplayValue();
            response.cost_center = gr.variables.cost_center.getDisplayValue();
            response.dmr_department = gr.variables.dmr_department.getDisplayValue();
            response.department = gr.variables.department.getDisplayValue();
            response.dmr_location = gr.variables.dmr_location.getDisplayValue();
            response.location = gr.variables.location.getDisplayValue();
            response.meeting_room_name_or_number = gr.variables.meeting_room_name_or_number.getDisplayValue();
            response.purpose_of_installation = gr.variables.purpose_of_installation.getDisplayValue();
        }

        gs.log("Response: " + JSON.stringify(response));
        return JSON.stringify(response);
    }
});

 

Back End:

alt_0-1737364558786.png

Service Portal:

alt_1-1737364597794.png

 

4 REPLIES 4

Ct111
Giga Sage

Check the client script UI type in the backend , is it selected to work only on DESKTOP or  All  .

 

If not make the change .

 

LINK

I hope this helps

Ankur Bawiskar
Tera Patron
Tera Patron

@alt 

ensure UI type-ALL for your client script

Also try to use getXMLAnswer()

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }

    var ga = new GlideAjax('GetVariableDetails');
    ga.addParam('sysparm_name', 'GetVariableDetails');
    ga.addParam('sysparm_id', newValue);
    ga.getXMLAnswer(function(answer){
        if (answer) {
            try {
                var reqItemDetails = JSON.parse(answer);
                g_form.setValue('dmr_cost_center', reqItemDetails.dmr_cost_center || '');
                g_form.setValue('cost_center', reqItemDetails.cost_center || '');
                g_form.setValue('dmr_department', reqItemDetails.dmr_department || '');
                g_form.setValue('department', reqItemDetails.department || '');
                g_form.setValue('dmr_location', reqItemDetails.dmr_location || '');
                g_form.setValue('location', reqItemDetails.location || '');
                g_form.setValue('meeting_room_name_or_number', reqItemDetails.meeting_room_name_or_number || '');
                g_form.setValue('purpose_of_installation', reqItemDetails.purpose_of_installation || '');
            } catch (error) {
                g_form.addErrorMessage("Error parsing JSON response: " + error.message);
            }
        } else {
            g_form.addErrorMessage("Failed to get a valid response from the server.");
        }
     });
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

alt
Tera Contributor

it is All still not showing in SP

@alt 

what debugging did you do?

did the script include call happen? Did you see some logs in script include?

what came in alert for the answer?

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader