Javascript within a client script within servicenow

danramirz
Giga Contributor
I have a custom table. In the form view there is a call name field (references the sys_user). What I want to is that when the form loads there should be an icon next to the caller name. This icon would allow the user to see the past incident records of the user. I run into an issue with a backtic. I do not know how to correct this error. Any ideas?Capture.PNG
 
function onLoad() {
    var callerField = g_form.getControl('u_caller_name');

    if (callerField) {
        var icon = document.createElement('span');
        icon.innerHTML = '📜'; // History icon
        icon.style.cursor = 'pointer';
        icon.style.marginLeft = '10px';
        icon.title = 'View Incident History';

        icon.onclick = function () {
            showIncidentHistory();
        };

        callerField.parentNode.appendChild(icon);
    }
}

function showIncidentHistory() {
    var userName = g_form.getValue('u_caller_name');

    if (!userName) {
        alert("Please select a user first.");
        return;
    }

    var ga = new GlideAjax('IncidentHistoryAjax');
    ga.addParam('sysparm_name', 'getHistory');
    ga.addParam('sysparm_user', userName);

    ga.getXMLAnswer(function(response) {
        if (!response) {
            alert("No incident history found.");
            return;
        }

        var historyData = JSON.parse(response);
        var historyHTML = "<table border='1' width='100%'><tr><th>Number</th><th>Short Description</th><th>State</th><th>Created</th></tr>";

        if (historyData.length === 0) {
            historyHTML += "<tr><td colspan='4'>No past incidents found.</td></tr>";
        } else {
            historyData.forEach(function(record) {
                historyHTML += `<tr>
                    <td>${record.number}</td>
                    <td>${record.short_description}</td>
                    <td>${record.state}</td>
                    <td>${record.created}</td>
                </tr>`;
            });
        }

        historyHTML += "</table>";

        var modal = new GlideModal('Incident History');
        modal.setTitle('Incident History for ' + userName);
        modal.setBody(historyHTML, false, false);
        modal.render();
    });
}
0 REPLIES 0