Help with Javascript in a client script

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? My full code is below. Thank you.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();
    });
}
2 REPLIES 2

GlideFather
Tera Patron

In this line:
historyHTML += `<tr>

replace `<tr> with  '<tr>'

Or use any code editor (e.g. VSC), it could help you to clean your code, there are some online editors but for that, use it only if it does not contain any company details (e.g. sys ids, names, etc).

———
/* If my response wasn’t a total disaster ↙️ drop a Kudos or Accept as Solution ↘️ Cheers! */


Agree, if I recall correctly, ServiceNow does not support multiline strings nor interpolation.  You'll have to go "old school"...