Siebel Business Rules Administration Guide > Implementing Rules in Scripts > Scenario for Validating Data Using Script to Invoke Rules >

Creating the Script


This topic is a task in the development process that is listed in Scenario for Validating Data Using Script to Invoke Rules.

The business logic requirements defined in the design process specify that the validation must be performed before the record is written to the database. Based on this requirement, a script is associated with a BusComp_PreWriteRecord () event.

CAUTION:  All examples of script in this document are written for use with the ST eScript engine. To implement the examples in this document, you must first confirm that Siebel Tools is set to use the ST eScript engine.

The ST eScript engine is the default scripting engine. You can use the following procedure to see which engine you are currently set to use, or to switch from the T eScript engine to the ST eScript engine.

NOTE:  If you want to revert back to the T eScript engine after enabling the ST eScript engine and modifying your code to be strongly typed, you need to undo your strongly typed code changes.

To enable the ST eScript engine for Siebel Tools

  1. In Siebel Tools, choose Screens > System Administration > System Preferences.
  2. In the System Preferences window, under System Preferences Name, query for Enable ST Script Engine.
  3. Set the System Preference Value to TRUE.
  4. Recompile your Siebel Repository.

For detailed information about the ST eScript engine, see Using Siebel Tools and Siebel eScript Language Reference.

To implement the Account Validation rules module in an eScript script

  1. Launch Siebel Tools against the sample database to which you deployed the Account Validation rule module.
  2. In the Object Explorer, select Business Component.
  3. Select and lock the Account business component.
  4. Right-click the Account business component record and choose Edit Server Scripts, then choose eScript as your scripting language.
  5. Select BusComp_PreWriteRecord and add the following eScript code to that function.

    function BusComp_PreWriteRecord ()
    {
       //Declare the variable that contains the Business Rule Service business service.
       var svc:Service = TheApplication().GetService("Business Rule Service");

       //Declare the inputs property set variable for Business Rule Service. Declare
       //child and grandchild property sets that will eventually be used to create
       //the hierarchical structure of the BusCompList input property.
       var inputs:PropertySet = TheApplication().NewPropertySet();
       var child:PropertySet = TheApplication().NewPropertySet();
       var grandchild:PropertySet = TheApplication().NewPropertySet();

       //Declare the outputs property set variable for Business Rule Service
       var outputs:PropertySet = TheApplication().NewPropertySet();

       // Fill in data in the input propertyset
       // <PropertySet RuleModuleName="Account Validation"
       // GetMoreData="N"
       // PerformAction="N"
       // <BusCompList>
       // <BusComp Id="this buscomp's id" Name="Account">
       inputs.SetProperty("RuleModuleName", "Account Validation");
       inputs.SetProperty("PerformAction", "N");
       inputs.SetProperty("GetMoreData", "N");
       child.SetType("BusCompList");

       //Note that this script is associated with PreWriteRecord for the Account business
       //component, so "this" is the Account business component.
       grandchild.SetType("BusComp");
       grandchild.SetProperty("Id", this.GetFieldValue("Id"));
       grandchild.SetProperty("Name", this.Name());
       
       // Add fields for Name and Location
       var nameField:PropertySet = TheApplication().NewPropertySet();
       var locationField:PropertySet = TheApplication().NewPropertySet();
       nameField.SetType("Field");
       nameField.SetValue(this.GetFieldValue("Name"));
       nameField.SetProperty("Name", "Name");
       nameField.SetProperty("Type", "DTYPE_TEXT");
       //Add the Name field as a child of the Account business component.
       grandchild.AddChild(nameField);
       locationField.SetType("Field");
       locationField.SetValue(this.GetFieldValue("Location"));
       locationField.SetProperty("Name", "Location");
       locationField.SetProperty("Type", "DTYPE_TEXT");
       //Add the Location field as a child of the Account business component
       grandchild.AddChild(locationField);
       
       //Add the Account business component as a child of the BusCompList property.
       child.AddChild(grandchild);
       //Finally, add the BusCompList property (now a complete hierarchy) as a child of
       //the inputs property set.
       inputs.AddChild(child);
             
       // invoke Business Rule Service
       svc.InvokeMethod("RunRules", inputs, outputs);
             
       // Examine the outputs propertyset which should contain the following
       var wsshell = COMCreateObject("WScript.Shell");
             
       var nChild = outputs.GetChildCount();
       var i = 0;
       var resultType;
       var errorText;
       
       // If no result, then the validation passed in this example.
       if (nChild == 0)
       {
          wsshell.Popup("Account Validation Succeeded!");
          return (ContinueOperation);
       }
       
       // Parse and process the output property set. In this example, you only look for
       // RaiseErrorText and ValidationList because you only used those actions in the
       // Account Validation rule module.
       for (i = 0; i < nChild; i++)
       {
          resultType = outputs.GetChild(i).GetType();
          
          // Process RaiseErrorText result
          if (resultType == "RaiseErrorText")
          {
             errorText = outputs.GetChild(i).GetValue();
             wsshell.Popup(errorText);
             return(CancelOperation);
          }
          
          // Process ValidationList result
          if (resultType == "ValidationList")
          {
             wsshell.Popup("Results include ValidationList back!");
             var j = 0;
             var msg:PropertySet = TheApplication().NewPropertySet();
             var valList:PropertySet = outputs.GetChild(i);
             var valCount = valList.GetChildCount();
             var valResult;
             
             // Look at each Validation result
             for (j = 0; j < valCount; j++)
             {
                valResult = valList.GetChild(j).GetProperty("Result");
                if (valResult != "Valid")
                {
                   var val:PropertySet = valList.GetChild(j);
                   var msgCount = val.GetChildCount();
                   var k = 0;
                   for (k = 0; k < msgCount; k++)
                   {
                      wsshell.Popup(val.GetChild(k).GetValue());
                   }
                   return (CancelOperation);
                }
             }
          }
       }
       
       return (ContinueOperation);
    }

  6. Save the script.
  7. Click the Check Syntax button. Make edits as necessary.
  8. Right-click the Account business component and select Compile Selected Objects.
  9. Restart the Siebel application (the developer client).
Siebel Business Rules Administration Guide Copyright © 2007, Oracle. All rights reserved.