Functions

You can write your own workflow script functions using the function keyword. Use parentheses to specify parameters for the custom function, and a block statement to specify the operations that the function should perform. To execute the function, use the name you assigned to it followed by the function call operator.

To try the sample below, you'll need the canIHasFunctions.fls.xml trigger webform.

server program iCanHasFunctions for form canIHasFunctions
{
  var hello = function()
  {
    trace "Hello World!";
  };
  hello();
...
}

"Hello World!"

Use the var keyword inside the parentheses after the function keyword to set up the required parameters for your function when you define it. When you call the function, use the function call operator to pass the required number of parameters to your function in the order specified in the function statement.

server program iCanHasFunctions for form canIHasFunctions
{
...
  var greeterBot = function(var firstname, var lastname)
  {
    trace "Why hello " + firstname + " " + lastname + ", it's so nice to see you!";
  };
  greeterBot("Petar", "Hoyt");
...
}
"Why hello Petar Hoyt, it's so nice to see you!"

Figure 318. The console output of the sample function above


You can also set up a function as a member of an object and use the dot operator to access it.

server program iCanHasFunctions for form canIHasFunctions
{
  var person =
  {
    greet: function()
    {
      trace "Hello, there.";
    }
  };
  person.greet();
...
}
"Hello there."

Figure 319. The console output of the sample function above


When setting up a function member for an object, use the '$this' keyword to refer to the object on which the function is called.

server program iCanHasFunctions for form canIHasFunctions
{
  var jedi =
  {
    name: "Luke",
    weapon:
    {
      name: "lightsaber",
      color: "green",
      sound: "vvvvvvuuwwwwooommmm"
    },
    age: 21,
    fight: function()
    {
      trace "I'm " +
      '$this'.name +
      " and I'm wielding my " +
      '$this'.weapon.color + " " +
      '$this'.weapon.name +"! " +
      '$this'.weapon.sound + " !!!";
    }
  };
  jedi.fight();
...
}
"I'm Luke and I'm wielding my green lightsaber! vvvvvvuuwwwwooommmm !!!"

Figure 320. The console output of the sample function above