9.3 Remotely Calling One Script from Another

It is possible to remotely make calls from a client script to a server script and vice-versa. This requires sending a prompt script that causes the recipient to execute the script via a remote proxy. The following example script defines a variable named callback and sends a prompt script to the client. The result is executing this script through a remote proxy.

///////////////////////////////////////////////////////////
// Operation definitions to add to an element: (paste into Operations.ini)
//
// [Enter Trouble Ticket]
// description=Trouble Ticket...
// context=element
// target=dname:root=Elements
// permission=manage
// type=serverscript
// operation=// @debug off \nload( "examples/ticket.fs" );
//

// Set our log category
formula.setLogCategory( "Ticket" )

// Log startup
formula.log.info( "Starting trouble ticket script" )

// Create our object which will be the "callback" from the client
// Note: this is standard javascript syntax for creating an object with named properties/values
var callback =
{
    setTicketInfo: function( reason )
    {
       // We'll log what the user did, to simulate connecting to the ticketing system.
       formula.log.info( "User created trouble ticket:" )
       formula.log.info( "   Element: " + element )
       formula.log.info( "   Reason:  " + reason )

       // Let's notify the user that we did what was asked.
       session.sendMessage( 'Trouble ticket created for ' + element + '. Content:\n\n' + reason )
    },

    cancel: function()
    {
       formula.log.info( "User cancelled trouble ticket creation" )
       session.sendMessage( 'Trouble ticket cancelled for ' + element + '.' )
    }
}

// We're going to send this callback and a prompt script to the client, which will
// then cause execution to this script through remote proxy.

var clientTicketScript = "\
    // @opt -1 \
    // @debug off \
    var result = prompt( 'Enter trouble ticket information for ' + elementName + ':', \
                         'Trouble Ticket' ) \
    if( ! result ) \
        callback.cancel() \
    else \
        callback.setTicketInfo( result )\
"

//
// Now, send the dialog script to the user who invoked this script
//
// Note: this script is sent to variables, called "callback" and "elementName"
//       The callback is wrapped via a remote proxy using the formula.util.makeRemote()
//       function.  The element name is simply the element name of the element
//       the user initiated this ticket for.
//
formula.log.info( "Sending dialog script to client" )
session.invokeScript( 'Enter Trouble Ticket',
                      clientTicketScript,
                      [ formula.util.makeRemote( callback ), element.name ],
                      [ 'callback', 'elementName' ] )
formula.log.info( "Done!" )