Article Using Server-side Javascript

When should I use Server-side Javascript?

Server-side Javascript (identified with the extension .jsm) is one of the two languages used to implement server side scripts used by scripted customizations. Server-side Javascript is an implementation of the ECMAScript 5.1 specification with access to the full Verint Community Widget/Scripting API. Javascript, and specifically Server-side Javascript, is best suited for processing of data whereas Velocity is better suited for text formatting.

Within a scripted customization, the scripted customization itself is executed within its own local scope with platform APIs exposed via the global scope. This means the Javascript concept "this" refers to the scripted component entry point (the current execution of the widget's content script or the current execution of the theme's body script, for example).

Server-side Javascript Syntax

Server-side Javascript is a full implementation of the ECMAScript 5.1 specification. Language details for ECMAScript 5.1 can be found here.

Widget Extension Usage in Server-side Javascript

Most of the Platform API is available to Server-side Javascript, implemented as a set of widget extensions automatically available in the context of an executed Server-side Javascript script.

For more information, please refer to the full set of Widget Extensions.

Example Widget Extension Usage

The following example will retrieve and render a list of blog posts in the current blog

// Retrieve the current blog. If there is none, hide the widget
var currentBlog = core_v2_blog.Current;
if (!currentBlog)
	return '';

// Store the result in an array that we'll later join to form
// the response string.
var resultHtml = [];

// Retrieve the 5 latest blog posts
var blogPosts = core_v2_blogPost.List({ 
    BlogId: currentBlog.Id, 
    PageSize: 5 
    });

if (blogPosts.Count > 0) {
    resultHtml.push('<ul>');
    for (var i = 0; i < blogPosts.Count; i++) {
        resultHtml.push('<li><a href="');
        resultHtml.push(core_v2_encoding.HtmlAttributeEncode(blogPosts[i].Url));
        resultHtml.push('">');
        resultHtml.push(blogPosts[i].Title);
        resultHtml.push('</a></li>');
    }
    resultHtml.push('</ul>');
} else {
    resultHtml.push('<span class="message">No blog posts</span>');
}

// Combine the array of HTML fragments and return it
return resultHtml.join('\n');

Tips and Tricks

Remember to return

The result of rendering Server-side Javascript is provided via the return statement which can occur anywhere within the script's implementation. If implementing a widget or theme script using Server-side Javascript, be sure to provide the result of the rendering using return.

You can return other types

When executing a Server-side Javascript script from another script (even from Velocity) using core_v2_widget.ExecuteFile(), the Server-side Javascript can return a Javascript object containing multiple values and even functions with contextual state. You can effectively create a custom scripting API by returning a complex object from an executed Server-side Javascript.

Built-in JSON formatting

When a string is expected (for example, when rendering the content of a widget or the response to an Ajax request), Server-side Javascript can return a complex object and the object will be automatically formatted as JSON (with any functions removed). This is a very simple way to provide JSON to custom Ajax callbacks in scripted components.

Working with API Lists

Lists of results from API calls are generally not arrays but can be accessed as if they were within server-side Javascript. An API paged list, when accessed in server-side Javascript, will expose .length and ECMA Script 5.1 array functions such as .forEach(function() { }).