02 February 2011

Siebel: Prototype in eScript - Part 2

The last example in prototypes dealt with extending a OOB data type, but that extension was done within the business service itself. Although the prototype can be defined only once and used multiple times, it is not really demonstrated well since the entire thing is handled within the same code. Here, let me take an opportunity to show how the prototype definition itself can be handled elsewhere.

Problem
Check whether the given string is a valid email address

Solution
Define the prototype to extend the OOB string object, and use it as many times as needed. To achieve this we define the prototype in the Application object.

Go to "Server Scripts" of 'Siebel Life Sciences' or similar, and define prototype.

(declarations)

String.prototype.IsEmail = CheckValidEmail;

Add a function 'CheckValidEmail' to the same application object.

function CheckValidEmail()

{

var rEmailPat = /^\w+[\w-\.]*\@\w+((-\w+)(\w*))\.[a-z]{2,3}$/;

return rEmailPat.test(this);

}

That's about it! Any string can now be tested for a valid email. We will quickly build a test function to demonstrate how this is used:

Service: COG Test Email

Method: Service_PreInvokeMethod

function Service_PreInvokeMethod (MethodName, Inputs, Outputs)

{

var sEmail = Inputs.GetProperty("Email");

Outputs.SetProperty("Valid", sEmail.IsEmail());

return (CancelOperation);

}

When you run this in the simulator, this should either return a 'true' or 'false' value depending on whether the supplied string is a valid email address. Now, a bit about how it works.

  1. First we define the prototype for the data type string with the statement String.prototype.IsEmail = CheckValidEmail'. This statement is executed when the application starts up.
  2. The above prototype points to a custom function 'CheckValidEmail'. This function uses a simple regular expression to test the validity of the entered string. It returns a 'true' when the string has the specified regex pattern, false otherwise
  3. None of the above has any effect until we actually invoke the 'IsEmail' function. When this function is used against the string object, Siebel checks the prototype since no such function exists OOB
  4. Return value from the function denotes whether the string 'sEmail' is a valid email address

Watch this space for one more example!

No comments:

Post a Comment