Showing posts with label Dynamic Script. Show all posts
Showing posts with label Dynamic Script. Show all posts

Thursday, February 6, 2014

Dynami Script Examples

Example 1: Debugging code by printing the value.
Print(node.Abbrev); // Will print out value of the current node. Handy for debugging.

Example 2: Extracting part of node name. Similar to ArrayItem() Formula.
return node.Abbrev.split("_").slice(0,1); // Returns first part of the array.
return node.Abbrev.split("_").slice(2,3); // returns 3rd part of the array.
OR
return node.Abbrev.split("_")[0]; // Returns first part of the array.
return node.Abbrev.split("_")[2]; // returns 3rd part of the array.


Example 3: Finding when was a hierarchy last updated by comparing all the nodes of the hierarchies (starting from top node) last update date.
var dt;
var childEnumerator = node.Hier.TopNode.GetChildEnumerator();
while(childEnumerator.MoveNext()) {
    var propValue = childEnumerator.GetCurrent().PropValue("Core.NodeLastChangedOn");
    if(dt == null || dt<propValue)
        dt=propValue;
}
return dt;

Example 4: validating description should not be empty and should be less than 10 characters. This helps users understand exactly why the validation failed.
var errm;
var dsr = node.PropValue("Core.Descr");
if (!dsr)
    errm="Description cannot be empty";
if(dsr.length >10)
    errm="Description should be less than 10 characters.";
if(!errm)
    return true;
else
    return{
        success:false,
        parameters:[errm]

    }
Dynamic error message

Example 5:This formula tries to find list of all the distinct property values for all the children of a parent node. it helps to identify nodes that have different children with different property values.

var cprt;
var childEnumerator = node.GetChildEnumerator();
while(childEnumerator.MoveNext()) {
    var propValue = childEnumerator.GetCurrent().PropValue("Custom.CPRTSchedule");
       if(cprt == null)
           cprt=propValue;
   
var re=new RegExp(propValue);
    if( re.test(cprt)==false)
         cprt=cprt+","+propValue;
   
}
if(!cprt)
    return(node.PropValue("Custom.CPRTSchedule"));

return cprt;






Dynamic Scripting

Oracle introduces Dynamic Scripting in DRM 11.1.2.3. This functionality makes drm much more powerful and flexible in deriving properties and validating business rules.

What is dynamic scripting?
Dynamic script is Java Script programming in DRM. This feature is introduced in DRM 11.1.2.3 version. It enables you to develop business logic for derived properties and validations.

Why do we need it?
Dynamic scripts provide
1.    Robust and better performing alternative to formulas.
2.    Better organization and less complexity of logic through the use of
a.    Multiple statements
b.    Variables
c.    In-line comments.
d.    Loops & regular expressions.

Where can we use it?
Dynamic scripts can be used in one of two ways
  • Derived Properties
  • Validations


Derived Properties: We can now use script instead of formula to derive property values. Bonus addition in this 11.1.2.3 is deriving Hierarchy and Version level properties.
Script can derive property values at node (Global, Local), Hierarchy and Version level.

Validations: Script and formula are now available in validations.We dont have to create properties and reference them in validations, instead we can directly use the formulas or script within validation.

How should we use?
DRM provides parameters (node, hierarchy, version, validation etc.). All applicable properties and methods can be referenced using these parameters. Check the example in later sections.

Pros and Cons
Pros
Cons
Programming Capabilities
Need to know/learn java scripting
Variables can be used to replace some properties

Customize error messages in validations.

Can use one validation to perform multiple validations.

Performance improvement and easy debugging