Siebel eScript Language Reference > About Siebel eScript >

About Functions and Methods


A function is an independent section of code that does the following:

  1. Receives information
  2. Performs some action on this information
  3. Returns a value to the item that called it

It begins with the following statement:

Function functionname

It ends with the following statement:

End Function

You use the same format as you use with a variable to name a custom function. You can write code that uses any valid variable name as a function name. It is recommended that you use a name that describes the work that the function performs.

You can write code that calls a function repeatedly from various objects or script. It is similar to a subroutine. To call a function, you must know what information the function requires as input and what information it provides as output. This book describes the predefined functions that come with Siebel eScript. You can write code that uses these functions any time you use the Siebel eScript interpreter.

You can write code that uses a function anywhere that you can use a variable. To use a function, you do the following:

  • To declare it, you use the function keyword.
  • To determine the data that Siebel eScript must pass to the function, you include the function operator. To include this operator, you use a pair of parentheses immediately after the function name. For example:

    TheApplication.RaiseErrorText()

A Siebel VB method is a function that is part of an object or class. It can include a predefined procedure that you can use to call a function that does an action or a service for an object.

A Siebel VB statement is a complete instruction.

For more information, see Passing a Value to a Function.

Example of a Function

The TheApplication.RaiseErrorText function is an example of a function that allows you to display formatted text if an error occurs. It does the following work:

  • Receives a string from the function that calls it
  • Displays this string in an alert box in the Siebel client
  • Stops the script

About Function Scope

A function is global. Siebel eScript can call it from anywhere in a script in the object where you declare it. The examples in this topic achieve the following results:

  • The code passes the 3 and 4 literals as parameters to the SumTwo function.
  • The SumTwo function includes corresponding a and b variables. These variables contain the literal values that Siebel eScript passes to the function.
Example of Calling a Function as a Function

The following example calls the SumTwo function:

function SumTwo(a, b)
{
   return (a + b)
}

TheApplication().RaiseErrorText(SumTwo(3, 4));

Example of Using a Method to Call a Function

The following example uses a method of the global object to call the SumTwo function:

function SumTwo(a, b)
{
   return (a + b)
}

TheApplication().RaiseErrorText(global.SumTwo(3, 4));

The Arguments Property of a Function

The arguments property of a function is a list of the arguments that Siebel eScript passes to the function. The first argument is arguments[0], the second argument is arguments[1], and so on. You can write code that references the arguments property for a function only in that same function.

You can configure a function that includes an indefinite number of arguments. The following example uses a variable number of arguments, and then returns the sum:

function SumAll()
{
   var total = 0;
   for (var ssk = 0; ssk < arguments.length; ssk++)


{
      total += arguments[ssk];
}
   return total;
}

About Recursive Functions

A recursive function is a type of function that calls itself or that calls another function that calls the first function. Siebel eScript allows recursion. Each call to a function is independent of any other call to this function. If a function calls itself too many times, then the script runs out of memory and aborts.

In the following example, the factor function calls itself so that it can factor a number. Factoring is an appropriate use of recursion because it is a repetitive process where the result of one factor uses the same rules to factor the next result:

function factor(i) //recursive function to print factors of i,
{// and return the number of factors in i
   if ( 2 <= i )
{
      for ( var test = 2; test <= i; test++ )
{
         if ( 0 == (i % test) )
{
// found a factor, so print this factor then call
// factor() recursively to find the next factor
         return( 1 + factor(i/test) );
}
}
}
// if this point was reached, then factor not found
   return( 0 );
}

Error Checking with Functions

If a function fails, then some functions return a special value. Consider the following example:

  • To allow a script to read from or write to a file, the Clib.fopen method opens or creates this file.
  • If the script calls the Clib.fopen method but this method cannot open a file, then the method returns null.
  • If the script then attempts to read from or write to this file, then Siebel CRM creates an error.

To prevent this error, you can use the following code to determine if Clib.fopen returns null when it attempts to open a file. If it does return null, then you can write code that aborts the script and displays an error message:

var fp = Clib.fopen("myfile.txt", "r");
var fp = Clib.fopen("myfile.txt", "r");
if (null == fp)//make sure null is not returned
{
   TheApplication().RaiseErrorText("Error with fopen as returned null " +
   "in the following object: " + this.Name() + " " + e.toString() + e.errText());
}

For more information, see Overview of the Clib Object.

Where Data Resides

Data in a script resides in a literal or in a variable. The following example includes variables and literals:

var TestVar = 14;
var aString = "test string";

This code does the following:

  • Saves the following literal value to the TestVar variable:

    14

  • Saves the following literal value to the aString variable:

    test string

After you save a literal value in a variable, you can reference this variable anywhere in the script where you declare the variable.

Siebel eScript Language Reference Copyright © 2013, Oracle and/or its affiliates. All rights reserved. Legal Notices.