9.2. CommandResult - A Script's Result

We will now get back to ReportServer scripts and have a closer look at the object returned by a script. In theory, you can return any object from a script. For example the script

"Hello World"

returns an object of type String. In order to transport the result onto the client the terminal process wraps such a result in an object of type CommandResult (located in package net.datenwerke.rs.terminal.service.terminal.obj) which the client knows how to handle. If executed in the terminal the terminal will extract the string and display

reportserver$ exec hello.groovy
Hello World

This is done dynamically behind the scenes. But, you can also make this explicit and directly return a CommandResult. For this, you need to change the script to

import net.datenwerke.rs.terminal.service.terminal.obj.CommandResult
new CommandResult("Hello World")

When executing this script the result is identical to before. However, making the CommandResult explicit will give us more control over how the client treats the result. In Chapter 3. we have already seen how to add hyperlinks and anchors to CommandResults. You can furthermore add simple lists, tables and strings to structure the output on the terminal. Consider the following script

import net.datenwerke.rs.terminal.service.terminal.obj.CommandResult

def result = new CommandResult() 
result.addResultList(['Hello','World'])
result.addResultLine('----------')
result.addResultList(['Hello','World'])

return result

Executing this script produces the following output

reportserver$ exec hello.groovy
Hello
World
----------
Hello
World

Besides lists and tables, you can also directly return HTML.

import net.datenwerke.rs.terminal.service.terminal.obj.CommandResult

def result = new CommandResult() 
result.addResultHtml('<b>Hello</b> World')

return result

These techniques, however, are limited to format the results of a script when displayed on a terminal. The CommandResult object, on the other hand, is not limited to terminal formatting. A CommandResult object can contain one or more objects of type CommandResultExtensions which trigger certain responses on the client. For example, this allows you to display popup messages or execute arbitrary JavaScript code.

9.2.1. Displaying Messages

In order to display popup messages your script needs to add a CommandResultExtension of type CreMessage to the CommandResult object. Following is a simple example.

import net.datenwerke.rs.terminal.service.terminal.obj.*

def result = new CommandResult() 
  
def msg = new CreMessage("Hello World")
result.addExtension(msg)

return result

You can further control the size of the window and also directly add HTML.

import net.datenwerke.rs.terminal.service.terminal.obj.*

def result = new CommandResult() 
  
def msg = new CreMessage()
msg.setTitle("Hello")
msg.setWindowTitle("Hello")
msg.setWidth(400)
msg.setHeight(300)
msg.setHtml("<b>Hello World</b>")
result.addExtension(msg)

return result
9.2.2. Executing Custom JavaScript

The CreJavaScript CommandResultObject allows to specify custom JavaScript which is executed on the client.

import net.datenwerke.rs.terminal.service.terminal.obj.*

def result = new CommandResult() 
  
def script = """
alert("Hello World");
"""

result.addExtension(new CreJavaScript(script))

return result