Controlling pricelist visibility using custom permissions.
Default Context Rules can only reach a few limited number of objects (Account, Contract, Quote, Opportunity, Order and Asset) and do simple logic, ex: check if the customer account BillingCountry equals “Portugal”.
To do more complex logic or reach other objects it’s necessary to use apex.
Use an Apex Class to check if the current user has the right permission to see the price list.
A Custom Permission and a Permission Set
An apex class
A Vlocity_cmt__FunctionDefinition__mdt metadata entry (This tells the Vlocity Functions how to reach the class and what methods to call)
A Vlocity Function (this will declare the class and method making them accessible to other Vlocity components)
A context scope and context dimension (These components will run the Vlocity function and contain the result)
A Rule and a Rule Set
1. Create the Custom Permission with the API name B2CPriceListAccess.
2. Create the Permission Set.
A. Create the Permission Set with the API name B2CPriceListAccess.
B. Add the Custom Permission B2CPriceListAccess.
3. Create the apex class VL_ContextRuleFunctions:
A. The inputs ('arguments') and outputs ('result') names must be respected.
global with sharing class VL_ContextRuleFunctions implements vlocity_cmt.VlocityOpenInterface2 {
public final static string METHOD_HAS_CUSTOM_PERMISSION = 'HasCustomPermission';
public final static string KEY_RESULT = 'result';
public final static string KEY_ARGUMENTS = 'arguments'; //this is set in the input map by Vlocity
/**
* @author Jorge Antunes
* @description The main entry point for Vlocity
**/
global Boolean invokeMethod(String methodName, Map<String, Object> inputMap, Map<String, Object> outputMap, Map<String, Object> options) {
Boolean result = true;
try {
if (methodName == METHOD_HAS_CUSTOM_PERMISSION) {
outputMap.put(KEY_RESULT, HasCustomPermission(inputMap));
}
} catch (Exception e) {
result = false;
}
return result;
}
/**
* @author Jorge Antunes
* @description Check if the current user has the requested permission
**/
private Boolean HasCustomPermission(Map<String, Object> inputMap) {
List<Object> arguments = (List<Object>) inputMap.get(KEY_ARGUMENTS);
Boolean result = false;
if(arguments != null && arguments.isEmpty()) {
result = FeatureManagement.checkPermission((String)arguments[0]);
}
return result;
}
}
4. Create a new entry in the vlocity_cmt__FunctionDefinition__mdt metadata:
Field |
Value |
Notes |
Label |
HasCustomPermission |
Suggestion: name of the apex method being called |
Function Definition Name |
HasCustomPermission |
Suggestion: name of the apex method being called |
ClassName |
VL_ContextRuleFunctions |
Name of the apex class being called |
Method Name |
HasCustomPermission |
Name of the apex method being called |
5. Create a Function.
A. In the Vlocity Product Console in the Rules section, click the plus icon ( + ) next to Function to create a new function.
B. In the New Function dialog, enter the following information:
Field |
Value |
Notes |
Name |
B2C Access |
|
Code |
RULE_B2CACCESS |
The Rule Code may be used within custom expressions and therefore should be unique and should not contain any spaces. |
Description |
Check if the current user should be given access to the B2C Price List |
Describes the purpose of the context rule. |
Expression Mode |
And |
When you create multiple rule conditions, it is important to specify whether they are evaluated as AND, OR or a CUSTOM combination. In this case it will be only a single rule, so we’ll leave it as “AND” |
Failure Message |
No access to the B2C Price List |
This will not be shown to the customer |
Active |
✔ |
|
Effective From |
[today’s date] |
C. Click Save.
D. In the left sidebar, click the Function Arguments facet.
E. Click New Argument to define the Output of the function.
F. In the Function Arguments window on the right, enter the following information.
Field |
Value |
Notes |
Sequence |
1 |
Can be 1 or any other number. Sequence is more important for the input order |
Argument Name |
PermissionSetOutput |
make it obvious that is the output |
Direction |
Output |
|
Data Type |
Boolean |
Select the data type of the output, in this case we will receive either TRUE or FALSE, so Boolean is the most indicated values type |
Domain Type |
Type In |
Use Type In in case of doubt. This parameter is more important for the Input |
G. Click Save.
H. Click New Argument to define the Input of the function.
I. In the Function Arguments window on the right, enter the following information.
Field |
Value |
Notes |
Sequence |
2 |
The sequence should follow the order of inputs in the apex method. If the method receives a String first and then a Boolean, then the sequence of the String input should be lower than the sequence of the Boolean input (ex: 1 – String and 2 – Boolean) |
Argument Name |
PermissionSetInput |
Make it obvious that is the input |
Direction |
Input |
|
Data Type |
Text |
Select the data type of the input, the Apex method expects only a single variable, a String variable containing the Custom Permission name |
Domain Type |
Type In |
Use Type In in case of doubt. This parameter is more important for the Input |
J. Click Save.
6. (Optional) Create a Context Scope if it doesn’t exist already.
The scope depends on the context of where the class should be executed.
If it should run only for Opportunity, Quote, Order, Asset, etc... then select a scope linked to the appropriate entity Object.
If it should always be executed (ex: checking user permissions), then use a scope for the Any Vlocity entity Object.
If the class should be run for the Vlocity Adjustment Data virtual Object, then choose the Vlocity Adjustment Data scope.
A. In the Vlocity Product Console at the Rules section, click the plus icon ( + ) next to Context Scope to create a new function.
B. In the General Properties dialog, enter the following information:
Field |
Value |
|
Name |
AnyScope |
|
Code |
SCOPE_ANY |
|
Description |
Root scope for the “Any” Vlocity Object |
|
Scope Type |
Entity |
Click Save.
Create a Context Dimension.
7. The Context Dimension is what will actually run and contain the results from the Vlocity Function declared earlier.
A. In the Vlocity Product Console in the Rules section, click the plus icon ( + ) next to Context Dimension to create a new function.
B. In the New Context Dimension dialog, enter the following information.
Field |
Value |
Notes |
Name |
HasB2CPermission |
Context dimension names cannot contain any spaces due to requirements from the rules engine. |
Code |
CD_HasB2CPermission |
|
Description |
Value of the check on the user permission set |
|
Condition Weight |
10 |
Add a condition weight as you see fit, it’s not mandatory |
Data Type |
Boolean |
Should be the data type of the output of the apex class |
Domain Type |
Type In |
This represents the possible values that are expected from the class. Either select Type In or Picklist if there is a picklist that represents the possible values. In cause of doubt use Type In, this allows to type in the expected value. |
Vlocity Picklist |
if the Domain Type chosen was Picklist then reference the picklist. |
|
Active |
✔ |
|
Effective From Date |
[today’s date] |
C. Click Save.
D. On the left sidebar, click the Context Mappings facet.
E. Click New Context Mapping.
F. In the Context Mapping dialog, enter the following information.
Field |
Value |
Notes |
Context Scope |
AnyScope |
Select the previously created Scope |
Initialization Policy |
Always Reinitialize |
|
Initialization Type |
Function |
|
Vlocity Function |
HasCustomPermission |
Select the function created
|
Sequence |
1 |
|
Active |
✔ |
|
Effective From |
[today’s date] |
|
Type In |
B2CPriceListAccess |
Pass the API name of the Custom Permission that the user should have to be able to view the pricelist |
G. Click Save.
8. Create a Rule:
A. In the Vlocity Product Console in the Rules section, click the plus icon ( + ) next to Rule to create a new function.
B. In the New Rule dialog, enter the following information.
Field |
Value |
Notes |
Name |
B2C Access |
|
Code |
RULE_B2CACCESS |
The Rule Code may be used within custom expressions and therefore should be unique and should not contain any spaces. |
Description |
Check if the current user should be given access to the B2C Price List |
Describes the purpose of the context rule. |
Expression Mode |
And |
When you create multiple rule conditions, it is important to specify whether they are evaluated as AND, OR or a CUSTOM combination. In this case it will be only a single rule, so we’ll leave it as “AND” |
Failure Message |
No access to the B2C Price List |
This will not be shown to the customer |
Active |
✔ |
|
Effective From |
[today’s date] |
C. Click Save.
D. In the left sidebar, click the Rule Conditions facet.
E. Using the Add Condition dropdown menu, select Simple.
F. In the Rule Condition window on the right, enter the following information.
Field |
Value |
Notes |
Code |
RC_B2C_ACCESS |
Rule Condition Code may not contain any spaces due to requirements from the rules engine. |
Context Dimension |
HasB2CPermission |
Use the lookup to select the context dimension you created earlier in this exercise; this contains the value returned by the apex class. |
Operator |
== |
This is the operator used to compare the value to the context mapping. |
Value |
✔ |
The appearance of the value box depends on the data type defined for function. Since we selected Boolean as the data type, a checkbox will be displayed |
Fail level |
Hard Fail |
G. Click Save.
9. Create a Rule Set:
A. In the Vlocity Product Console in the Rules section, click the plus icon ( + ) next to Rule Set to create a new function.
B. In the New Rule Set dialog, enter the following information.
Field |
Value |
Notes |
Name |
HasB2CAccess |
|
Description |
Check if the current user should be given access to the B2C Price List |
|
Rule Type |
Qualification |
|
Rule Purpose |
Eligibility |
|
Expression Mode |
And |
If the Rule Set will only contain one Rule, select AND, otherwise select the appropriate expression to evaluate the rules |
Failure Message |
||
Active |
✔ |
|
Effective From |
[today’s date] |
C. Click Save.
D. Select Children Rules. Click New Children Rule.
E. In the New Children Rule dialog, enter the following information.
Field |
Value |
Name |
HasB2CAccess |
Rule |
B2C Access |
Sequence |
1 |
Action Taken |
Qualify |
F. Save the Rule.
10. Apply the rule to the Vlocity component of choice, ex: Price Lists, Products, Promotions, Discounts, etc. In this example we will add to one Price List.
A. In the Vlocity Product Console in the Pricing section create a new Price List:
Field |
Value |
|
Name |
B2C Price List |
|
Code |
B2C_Price_List |
|
Description |
Used to test access via permission sets |
|
Price Book |
Standard Price Book |
|
Sequence |
30 |
|
Active |
✔ |
|
Effective From |
[today’s date] |
B. Click Save.
C. Select Context Rules. Click Add Rule Set and select the Rule set.
D. Click in the Button Add Rule Set.
E. Search for the rule HasB2CAccess.
F. Add the rule HasB2CAccess.
G. Save the Rule.
11. Clean and repopulate Cache.
Context rules, rule conditions and rule sets are cached in the org cache of the CPQPartition platform cache. When troubleshooting, you will need to clear the cache for changes to be reflected at run time.
A. Go to the Vlocity CMT Administration tab.
B. Click on Maintenance Jobs and click the Start button for the following jobs.
Results:
1. User without the permission Set B2CPriceListAccess
2. User with the permission Set B2CPriceListAccess
As we have seen this article explains how to create context rules with complex logic and use them for any SObject. We can leverage the power of apex and the ubiquity of Context rules to control the visibility of almost every component in the Vlocity cart: price lists and prices, product eligibility, promotions and discounts.
Additionally, creating Vlocity Functions this way, exposes them to DataRaptors and Integration Procedures formula fields with the added benefit of making them reusable.