Using EVAL blocks
This document describes the #EVAL[]
function to utilise in the Platform for evaluating any JavaScript expression.
Usage
The #EVAL[]
function is used to evaluate a JavaScript expression dynamically. The main intention of this #EVAL function is to allow users to perform calculations and execute code at runtime.
Server-Side (Evaluate Expressions)
Users can use this #EVAL[]
in the components like Templates. For example, you may have a calculated field that performs a dynamic calculation based on other field values. To use #EVAL in an expression, enclose the JavaScript expression within #EVAL[]
.
Example: If you have two numeric fields "Sub1" and "Sub2," and you want to sum both values, you can use the following expression:
#EVAL[ var sub1= rbv_api.getFieldValue("Marks", id,"sub1"); var sub2= rbv_api.getFieldValue("Marks", id,"sub2"); return sub1+sub2; ]
Client-Side (Evaluate in Scripting)
Users can also use #EVAL[]
in client-side scripting. For example: In the Design This Page section, the user can use the #EVAL[]
in the HTML component section. The following example loops through records but renders <img> tag only for records with an amount > 1000.
#EVAL[ <ul> {!#LOOP_BEGIN.R8011504#8108944} <li>#EVAL[{!R8011504.amount}>1000 ? "<img src='important.gif'>" : ""] Bill: {!order.name} Amount: {!R8011504.amount}</li> {!#LOOP_END.R8011504} <ul> ]
Debug #EVAL[] block script
#EVAL[]
block allows you to use simple JavaScript-based expressions in a Platform template. To debug an #EVAL[]
block in the Template Helper, click Test EVAL[] Block. The syntax for an EVAL block is as follows:
#EVAL[ JavaScript returning String ]
The following example loops through records but renders <img> tag only for records with amount > 1000:
<ul> {!#LOOP_BEGIN.R8011504#8108944} <li>#EVAL[{!R8011504.amount}>1000 ? "<img src='important.gif'>" : ""] Bill: {!order.name} Amount: {!R8011504.amount}</li> {!#LOOP_END.R8011504} <ul>
Portal page
You can add an #EVAL[]
block to a portal page to add JavaScript-based extensions to the page. You can use an #EVAL[]
block when you want to use JavaScript expressions to generate HTML. An #EVAL[]
block returns a String. You can use server-side Platform APIs in an #EVAL[]
block. See Using EVAL blocks for more information about #EVAL[]
blocks. See Server-side API for more information about server-side Platform APIs.
To add an #EVAL[]
block to a page:
-
Open the page editor in one of the following ways: From the portal view, click Edit in the page table next to the page you want to edit. From the portal page view, click Edit. The page editor opens.
-
Drag New Script Component from the left column to the location where you want to add the
#EVAL[]
block. A new <Script Component> appears on the page. The component can be in its section or can be added to an existing section, depending on the page type and existing content. In the following graphic, the component is added to the default section of a generic page: -
Click Edit in the <Script Component>. The Edit Script dialogue opens:
-
Add the JavaScript
#EVAL[]
block to the text area. You can use the Template Helper to generate tokens for IDs, URLs, and links you want to use in the block. -
When you are finished adding the
#EVAL[]
block, click Save in the Edit Script dialogue and click Save in the page editor.
The following #EVAL[]
block:
-
Defines a function f1().
-
The function uses the server-side query API to execute a SELECT query that returns the id, name, and author fields from Title records.
-
The function then loops through the query results and generates a buffer containing an HTML table that displays the title and author of each Title record.
-
Each displayed title links to the portal's Object View page which displays information about a single title. The link uses tokens for the portal, the portal's Object View page, and the current Title record ID.
-
The function returns the resulting HTML.
-
Finally, the
#EVAL[]
block calls the function.#EVAL[ function f1() { var arr = rbv_api.selectQuery("SELECT id,name,author FROM title_lb", 1000); var buff = '<table cellpadding=5><tr><th>Title</th><th>Author</th></tr>\n'; for (var k=0; k<arr.length; k++) buff += '<tr><td> <a href="portal.jsp?c={!#CURR_CUSTM.id} &p={!#PORTAL.125233463.#id}&g={!#PORTAL.125233463.127055194#id} &id='+arr[k][0]+'">' +arr[k][1]+'</a></td>'+'<td>'+arr[k][2]+'</td></tr>\n'; buff += '</table>\n'; return buff; } f1(); ]
The resulting portal page looks like the following:
Merge Tokens
You can use merge tokens in the #EVAL[]
block. For example:
#EVAL[#CALC_COUNT.Bug( 1 | true )] #EVAL["{!name#text}"]
Hosted Files
You can use the #EVAL[]
block in a hosted file (E.g. testeval.js) with the following content.
#EVAL[ return "alert('Hello World');"; ]
In any record view page, add a script component with the following content and save. On saving the page “Hello World“ an alert should be displayed.
<script> {!#HOSTED_FILE.YPqxFA5LToq3i9z2lyZmFg#text} </script>
Recommended list of features which can utilize the #EVAL[] function
In the following features, the #EVAL[]
block is recommended to use.
-
Templates
-
Email Templates
-
Subject
-
Body
-
-
Document Template
-
-
Template Field
-
Application pages
-
HTML Component
-
Script Component
-
-
Portals pages
-
HTML Component
-
Script Component
-
-
Reports
-
Tabs & Menus
-
Custom Sidebar
-
Hosted Files
Not Recommended list of features along the #EVAL[] function
In the following features, the #EVAL[]
block may work but is not recommended to use as the user can write JavaScript code directly.
-
Expression Field.
-
Formula Field
-
Custom Methods
-
Post Install Scripts
Unsupported list of features along the #EVAL[] function
The following list of features does not support the #EVAL[]
block.
-
EVAL block within another EVAL block
-
Page Properties
-
Application Header and Footer
-
Portal pages - Header and Footer
-
PDF - Header and Footer
-
Text boxes (Eg.: Default value) & Text areas (Rich Text Editor)