This was a question I found on the Salesforce Trailblazer community that I thought was a really interesting use case to solve declaratively. Here’s a link to the original question.
The original use case asked for a solution to prevent any given product from being reserved for sale for more than N times in a given month.
I’ve got a solution in mind that might help you with your use case.
Assuming you’re using Opportunity Products to track your sales of Products, and that ‘only support the sale of 8 products per month’ is defined by an Opportunity Product’s Opportunity Close Date being within a given month, you could achieve this by using a record-triggered flow on Opportunity Product.
You could build an after-save record-triggered flow to update a custom number field (say ‘Number_Reserved_N_Month__c’) on Product when your users create a new Opportunity Product related to that Product. The flow would:
- Get all existing Opportunity Products for the new Opportunity Product’s Product. Store as a collection variable.
- Loop through the Opportunity Product Collection variable and count all Opportunity Products whose parent Opportunity Close Date is in the same month as the new Opportunity Product’s parent Opportunity Close date
- Update the Opportunity Product’s Product Number_Reserved_N_Month__c field with the count of Opportunity Records that have the same Product and parent Opportunity Close Date as the new Opportunity Product
Create a field on your Product object to specify the maximum number of items for that product to be sold per month (maybe Maximum_Reserved_per_Month__c).
Create a validation rule that throws an error on Product if Maximum_Reserved_per_Month__c > Number_Reserved_This_Month__c (because this is an after-save flow, it’ll count the newly created Opportunity Product too).
This should stop users from creating more than Maximum_Reserved_per_Month__c of a given Product for Opportunities.
Here’s a link to the Salesforce Product schema that might help visualise this.
Hope this makes sense.