Populate fields on Catalog item from Incident form

NeilCarloB
Tera Contributor

Hello

I have requirement that populate the catalog item fields based on the incident table basically by creating request on incident table and selecting catalog item will auto populate. Please check my code

function onLoad() {
    // Retrieve the 'sysparm_parent_sys_id' (which is the incident sys_id) from the URL
    var incidentSysId = g_url.getParameter('sysparm_parent_sys_id');

    // Check if the 'incident_sys_id' is available
    if (incidentSysId) {
        var gr = new GlideRecord('incident');  // Create a GlideRecord object to query the incident table
        if (gr.get(incidentSysId)) {
            // Retrieve necessary fields from the incident record
            var shortDescription = gr.short_description;
            var description = gr.description;
            var callerId = gr.caller_id;

            // Log the retrieved values (for debugging)
            console.log('Short Description:', shortDescription);
            console.log('Description:', description);
            console.log('Caller ID:', callerId);

            // Set values in the Catalog Item form using 'g_form.setValue'
            if (shortDescription) {
                g_form.setValue('u_short_description', shortDescription);  // Populate 'u_short_description' field
            }
            if (description) {
                g_form.setValue('u_description', description);  // Populate 'u_description' field
            }
            if (callerId) {
                g_form.setValue('u_caller_id', callerId);  // Populate 'u_caller_id' field
            }
        } else {
            console.log('Incident record not found.');
        }
    } else {
        console.log('Incident sys_id not passed to the catalog item.');
    }
}

 

2 REPLIES 2

Bhavya11
Kilo Patron

Hi @NeilCarloB ,

 

Best practices in ServiceNow do not use GlideRecord on client side it will give performance issues.

 

Some issues are there in your code. Please update as below it will work fine

 

Create Script include where client callable  is true

Bhavya11_0-1734339851563.png

 

 

Script sample:

var IncidentUpdate = Class.create();
IncidentUpdate.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    getIncidentInfo: function() {
        var result = {};
        var sysId = this.getParameter('sysparm_userId');
        var taskGR = new GlideRecord('incident');
        taskGR.addQuery('sys_id', sysId);
        taskGR.addQuery('active', true);
        taskGR.query();
        if (taskGR.next()) {
            result.shortDescription = taskGR.getValue('short_description');
            result.description = taskGR.getValue('description');
			result.callerId=taskGR.getValue('caller_id')

        }
        return JSON.stringify(result);

    },
    type: 'IncidentUpdate'
});

 

 

onload catalog client script on catalog item:

function onLoad() {
	var gUrl = new GlideURL();   //GlideURL is a client side class that can help you parse parameters from the URL quickly and safely. Working to get it better documented on developer.service-now.com.

gUrl.setFromCurrent();   
var incidentSysId=gUrl.getParam("sysparm_parent_sys_id");
    // Check if the 'incident_sys_id' is available
    if (incidentSysId) {
        var ga = new GlideAjax('IncidentUpdate');
        ga.addParam('sysparm_name', 'getIncidentInfo');
        ga.addParam('sysparm_userId', incidentSysId);
        ga.getXMLAnswer(function(response) {
                var userDetails = JSON.parse(response);
						

                if (userDetails) {
                        g_form.setValue('u_short_description', userDetails.shortDescription);
                    
                        g_form.setValue('u_description', userDetails.description); 
               
                        g_form.setValue('u_caller_id', userDetails.callerId); 
                    
                }
            });

        }

}

 

Please mark this as Correct or Helpful if it helps.

Thanks,

BK

 

 

Ankur Bawiskar
Tera Patron
Tera Patron

@NeilCarloB 

how are you redirecting user to catalog?

why not send the caller, description etc in URL parameter and use onLoad to get the value and set the variables?

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