Print List view of record with g_modal

ABC6
Tera Contributor

Hello Team,

I need to print the list of incident as a modal in SOW when we select caller from caller field in an incident form,

i am new to SOW , Please guide me to help this issue resolved

5 REPLIES 5

Aniket15
ServiceNow Employee
ServiceNow Employee

Hey @ABC6  any specific reason you want to use g_modal ? why not use declarative actions ?

ABC6
Tera Contributor

beacause i did implemented with g_modal by refering one community article of Ashly Snider and it get works ,  ut i dont want specific field i want to print whole list view and with the declarative action it is going to be too much complex

Deepak Shaerma
Kilo Sage

Hi @ABC6 
you need to create a new client script and script include.

Step 1: Create a Onchange client script.
Table : incident

Field Name: Caller

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

    var ga = new GlideAjax('GetCallerIncidents');
    ga.addParam('sysparm_name', 'getIncidentsForCaller');
    ga.addParam('sysparm_caller_id', newValue);
    ga.getXMLAnswer(function(answer) {
        var incidents = JSON.parse(answer);
        if (incidents.length > 0) {
            // Construct HTML for modal content
            var modalContent = '<table><tr><th>Incident Number</th><th>Short Description</th></tr>';
            incidents.forEach(function(incident) {
                modalContent += '<tr><td>' + incident.number + '</td><td>' + incident.short_description + '</td></tr>';
            });
            modalContent += '</table>';

            // Display the modal
            var modal = new GlideModal();
            modal.setTitle('Related Incidents');
            modal.setBody(modalContent, false); // false to not automatically escape HTML
            modal.render();
        } else {
            alert("No related incidents found for this caller.");
        }
    });

}


Step 2: Create a Script Include 
Name : GetCallerIncidents
Client callable is true 


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

    getIncidentsForCaller: function() {
        var callerId = this.getParameter('sysparm_caller_id');
        var incidentsArray = [];

        var gr = new GlideRecord('incident');
        gr.addQuery('caller_id', callerId);
        gr.query();

        while (gr.next()) {
            var incident = {};
            incident.number = gr.number.toString();
            incident.short_description = gr.short_description.toString();
            incidentsArray.push(incident);
        }

        return JSON.stringify(incidentsArray);
    }
});

Please Mark this Helpful and Accepted Solution. If this Helps you to understand. This will help both the community and me..
- Keep Learning ‌‌
Thanks & Regards 
Deepak Sharma 


it is not working for Service operation Workspace