0

Is it possible to add a Validation Rule to Orders which checks a certain product name has been added to the order?

Basically the price book includes products a, b and c. If the user selects a certain field on the order then I need to ensure AT LEAST product b is added to the order. The easiest way to do this is to validate that a product exists on the order with the name like "removal".

I have failed so far and I have tried these:

  • Adding a Roll Up Summary field on Order Items. This didn't work as product name isn't available. I would then validate that this roll up summaries count was > 1.
  • Adding an order product Validation Rule on the product name and order field. This didn't work as it fired when adding products a and c and these can be legitimately added to the order.
  • Apex trigger on orderitems, I didn't get too far with this as I struggled with the logic.

Is there a recommended approach to validating order products?

1

You should be able to add the product name as a criterion in any rollup helper tool. I am most specifically familiar with Declarative Lookup Rollup Summaries, which is certainly capable of crafting the rollup you need.

1

An alternative to DLRS is to do the following:

  • Create a custom field on OrderItem called Product_Name__c
  • Create a workflow that populates OrderItem.Product__c with the value of OrderItem.PricebookEntry.Product2.ProductCode
  • Use standard RollupSummary field on Order (since OrderItem is master-detail) to count the number of OrderItem.Product__c = 'B' into a number field
  • Then you can use a Validation Rule on Order.

Personally, I like DLRS because it has better expressive power and doesn't involve this trick of avoiding cross-object fields in RSF criteria. For orgs that have well-written existing triggers, adding DLRS shouldn't be an impact.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.