05 February 2011

Siebel: Prototype in eScript - Part 3

In earlier posts, we have seen using prototypes to extend OOB objects. In other posts we have also dealt with user defined objects and scripts libraries. Time for seeing all three in action you say? And I hear you, we shall see how user defined object can be prototyped, and used across the application.
Problem: I need a function to lookup the description of a LOV provided its type and value
Solution: We address the problem in two steps:
  1. Define a user object and make sure it is available for the session. One of the common ways of doing this is in Application object
  2. Use the custom object to define a prototype for useful function(s) - in our case 'LookupDesc'
 Now, the how..
  • Define a variable in the application script, for example: Siebel Life Sciences
[Application: Siebel Life Sciences, (declarations)]
var MyApp;
And, call a business service where MyApp is initialized. This is just to keep the application object, it works just fine within the application itself.
[Application: Siebel Life Sciences, Method: Application_Start]
function Application_Start (CommandLine)
{
 var bsStarter = TheApplication().GetService("COG Test Proto");
 bsStarter.Init();
}

  • Write the custom business service 'COG Test Proto'
[Service: COG Test Proto, Method: Init]
 function Init ()
 {
    TheApplication().MyApp = new MyAppFun;
    MyAppFun.prototype.LookupDesc = LookupDescFun;
 }

[Service: COG Test Proto, Method: MyAppFun]

       function MyAppFun()
       {
 // this is a dummy, but funny function.
       }


  [Service: COG Test Proto, Method: MyAppFun]
function LookupDescFun (Type, Value)
 {
 try {
  var sDesc = "";
  var boLOV = TheApplication().GetBusObject("List Of Values");
  var bcLOV = boLOV.GetBusComp("List Of Values");
  with (bcLOV) {
     ClearToQuery();
     SetViewMode(AllView);
     ActivateField("Description");
     SetSearchExpr("[Type]='" + Type + "' AND [Value] = '" + Value + "'");
     ExecuteQuery(ForwardOnly);
     if (FirstRecord()) sDesc = GetFieldValue("Description");
  } //with
  return(sDesc);
 }
 catch(e) {
  throw(e);
 }
 finally {
  bcLOV = null;
  boLOV = null;
 }
 }


 That's about it, now MyApp is ready to have some fun. Again, a quick service to test how things work.

[Service: COG Test Proto Call, Method: Service_PreInvokeMethod]
 function Service_PreInvokeMethod (MethodName, Inputs, Outputs)
 {
    Outputs.SetProperty("out", TheApplication().MyApp.LookupDesc("blah", "blah"));
 }

A bit of theory on how it works:
  1. MyApp is defined as a variable in Application, the same is initialized in a business service. It does not matter what the function [MyAppFun] contains, we will not be using that anyway
  2. The prototype LookupDesc is defined as an extension of MyAppFun. Any object based on MyAppFun will have access to the LookupDesc functionality
  3. We invoke LookupDesc as an extension of the variable in the application object. There are other blogs out there who just use TheApplication = MyApp, but that practice is not encouraged as I understand
  4. When TheApplication().MyApp.LookupDesc is invoked, Siebel will look for the LookupDesc function for MyApp object. Since the functionality does not exist OOB, the prototype is consulted and in turn LookupDescFun function is executed.

If you are thinking this is an overkill, you may be right if this is all you want to do. Keep in mind that this coding practice will be more scalable, and ensures reusability.

1 comment:

Anonymous said...

Hi But will this really work with HLOV/MLOV and Organization-based LOVs - is there any other posibility to extend the original LookupValue to retrieve Description (or any other existing/Custom field) from LOV record where was taken translation?

Post a Comment