6/29/2025 1:32:02 PM
|
|
slxdeveloper.com Community Forums |
|
|
|
The Forums on slxdeveloper.com are now retired. The forum archive will remain available for the time being. Thank you for your participation on slxdeveloper.com!
Forum to discuss the use of the SalesLogix Web Platform, Client and Customer Portals, and the Application Architect (For version 7.2 and higher only). View the code of conduct for posting guidelines.
|
|
|
|
Have an entity with a collection of child entities. Need a DB field that summarizes data from that collection. How to have it in sync?
Posted: 06 Oct 10 11:55 AM
|
Ok, here's the deal.
I have an entity (lets call it invoice) which has a collection of child entities (lets call it invoiceLines). Each invoiceLine has an amount field. I need a db field (a real db field, not calculated one) that summarizes the amount of the invoiceLines at the invoice level. It should stay in sync, so when you add, delete or update an invoiceLine, that amount must be updated. Makes sense?
My approach has been to create a BR on the invoice entity to update that sum:
public static void RecalculateTotalAmount( IInvoice invoice) { double? sum = 0; foreach(IInvoiceLine line in invoice.InvoiceLines){ sum += line.Amount; } invoice.TotalAmount = sum; }
And call that BR on OnAfterInsert, OnAfterDelete and OnAfterUpdate events at the invoiceLine level. The events at the invoiceLine level look something like:
public static void OnAfterUpdateStep( IInvoiceLine invoiceline) { invoiceLine.Invoice.RecalculateTotalAmount(); //make the parent invoice recalculate its total amount invoiceLine.Invoice.Save(); // save the parent invoice }
It seems to be working all right, but i'm not sure if this is the correct approach to do something like this. I'm also having a problem where I'm inserting a new invoice and start adding new invoiceLines and need to delete one of them. The afterDelete event is triggered, the total amount is recalculated, and the parent invoice is saved (due to the "Save()" at the end of the onAfterDelete) which is wrong. So, i need some way to tell if we are deleting an invoiceLine of an invoice that has not been persisted at all. Makes sense?
I'd like to hear what the more senior people here can comment on this... |
|
|
| |
| |
|
You can
subscribe to receive a daily forum digest in your
user profile. View the site code
of conduct for posting guidelines.
Forum RSS Feed - Subscribe to the forum RSS feed to keep on top of the latest forum activity!
|
|
|
|
|
|
|
|