With Statement

The use of the with statement is discouraged. eScript follows the ECMAScript specification and the writers of the specification have these two statements to make about using the with statement in script.

  1. It is potentially confusing.

    Warning: Use of the with statement is not recommended, as it may be the source of confusing bugs and compatibility issues.
  2. It is not recommended.

    Use of the Legacy "with" statement is discouraged in new ECMAScript code. Consider alternatives.

This is an example of how you could encounter logic issues by using the with statement in Siebel eScript:

You have a Business Component with the method "myMethod". You also have a Business Service with the method "myMethod". In the Business Component script, you have a with statement block using the Business Service as its argument. In that with statement block you call the method myMethod on the Business Service. You intend to call myMethod on the Business Service, but the enclosing Business Component's myMethod will be called instead. This is unexpected but is how the eScript engine works.

Example script in the Account BC where the method "myMethod" exists:

var lBS_service = TheApplication().GetService("MyService");

with(lBS_service)
{
 myMethod();
}//end with

Result: myMethod is called on the Account Business Component, NOT the intended Business Service.

Recommendation: Avoid using the with statement in eScript. It is potentially confusing and may produce logic errors at runtime when the wrong object is referenced.

The With statement associates a default object with a statement block. It only applies to the code that resides in the statement block where the With statement occurs, regardless of how Siebel eScript enters or exits the statement block. If Siebel eScript exits a With statement, then the With statement no longer applies.

You cannot write code that uses a Go to statement or a label to enter or exit the middle of a statement block that resides in a With statement.

Format

As shown in the following format, you can specify both methods and variables within the With statement.

with (object)
{
method1;
method2;
methodn;
.
.
variable1;
variable2;
variablen;
}

The following table describes the arguments of the With statement.

Argument Description

object

An object where you must use multiple methods.

method1, method2, methodn

Methods that Siebel eScript runs with the object. The With statement prefixes each method with the object name and a period.

variable1, variable2, variablen

Variables that you define for the With statement.

Example

In the Opportunity business component, in the declarations section, define a variable as follows:

var myVariable = "HELLO"; 

The following example code includes a With statement that specifies both methods and variables.

var bcOppty;
var boBusObj;
boBusObj = TheApplication().GetBusObject("Opportunity");
bcOppty = boBusObj.GetBusComp("Opportunity");

// The following line defines srowid as a specific ROW_ID we are 
//looking for.
var srowid = "1-ABC";
// The following line sets the variable defined in this function to
// "GOODBYE".
var myVariable = "GOODBYE";

try
{
   with (bcOppty)
   {
      SetViewMode(SalesRepView);
      ActivateField("Sales Stage");
      SetSearchSpec("Id", srowid);
      ExecuteQuery(ForwardOnly);

// The following line outputs "HELLO" because myVariable refers to the
// variable defined on the Opportunity BusComp.
      TheApplication().Trace("myVariable is " + myVariable);

// The following line changes the variable defined on Opportunity to
// "SEE YOU!" It does not affect the instance of the variable defined
// in this function.
      myVariable = "SEE YOU!"
}
   }

// The following line outputs "GOODBYE" because myVariable refers to the variable
// defined in this function, which was not touched in the "with" block.
TheApplication().Trace("myVariable is " + myVariable); 

finally
{
   bcOppty = null;
   boBusObj = null;
}

The code in the With statement block is equivalent to the following code:

bcOppty.SetViewMode(SalesRepView);
bcOppty.ActivateField("Sales Stage");
bcOppty.SetSearchSpec("Id", srowid);
bcOppty.ExecuteQuery(ForwardOnly);