Skip to main content
  1. Posts/

Prototype in Siebel eScript - Part 2

·2 mins

The last example  in prototypes dealt with extending a OOB data type, but that extension was done within the business service itself. Prototype in Siebel eScript can be defined only once and used multiple times, this is better demonstrated with the example below.

Problem #

Validate 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 the application that you use), and define prototype.

(declarations) #

String.prototype.IsEmail = CheckValidEmail;
```


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

```js

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 quickly enough. We will build a test function to demonstrate how this is used:

**Service:** COG Test Email | <strong style="font-size: 15px;">Method:</strong> Service_PreInvokeMethod

```js

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