Create an Extension Rule
In this example, you define an extension rule that calculates the effect of applying different discounts to the list price of an item, in order to arrive at the sale price.
The formula used in this example to calculate the discount is simple, but you can use this technique to construct more complex extension rule functionality, through the script in your rule text.
The tasks in this example are:
-
Define supplemental features for list price, discount, and sale price.
-
Create an extension rule, with the discount feature node as the base node.
-
Write a Groovy script that defines a discounting function and applies it to the supplemental features.
-
Define event and argument bindings for the function.
-
Test the rule.
Defining Supplemental Features
The supplemental features are used for the end user inputs for list price and discount, and the calculated output of the sale price.
-
On the Overview page of the Configurator Models work area, select Create from the Actions menu, to create a new workspace. Set the Effective Start Date to tomorrow's date.
-
Open the workspace. On the Workspace page, select Select and Add Models from the Actions menu.
-
On the Select and Add: Models page, search for a model, select it, then click OK, to add it to the workspace. Ignore any warning about drafts in other workspaces.
-
On the Workspace page, click the model's name to open it for editing.
-
On the Edit Configurator Model page, select the root node of the model, and select Create Decimal Feature from the Actions menu.
-
Create the following decimal features, as shown in these tables.
Field
Value
Name
List Price
Minimum
20,000
Maximum
60.000
Field
Value
Name
Discount
Minimum
0
Maximum
10
Field
Value
Name
Sale Price
Minimum
0
Maximum
100.000
-
Click Save.
Creating the Extension Rule
The extension rule will apply the discount to the list price, and put the result in the sale price.
-
On the Rules tab of the Edit Configurator Model page, select Create Extension Rule from the Rules toolbar.
-
Enter a Name for the extension rule, such as Apply Discount.
-
In the Structure palette, expand the model tree, select the node Discount, then right-click and select Set as Base Node from the context menu.
-
Click Save.
Writing the Rule Text
The behavior of the extension rule is defined in the script entered in the Rule Text field, which is written in the Groovy scripting language.
-
In the Rule Text field, enter the following script.
// Import the needed Configurator interfaces import oracle.apps.scm.configurator.runtime.core.IConfiguration import oracle.apps.scm.configurator.runtime.core.IBomModelInstance import oracle.apps.scm.configurator.runtime.core.IDecimalFeature // Define the discounting function. def applyDiscount ( p_listPrice, p_discount ) { // Get values of nodes from arguments. double listPrice = ((IDecimalFeature)p_listPrice).getValue() double discount = ((IDecimalFeature)p_discount).getValue() double salePrice = 0 // Calculate the price. salePrice = listPrice - ( listPrice * ( discount / 100 ) ) // Get the node whose value will be set. IConfiguration config = cxEvent.getConfiguration() IBomModelInstance root = config.getRootBomModel() IDecimalFeature salePriceNode = root.getChildByName("Sale Price") // Set the value. ((IDecimalFeature)salePriceNode).setDecimalValue(salePrice) }
-
Click Save.
-
Click Validate.
-
You should receive the error message
The rule is invalid. The extension rule must have at least one event binding defined.
Defining the Event Binding
To make the script execute, you must bind it to a configurator event. So now you add an event binding to the rule.
-
In the Event Bindings table, click Create
-
In the new row for the event binding, from the Event list, select
postValueChange
. -
From the Event Scope list, select
Base node
. -
From the Class list, select
ScriptClass
. -
From the Method list, select
applyDiscount
. The argumentsp_listPrice
andp_discount
are displayed with the function name. -
Click Save.
Defining the Argument Bindings
When you selected the applyDiscount
method in the Event Bindings table, the Argument
Bindings table should have automatically appeared, populated with
a row for each argument
-
In the Argument Bindings table, select the row for the argument named
p_listPrice
. -
From the Specification column for that row, select
Model node
. -
Expand the model tree in the Structure pane, and select the node
List Price
, then right-click the node and select Set as Argument Value. -
Click Validate.
-
You should receive the error message
The rule is invalid. The node referenced in the argument p_discount bound to the event postValueChange was not found.
-
Repeat the preceding argument binding steps for the argument
p_discount
and the nodeDiscount
. -
Click Validate.
-
You should receive the information message
No errors were detected.
-
Click Save and Compile.
-
You should receive the confirmation message
Model compilation has completed without errors.
.
Testing the Model
Test the model to verify the functionality of the extension rule.
-
On the top of the Edit Configurator Model page, select Test Model.
-
In the Test Model dialog box, ensure that User Interface is set to
Default
, then click OK. -
On the Test Model page, fields for the three decimal features that you added should appear, in the sequence that you created them:
-
List Price
-
Discount
-
Sale Price
-
-
In the List Price field, enter the value 30,000. Notice that you can only enter values between the minimum and maximum that you defined when you created the decimal feature.
-
In the Discount field, enter the value 5. Notice again that the allowable values are between the minimum and maximum that you defined.
-
When you press Enter in the Discount field, or tab out of it, notice that the value of the Sale Price field changes, from empty to 28500.
-
Enter a different value in the Discount field, and notice the changes in the Sale price field. This change happens because you bound the execution of the extension rule to changes in the value of Discount.
-
Enter a different value in the List Price field, and notice that there is no change in the Sale price, because there is no binding in the rule to changes in the value of List Price.
-
Click Finish to end the test session.