Can't embed javascript logic which is working fine into UI page Jellyscript tags.

Community Alums
Not applicable

I am using javascript logic to retrieve alert worknotes with associated case records , which are also need to filtered by a specific condition to retrieve work notes that contains "due to event" string, it is working fine for javascript logic when tested in background script but when embeded to use in jellyscript logic - its not working.

 

Javascript Logic(background scripts):

var incs = [];  
var ws = [];        
var gr = new GlideRecord('em_alert');
gr.addQuery("u_case","aa6c5270db0d330092daf3c61d961998");
gr.addEncodedQuery('work_notesLIKEdue to event');        
gr.query();
var myCount = gr.getRowCount();
while (gr.next()) {  
    var obj = {};
    obj.number = gr.getDisplayValue("number");
    obj.work = gr.getDisplayValue("work_notes");
    incs.push(obj); 
}

 

var filteredIncs = incs.filter(function(item) {

    var workLines = item.work.split('\n');

    var dueToEventLines = workLines.filter(function(line) {
        return line.indexOf("due to event") !== -1;
    });

    item.work = dueToEventLines.join('\n');

    return dueToEventLines.length > 0;
});

 

for (var i = 0; i < filteredIncs.length; i++) {
    gs.info("Number: " + filteredIncs[i].number + "\nWork Notes: " + filteredIncs[i].work);
}

 

-------

JellyScripted Same Logic for UIPage:

<?xml version="1.0" encoding="utf-8"?>
<j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">
    <h3>Event table</h3>
    <g:evaluate var="jvar_sysId" expression="RP.getWindowProperties().sysparm_sysid"/>

 

    <g:evaluate jelly="true" object="true" var="jvar_jsonObj">
        var incs = [];
        var gr = new GlideRecord('em_alert');
        gr.addQuery("u_case", jelly.sysparm_sysid);
        gr.query();

 

        while (gr.next()) {
            var obj = {};
            obj.number = gr.getDisplayValue("number");
            obj.work = gr.getDisplayValue("work_notes");
            incs.push(obj);
        }

 

        incs;
    </g:evaluate>

 

    <script>
        // Filter the incs array to include only objects whose work property contains "due to event"
        var filteredIncs = jvar_jsonObj.filter(function(item) {
            var workLines = item.work.split('\n');
            var dueToEventLines = workLines.filter(function(line) {
                return line.indexOf("due to event") !== -1;
            });
            item.work = dueToEventLines.join('\n');
            return dueToEventLines.length > 0;
        });

 

        for (var i = 0; i < filteredIncs.length; i++) {
            gs.info("Number: " + filteredIncs[i].number + "\nWork Notes: " + filteredIncs[i].work);
        }
    </script>

 

    <table border="1">
        <tr>
            <td colspan="2">Event Messages</td>
        </tr>
        <tr>
            <th>Number</th>
            <th>Work Notes</th>
        </tr>
        <j:forEach items="${filteredIncs}" var="item">
            <tr>
                <td>${item.number}</td>
                <td>${item.work}</td>
            </tr>
        </j:forEach>
    </table>
    <style>
        td, th {
            padding: 10px;
        }
    </style>
</j:jelly>

Here jelly.sysparm_sysid is the matching case sysid which is being retrieved from UI action:

   function showEvents() {
    var gm = new GlideModal('event_messages');

gm.setTitle('Show title');

gm.setPreference('sysparm_sysid', g_form.getUniqueValue());
gm.render();

    populateWorkNotes("due to event");
}

// Function to populate work notes based on the specified criteria
function populateWorkNotes(filterString) {
  // You can add any additional logic here if needed
  // For example, you may want to reload the modal after updating work notes
  // or perform other actions.
 
  // Query the records based on the filterString
  var gr = new GlideRecord("em_alert");
  gr.addQuery("u_case", g_form.getUniqueValue());
  gr.addQuery("work_notes", "LIKE", filterString);
  gr.query();

  // Loop through the records and perform actions
  while (gr.next()) {
    // Perform actions with the retrieved records, e.g., update or log
    // For example: gr.work_notes = "Updated work notes";
    // gr.update();
    gr.work_notes;
  }
  g_form.save(); // Save the form to trigger reload with updated work notes
}
 
Can anyone showcase where i am going wrong and how can i correct it?
2 REPLIES 2

Sohithanjan G
Kilo Sage
Kilo Sage

Hi @Community Alums 

In your Jelly script, the issue might be with the way you're trying to iterate over the jvar_jsonObj array. Jelly doesn't directly support JavaScript array iteration like forEach. Instead, you need to use the <j:forEach> tag to loop through the array.

Here's how you can modify your Jelly script to correctly iterate over the jvar_jsonObj array:

 

<?xml version="1.0" encoding="utf-8"?>
<j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">
    <h3>Event table</h3>
    <g:evaluate var="jvar_sysId" expression="RP.getWindowProperties().sysparm_sysid"/>

    <g:evaluate jelly="true" object="true" var="jvar_jsonObj">
        var incs = [];
        var gr = new GlideRecord('em_alert');
        gr.addQuery("u_case", jelly.sysparm_sysid);
        gr.query();

        while (gr.next()) {
            var obj = {};
            obj.number = gr.getDisplayValue("number");
            obj.work = gr.getDisplayValue("work_notes");
            incs.push(obj);
        }

        incs;
    </g:evaluate>

    <table border="1">
        <tr>
            <td colspan="2">Event Messages</td>
        </tr>
        <tr>
            <th>Number</th>
            <th>Work Notes</th>
        </tr>
        <j:forEach items="${jvar_jsonObj}" var="item">
            <j:if test="${item.work.indexOf('due to event') != -1}">
                <tr>
                    <td>${item.number}</td>
                    <td>${item.work}</td>
                </tr>
            </j:if>
        </j:forEach>
    </table>
    <style>
        td, th {
            padding: 10px;
        }
    </style>
</j:jelly>

 

 

In this modified script:

  • I replaced the JavaScript array iteration logic with Jelly's <j:forEach> tag.
  • Inside the <j:forEach> loop, I added a <j:if> tag to check if the work property of each item contains the string "due to event".
  • If the condition is met, it will display the corresponding incident number and work notes in the table row.

This should properly filter and display the incident records with work notes containing "due to event" in your Jelly script.

Mark as helpful & Accepted Solution !!

Please mark as Accepted Solution if this solves your query and HIT Helpful if you find my answer helped you. This will help other community mates too..:)

Community Alums
Not applicable

It's not working and not bringing any content. BTW Thanks but we are not filtering item.work value for 'due to event' because it also contains multiple worknotes wihin every obj.work filed , so need to segragate each of them  and bring only the value which contains due to event .