Issue with adding products to the cart and creating invoices
Merry Christmas )
am using Ninox to manage sales and carts, and I am facing an issue when adding products to the cart. I want to implement the functionality where:
- If the client already has an open cart (invoice with
CartClosed = "No"
), the new product should be added to that cart. - If the client doesn’t have an open cart, a new invoice should be created, and the product should be added to that new invoice.
However, despite the correct condition checks, the system always creates a new invoice even if an open cart already exists. I am using the following code:
let selectedClient := this.Clients;
let xNewProduct := this.Products;
let xNewQuantity := this.Quantity;
let openInvoice := first(select Sales where Clients = selectedClient and CartClosed = "No");
if openInvoice != null then
let existingProduct := first(select Sail_tab where Sales = openInvoice and Products = xNewProduct);
if existingProduct != null then
existingProduct.('Quantity1' := existingProduct.'Quantity1' + xNewQuantity);
else
let newSailTab := create Sail_tab;
newSailTab.(Sales := openInvoice);
newSailTab.(Products := xNewProduct);
newSailTab.('Quantity1' := xNewQuantity);
end
else
let newInvoice := create Sales;
newInvoice.(Date := today());
newInvoice.(Clients := selectedClient);
newInvoice.(CartClosed := "No");
let newSailTab := create Sail_tab;
newSailTab.(Sales := newInvoice);
newSailTab.(Products := xNewProduct);
newSailTab.('Quantity1' := xNewQuantity);
end;
this.Products := 0;
this.Quantity := null;
What’s not working:
- When adding a product to the cart, it always creates a new invoice even if an open cart already exists.
- It’s not correctly finding and using the open invoice to add products.
What I’ve tried:
- Using a query to find the open invoice with
CartClosed = "No"
. - If an open cart is found, I add the product to it.
- If no cart is found, I create a new invoice.
Question: Why is it not finding the open invoice, and why does it always create a new one? How can I fix this code to correctly work with already existing open carts?
10 replies
-
I would test out line 4 by itself to see that you are actually getting the records you want. So in a new formula field just copy the first 4 lines and then put this at the end:
debugValueInfo(openInvoice)
Do you see a nId returned when you select a client?
After you solve the above, it looks like Sales and Clients are linked so you can replace the select in openInvoice with:
let openInvoice := first(selectedClient.Sales[CartClosed = "No"]);
-
I suppose your CartClosed is a Yes/no field? Then you should know that such fields have 3 states: true, false and null.
So if you want to check if the CartClosed is "No" it will probably would be null since you never touched it. A solution would be is to check if it is not "Yes". Changing line 4 in your code to: let openInvoice := first(select Sales where Clients = selectedClient and CartClosed != true); can probably help...
Content aside
- Status Answered
- 2 wk agoLast active
- 10Replies
- 59Views
-
4
Following