scottmahr - Thursday, April 21, 2011 9:38 PM:
I really think this should be easy, but I am having issues.
I have a "purchase request", and "line items" that belong to each request. When I add or update either of these, I need to update the total cost in each. For the purchase request, this finds all the line items, and sums their total costs and the shipping cost for the purchase request to get a total cost. For the line items, it multiplies the quantities by the unit cost to get the total cost.
This works just fine off of update, everything updates and is fine. My issue is upon creation. If I create a purchase request, with line items the total for the purchase request just reflects the stuff in the purchase request itself, and doesn't find any of the newly created line items. I use "onBeforeAdd" and "onBeforeUpdate" for both entities.
I then made another method that simply finds the purchase request for the line item, and triggers an edit (by setAction("edit"); apply(); ). I tried putting this "onAfterAdd", and also "onAfterUnlock" on the line item. My rational was that the purchase request was being created before the line item (and thus not finding it to add it up). I figured that if I triggered an edit after the line item was added, the "onUpdate" flag would cause the purchase request to re-sum everything. It works fine if I go in and lock then unlock the line item, but still doesn't work when I add all of them at the same time.
I might just be missing some key idea here. Any help would be great.
Thanks, Scott
aknourenko - Thursday, April 28, 2011 12:23 PM:
In "onBeforeAdd" for "purchase request" its "line items" are not created yet, so you don't find them when you try to "get" them from Innovator (NOTE: it's not clear to me if you have at all and, if you do, what you are trying to do in "onBeforeAdd" for "line items"; in my opinion having "onBeforeAdd" for 'line items' is pretty pointless). From other side the "inDom" in "onBeforeAdd" for "purchase request" must contain information about the "purchase request" itself AND all its "line items" as <Relationships>. So you can try to just find all "line items" in the "inDom" XML and their cost and calculate the total cost which put into the total cost for the "purchase item".
jjlong - Friday, April 29, 2011 12:18 PM:
Scott,
Try tying it to "OnAfterGet". The Programmer's Guide says "OnAfterGet runs after a search is executed, but before the results are returned. OnAfterGet methods are commonly used to populate federated properties. This might involve performing calculations on other properties or extracting data from an external system. Once the value of the property is set, it appears to the client just like any other property."
I wrote a server method to set "ColorList" color codes based upon values selected from a list. When I tied the method to "OnAfterGet" the colors would appear as soon as I selected a value and hit the tab key to the next field.
I still had to tie the code to "OnBeforeAdd" and "OnBeforeUpdate" to get the value to persist in the database, but by having it in the "OnAfterGet" I could see the results before a "Save" was performed. It also means the logic get run a lot, so for the sake of performance you need to be prudent in what you perform.
Let me know if this works,
Jeff
scottmahr - Tuesday, May 10, 2011 2:30 PM:
Thanks Jeff, works great. I removed the the beforeAdd and beforeUpdate triggers. If we are going to calculate on ever get anyways, no real reason to have it persist in the database.
Scott