Can't embed javascript logic which is working fine into UI page Jellyscript tags.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-16-2024 06:20 AM - edited ‎02-16-2024 06:27 AM
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:
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-18-2024 01:44 AM
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 !!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-19-2024 07:41 AM
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 .