Introduction
When creating an opportunity, allow the user to pick a currency associated with multiple price lists for their products. Since each price list contains a collection of products with their own unit price, when an opportunity is assigned to a price list the product’s cost will be set automatically.
When creating an opportunity, allow the user to pick the currency and then only display pricelists of the same currency. This works because each pricelist has a defined currency. Once the opportunity’s currency and pricelist are saved, allow the user to add products. Because each opportunity is assigned a pricelist, and that pricelist contains a collection of products with their own unit price, the product’s price must be set automatically and be a read-only field.
Step-by-step instructions
1. Defining a User’s Currency Selection and Price List Association
While a user is creating an opportunity, the currency of the opportunity needs to impact which pricelist will be available for selection. To limit the pricelists available to a user, add a candidate value to the “priceList” param of the “create” command from the opportunity Aggregate. The data type should be “pricelist”. Then, add a criteria to the candidate value, select “currency equal (JS) values.currency”. In the javascript block, "values" contains the data selected in the dialog before submitting the form. This is possible because a pricelist is associated to currency.
2. Displaying Price Lists Based on Selected Currency
Similarly, after the opportunity has been created, the user might need to change the price list previously selected. In this scenario, the user should once more be limited only to price lists of the same currency as the one saved for the opportunity. This time, when adding a candidate value to the “priceList” param of the “changePriceList” command from the “opportunity” Aggregate, the Javascript should be “data.currency” instead of “value.currency”.
3. Resetting Price List When Currency Changes
This step is part of this workflow but isn’t related to “on change” or “candidate value”.
To empty the pricelist selection if the currency changes, add a mutation to the “changeCurrency” command. Add the “pricelist” field and select “null value” as type.
4. Restricting Products to Available Price Lists
Keep in mind that some products could be available only in some pricelists and each product’s price could vary from one pricelist to another. So, when adding products under an opportunity, the user should only have access to add those products that are part of the pricelist selected for the opportunity.
If it hasn’t already been done, create, an “add Entity command” under the “opportunity” Aggregate with the container field name set to “products” and with “product” and “quantity” as required fields as well as “unitPrice as an unrequired one. In this example, this command was named “addOpportunityProduct”. At this point, any products can be added to opportunities.
Then, let’s see how to filter our products. Simply add a “candidate value”, in JavaScript, to the “product” param of the newly created command:
data.priceList.priceListItems.map("product")
While adding products from the pricelist selected, to an opportunity, the price of the product must match the one from that pricelist.
To do so, add this Javascript line in the “on change” section of the properties panel:
return p => ({
unitPrice: data.priceList.priceListItems.find({product: p}).price
})
At this point, when the user adds a product, the price is automatically displayed. However, the price could be manually edited by the user.
5. Making Unit Price Read-Only During Product Addition
The user should not have the option to change the unit price of the items; the field should be a “read only” one.
To achieve a more granular control over the UI of the dialog, add a dialog under the “addOpportunityProduct” command, from the “opportunity” Aggregate. Then, select the “unitPrice” field of the dialog and activate the “read only” option. It can be found under the properties subpanel of the project explorer panel.
Go Live to test.
Conclusion
Using Candidate values and "on change" actions ensures that the appropriate price list is automatically selected based on the chosen currency. This dynamic approach streamlines product price management, enhances accuracy, and provides a smoother user experience in handling opportunities within the system.