Update BO with the Last Record from a child BO

If you have questions or if you want to share your opinion about Aware IM post your message on this forum
Post Reply
Jhstephenson
Posts: 297
Joined: Wed Apr 22, 2015 11:44 pm

Update BO with the Last Record from a child BO

Post by Jhstephenson »

I need to create an expiration date for a BO based on child records for that BO that have a 'Expires in Years' type attribute.

Here is what I have:
Parent BO (Event): Has a attribute called LastExpirationDate and one called "EventDate"
Child BO (Reasons): Multiple records that have atrributes called "Reason" and "ExpiresInYears"

Whenever an Event is saved I have a rule that checks to see if there are any Reason records for that Event. If there are I want to update the Event LastExpirationDate with (EventDate + ExpiresInYears - 1 Day). But I really only want to update it with the largest ExpiresInYears value.

So, if I have 5 records in the Reasons BO and they have expiration values of 1,2, 3, 4, & 5; I really only want the last one to be used to update the LastExpirationDate attribute.

Does that make sense?

Right now I have it read through each Reason record, sorted by the Expiration value and update the LastExpirationDate everytime, hoping that the last one is the one I actually want. My rule looks like the following:

Code: Select all

If Event.child_Reasons IS DEFINED Then 
FIND Event_Reasons WHERE Event_Reasons.Event=Event AND Event_Reasons.ExipresInYears>0 ORDER BY Event_Reasons.ExpiresInYear ASC
Event.LastExpirationDate=AS_DATE(AS_STRING(DAY_OF_MONTH(Event.EventDate)-1)+'-'+AS_STRING(MONTH(Event.EventDate))+'-'+AS_STRING(YEAR(Event.EventDate)+Event_Reason.ExipresInYears),'dd-MM-yyyy')
The actual calculation of the ExpirationDate works fine. It is the order of the records that seems to be an issue. It is inconsistent.

I am sure there is an easier way to do this, but not sure what it is yet.

Any ideas?
BLOMASKY
Posts: 1473
Joined: Wed Sep 30, 2015 10:08 pm
Location: Ocala FL

Re: Update BO with the Last Record from a child BO

Post by BLOMASKY »

I believe the problem is that while you are sorting the child records you are not reading only the largest expiration years. Add a TAKE BEST 1
then call a subprocess with both the parent and child passed to it, and put your calculation in the child process.

Does that make sense?

Bruce
Jhstephenson
Posts: 297
Joined: Wed Apr 22, 2015 11:44 pm

Re: Update BO with the Last Record from a child BO

Post by Jhstephenson »

Thanks Bruce. I forgot about ‘TAKE BEST’. I will. Play with that and see what I get.
Jhstephenson
Posts: 297
Joined: Wed Apr 22, 2015 11:44 pm

Re: Update BO with the Last Record from a child BO

Post by Jhstephenson »

All I had to do was change my sort order to DESC and add TAKE BEST 1 and it is working.
Post Reply