Verify if the products with the same code in the table records have a different price

I have a table records that have some rows with some info using the Entity “Product” like this “trProducts.List.Crrent.Product. Description”.

The Entity “Product” has a column “Code” and also a column “Price”. There is a validation rule that if the table records “trProducts” has more than one product with the same “Code” (trProducts.List.Crrent.Product.Code) those products should have the same price (trProducts.List.Crrent.Product.Price).

My doubt is how to know if there are multiple products in the table records with the same “Code” and for those products check if the “Price” is different.

I created a function “Validate” that receives the table records list and returns a boolean (if is valid or not, if the prices are different and the Code is the same is not valid). But now I'm not understanding properly how to verify if there are products with equal “Code” and if there are if those products have the same “Price”.

To achieve this I have a flow like this:


Start   

Assign with: ListAux = List

Foreach with: Record List: List (is the table records list input parameter of the action), Start Index 0

 Then also another Foreach with: Record List: List, Start Index: 0

  Then an  If "EqualCode" is true "ListAux[List.CurrentRowNumber].Product.Code = ListAux[List.CurrentRowNumber].Product.Code" then I have also other if "DifferentPrice" with the condition"ListAux[List.CurrentRowNumber].Product.Price = ListAux[List.CurrentRowNumber].Product.Price", if this "DIfferentPrice" condition is true I have an assign IsValid = False, if is false the flow returns to the inner foreach.

End


But it shows an error "Detected recursive iteration over the same record list". Do you know how to properly verify if the products with the same code in the table records have a different price?

I can think of a couple of ways to achieve this. 


First one on the same lines as you are thinking would be , 

- Select Code, Price from the table based on the current code. 

- Use list.Any function to see if there is anything in the list where Price NOT EQUALS Price of the first record. 


Second and simpler approach would be , 

- Use a SQL query to group by Distinct Code, Price and also sect a count(*) . If the count is > 1 then there are records with varying prices. 


Hope this helps.


Cheers,

Tushar

Tushar Panpaliya wrote:

I can think of a couple of ways to achieve this. 


First one on the same lines as you are thinking would be , 

- Select Code, Price from the table based on the current code. 

- Use list.Any function to see if there is anything in the list where Price NOT EQUALS Price of the first record. 


Second and simpler approach would be , 

- Use a SQL query to group by Distinct Code, Price and also sect a count(*) . If the count is > 1 then there are records with varying prices. 


Hope this helps.


Cheers,

Tushar


Thanks, but the table records already have all the information (each row has info about the product like the price and code). Is not possible to use the info in the table records to verify if the products with the same code in the table records have a different price? 


The flow that I have that shows the error "Detected recursive iteration over the same record list":


Hi Oscar,


this is correct error message, you are probably doing a ForEach twice on the same list, that's not possible, OS keeps track of where you are within the for each with 'Current' and can't do that if you add a second ForEach inside the loop.

This seems like a very complex attempt at something that should be much easier, can you share the oml, or maybe share your entity definitions (of product and related entities), can you explain why you are trying to check this unique price on a table records and not in the database.

How does the table records get populated, by an Aggregate ?  If so, everything in the TableRecords is also available in the database, right ?Then I can think of a few places earlier in the process where you should prevent wrong prices :

1) in your data model : if price should be same for all products with same code, then in data model price should be an attribute of some productCode entity, and product should only have productCodeId, ensuring that same codes always have same price

2) the place where you add products to your database : before doing a create or update, verify that there are no other products with same code but different price.

Hope I didn't confuse things for you,

Dorine

Solution

Hi Oscar,


If you are stuck with doing this validation on the Table Records, You could define a second list holding only 2 attibutes : code and price.

Then while looping through the list of your table records, do an ListIndexOf to see if that code is already in your second list, if not, do an appendList, if it is then compare prices

See attached OML

Dorine

Solution