Incident Details are not populated in the dynamic content blocks for content blocks dash board

lakshmi123
Tera Contributor

Hi Team, 

I have used the below script to get incident details in the dynamic content blocks for content blocks dash bord, for this I have write one script include and one content block script. Can anyone help me to get correct output.

Script include: 

lakshmi123_0-1736532229930.png

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

    // Method to get incident details based on sys_id
    getIncidentDetails: function() {
        // Get the sys_id from the request
        var sys_id = this.getParameter('sysparm_task_type');
       
        // Create a GlideRecord to retrieve the incident details
        var gr = new GlideRecord('incident');
        if (gr.get(sys_id)) {
            // Prepare the result object with necessary incident fields
            var result = {
                number: gr.getValue('number'),
                short_description: gr.getValue('short_description'),
                state: gr.getDisplayValue('state'),
                description: gr.getValue('description'),
                priority: gr.getDisplayValue('priority'),
                caller_id: gr.getDisplayValue('caller_id'),
                assigned_to: gr.getDisplayValue('assigned_to'),
                created_on: gr.getValue('sys_created_on')
            };

            // Return the result as a JSON object
            return new JSON().encode(result);
        }
       
        // If the incident is not found, return an error message
        var error = { error: "Incident not found or invalid sys_id." };
        return new JSON().encode(error);
    }
});
Content block script :
<?xml version="1.0" encoding="utf-8" ?>
<j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide">
    <g:evaluate var="jvar_tasktypes" object="true" jelly="true">
        var incidents = [];
        var gr = new GlideRecord('incident');
        gr.addQuery('short_description', ''); // Filter incidents with an empty short_description
        gr.orderBy('number');
        gr.setLimit(5); // Limit the number of records for display (optional)
        gr.query();
        while (gr.next()) {
            incidents.push({
                sys_id: gr.getUniqueValue(),
                number: gr.getValue('number')
            });
        }
        incidents;
    </g:evaluate>

    <div class="container">
        <div class="col-md-3">
            <div class="panel-body">
                <h4>Select an Incident</h4>
                <select id="filter_task_type" class="form-control" onchange="filterTaskType()">
                    <option value="">-- Select Incident --</option>
                    <j:forEach var="jvar_tasktype" items="${jvar_tasktypes}">
                        <option value="${jvar_tasktype.sys_id}">${jvar_tasktype.number}</option>
                    </j:forEach>
                </select>
            </div>
        </div>
        <div class="col-md-9">
            <div class="panel-body">
                <h3>Incident Details</h3>
                <div id="incDetails">
                    <div class="list-group">
                        <p>Select an incident to view details.</p>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <script>
        function filterTaskType() {
            var taskType = document.getElementById('filter_task_type').value;
            console.log("Selected task type (sys_id): " + taskType); // Debugging the selected sys_id

            var incDetails = document.getElementById('incDetails').querySelector('.list-group');
            incDetails.innerHTML = '<p>Loading...</p>'; // Display loading text

            if (!taskType) {
                incDetails.innerHTML = '<p>Please select an incident.</p>';
                return;
            }

            // GlideAjax call (ensure the Script Include method is correct in ServiceNow)
            var ga = new GlideAjax('BillingInfoUtil'); // The Script Include must be named 'BillingInfoUtil'
            ga.addParam('sysparm_name', 'getIncidentDetails'); // Ensure the method name is correct in your Script Include
            ga.addParam('sysparm_task_type', taskType);
            ga.getXMLAnswer(function(response) {
                console.log("Raw Response: ", response); // Debugging the raw response

                try {
                    var result = JSON.parse(response);
                    console.log("Parsed result: ", result); // Log the parsed result

                    if (result.error) {
                        incDetails.innerHTML = '<p>' + result.error + '</p>';
                        return;
                    }

                    var details = result; // Assume result contains the details object directly
                    console.log("Incident details: ", details); // Debugging the fetched details

                    // Check if details.number exists
                    if (!details.number) {
                        console.error("No number field found in incident details.");
                        incDetails.innerHTML = '<p>Incident number not found.</p>';
                        return;
                    }

                    // Dynamically generate the HTML to display incident details with a tab-like spacing
                    var html = `
                        <div class="list-group-item">
                            <p><strong>Number:</strong>&#160;&#160;&#160;&#160;${details.number || 'N/A'}</p>
                        </div>
                        <div class="list-group-item">
                            <p><strong>Short Description:</strong>&#160;&#160;&#160;&#160;${details.short_description || 'N/A'}</p>
                        </div>
                        <div class="list-group-item">
                            <p><strong>State:</strong>&#160;&#160;&#160;&#160;${details.state || 'N/A'}</p>
                        </div>
                        <div class="list-group-item">
                            <p><strong>Description:</strong>&#160;&#160;&#160;&#160;${details.description || 'N/A'}</p>
                        </div>
                        <div class="list-group-item">
                            <p><strong>Priority:</strong>&#160;&#160;&#160;&#160;${details.priority || 'N/A'}</p>
                        </div>
                        <div class="list-group-item">
                            <p><strong>Caller:</strong>&#160;&#160;&#160;&#160;${details.caller_id || 'N/A'}</p>
                        </div>
                        <div class="list-group-item">
                            <p><strong>Assigned To:</strong>&#160;&#160;&#160;&#160;${details.assigned_to || 'N/A'}</p>
                        </div>
                        <div class="list-group-item">
                            <p><strong>Created On:</strong>&#160;&#160;&#160;&#160;${details.created_on || 'N/A'}</p>
                        </div>`;
                    incDetails.innerHTML = html;

                } catch (e) {
                    incDetails.innerHTML = '<p>An error occurred while processing the response.</p>';
                    console.error("Error parsing response: ", e);
                }
            });
        }
    </script>
</j:jelly>
 
lakshmi123_1-1736532334972.png

OUTPUT:

lakshmi123_2-1736532372967.png

 

 

Please help me to get incident details.

3 REPLIES 3

Mark Manders
Mega Patron

Can you check on your parameter naming? The Script Include expects sysparm_task_type but the content block script uses sysparm_name for the method and sysparm_task_type for the task type.


Please mark any helpful or correct solutions as such. That helps others find their solutions.
Mark

lakshmi123
Tera Contributor

give me correct code

Sorry? You already have the code (see your own question) and need to make sure that the parameters are correct, since your script include is expecting something else than your content block is using.

Next to that, replying with 'give me correct code' is a very rude response on a community where people are helping others (in their own time). 


Please mark any helpful or correct solutions as such. That helps others find their solutions.
Mark