function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
RestAPI UerRestAPI Uer 

Validation in trigger based on unique price on Quoteline item object

Hello All,

Let me explain the scenario in detail here. So I have one Quote record. For that they 10 line items. I am using the following three fields:
  • Reference URN Number(Integer)
  • Quantity(Number) 
  • Estimated Amount(Currency)
There are some line items which has the following criteria:
  1. same URN number, Quantity and Amount. 
  2. Same URN number, Amount. But Quantity is unique
  3. Same URN number, Quantity but Amount is unique.
Validation must throw in Quote level If I change the status to Submit and the related line items have Same URN Number but Quantity and Amount is not Unique.

How to achieve this scenario? Is this possible through validation rule?


Thanks in advance
 
Andrew GAndrew G
A simple validation won't work, as the parent record can't "see" all the children in that scenario.  A validation is done on the record itself, or you can check parent records, to a certain degree.

To do what you are after, I would:

Create a trigger on the Quote Line Item record.
On Insert or Update
Get the Quote Id from the parent record  ( parentQuoteId = quoteLineItem.QuoteId)
Using that ID, do a SELECT statement of all QuoteLineItems where the QuoteId = parentQuoteId
Then loop the results to Set of Ids of the URN number.  (sets are unique so duplicates will get eliminated)
Test the size of the Set against the Size of list of QuoteLineItems.
If the sizes aren't the same, then there are some duplication of URN numbers
//now the tricky bit.
create 1 x maps
where Map<String, QuoteLineItem> where string is a concatenated string consisting of the URN + Quantity + Amount.
Map.put<key, quotelineitem>
Test the size of the Map now 
If the map size is smaller again, then we have duplication of the key, therefore the duplication of URN with Quantity with Amount.

Now at this point we can either 
Throw an error. - This will stop the quotelineitem being saved in the first place.
OR
we can update a field on the quote record (check box - duplicate quote items ) which we then test in the validation rule.

At the last step, you could use a Set again, as noted they are unique, i only suggested a map as you may want some reference to the quotelineitem in your error message.

That is a random, off the top of my head idea.

Regards
Andrew