Siebel eScript Language Reference > Using Siebel eScript > Coding with Siebel eScript >

Declaring and Using Variables


A variable is an object that stores and represents information in a script. Siebel eScript can modify the value of a variable but it cannot modify the value of a literal. For example, to display a name literally, you must use the following code multiple times:

TheApplication().RaiseErrorText("Aloysius Gloucestershire Merkowitzky");

To simplify this code, the following code uses a variable:

var Name = "Aloysius Gloucestershire Merkowitzy";
TheApplication().RaiseErrorText(Name);

The value of the Name variable changes, which allows you to use shorter lines of code and to reuse the same lines of code.

About Local and Global Variables

Siebel eScript includes the following types of variables:

  • Local. A variable that you declare in a function. You can write code that references a local variable only in the function where you declare the variable.
  • Global. A variable that you declare in one of the following ways:
    • Declare the variable outside of a function.
    • Declare the variable in the general declarations section of the application object.

      You can write code that references or modify a global variable from the following items:

    • Any function that is associated with the Siebel object for which you declare the variable.
    • Any object in a Siebel application where you declare the variable.
    • Another Siebel application.
    • If you declare a global variable outside of a function, then you can reference it from any object that resides in the Siebel application where you declare this variable. For more information, see Declaring a Global Variable Outside of a Function.

If you declare a local variable that uses the same name as a global variable, then you cannot reference this global variable from the function where you declare this local variable.

Siebel VB includes a Global statement. You cannot use this statement in Siebel eScript.

Declaring a Global Variable Outside of a Function

You can write code that declares a variable in a location other than in the declaration section. For example:

var global1 = 6;

function ABC()

{

global1 = 8;

global2 = 6;

}

var global2 = 8;

Using a Local Variable Is Preferable to Using a Global Variable

It is recommended that you use a local variable where possible instead of a global variable for the following reasons:

  • A local variable helps you create modular code that is easier to debug and modify.
  • A local variable requires fewer resources.
  • It is easier for other developers to understand how you use a local variable in a single function than it is to understand how you use a global variable across an entire Siebel application.
  • If a subsequent development team encounters an object that you script with a global variable, then this team might not understand the use of the global variable. If the team uses this variable, then the team might introduce defects.
  • The scope of a global variable is too large to meet the business requirement and often results in a variable whose lifecycle is not clear.

Instead of using a global variable, it is recommended that you configure Siebel CRM to pass an object as a parameter to a function so that you can control the scope of the parameter. If you are considering using a global variable, then you must consider this usage carefully. If you use a global variable, then do so only rarely and document it thoroughly.

Example of Declaring Local and Global Variables

The following example includes local and global variables:

var globalVariable = 1;
function Function1()
{
   var localVariable1 = 1;
   var localVariable2 = 3;
   Function2(d);
}

function Function2(e)
{
   var localVariable3 = 2
   ...
}

This example illustrates the following concepts:

  • The globalVariable variable is global to the object where you declare it because it is declared outside of a function. Typically you declare all global variables in a general declarations section.
  • To create a local variable, you declare it in a function. The following variables are local because this example declares them in a function:
    • localVariable1
    • localVariable2
    • localVariable3
  • This example cannot use localVariable3 in Function1 because it is not defined in this function.
  • This example uses the d variable in Function1. It uses the e parameter to pass the d variable to Function2.

The following code includes variables that are available to Function1 and Function2:

Function1(): globalVariable, localVariable1, localVariable2
Function2(): globalVariable, localVariable3, e

Declaring a Variable

This topic describes how to declare a variable.

To declare a variable

  • Use the var keyword.

    For example:

    var perfectNumber;

You can write code that saves a value in a variable when you declare it. For example:

var perfectNumber = 28;

Declaring a Variable In a Statement Block

If you declare a variable in a statement block in a method, then you can reference this variable anywhere in the method, including from a statement block that resides in the method where you did not declare the variable.

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