In this blog post we will be leveraging power of computed methods and properties. We will go over a use case using a simple example method to calculate a product score comparing competitor products for a company.
Our main focus will be the user input from two properties: key relevance and market alignment. According to the user manual inputs, we will calculate a dynamic D-Score value signifying whether our product requires more attention in comparison to other products in the market.
Before we dive into the method code, lets create our CMF structure. Note that our last blog post on CMF goes over creating the container ItemType and creating the structure if you are not familiar with this process.
1. We will create the following structure (shown below) with Key_Diff as the root element type and Prod_Values as an element type branching off Key_Diff.
2. Leave out the DScore property type for last.
3. Now we can add the DScore property type by first going to the TOC > Administration > Methods.
5. Go back to the CMF structure and at the root node (Key_Diff) add another property type:
- Data Type: Float
- Computed Method: Compute D-Score
- Dependent Properties: Relevance and Market Alignment
6. Make sure to add the in the views as well with the same names and mapped values all defined as property default.
Before we get into the explanation of the function code, we should go over the CMF API briefly. CMF provides us a number of functions we can call to access data in the data structure:
This function is called to get all the nodes under a root element type giving us access to any of the associated element types, property types, element bindings, and document elements. We can use this paired with the other two functions to traverse to either property/document elements or property types within the return values of this array.
Using the ancestor function the descendant function allows us to loop through any child element type or element types associated.
The getPropertyItems() can be used grab the property values on the element type or child element type.
This function allows user to set a new value to a property item in a cell using its ID.
Similar to markToUpdateValue(), one can set a style using the update style function through the ID as well.
Examining the Method Code
Our goal is to take the user input relevance score and divide that by the highest market alignment score then return this value.
1. In the first line we are grabbing the arguments array of the dependent properties we setup in the CMF structure according to the context of the root node. This means if we are creating a new Product Analysis table if we first add a Key Differentiator then it would be in context of the Relevance.
2. Using getElement() we can get access to the CMF API functions and we can call findAncestorElement() to grab the top level node of the structure.
3. Since DScore and Key Relevance are property types under the root node we can grab those values using getPropertyItem().
4. We also do some error checking to end the method if this is a new document and the user has not inputted these values yet.
5. Continuing on we use findDescendantElements() to get an array of the property types with the product names and market alignments.
6. In order to get the user inputted market alignment values we loop through the array and use getPropertyItem().
7. We then check for the highest value in the array and use that to calculate our Dscore result.
9. Finally wrapping up our last call is markToUpdateValue() that takes in the id of the cell that it is updating and sets it to the new value.
Here we can see our table in action. When we add in our computed values, the relevance, and market alignments on compared products we can see the D-Score value dynamically changing. This is great for the purposes of our use case to distinguish whether our product stands up with the competition.