"MAX" Question

If you have questions or if you want to share your opinion about Aware IM post your message on this forum
Post Reply
john
Posts: 113
Joined: Tue Jul 25, 2006 10:48 am
Location: UK

"MAX" Question

Post by john »

Hi

I am just having a small problem with the MAX function. I am trying add a uninque ID to a record by taking the MAX value of all records that meet a particular criterial and adding 1 onto it. Basically I need my unique ID to be concurrent for each person submitting new data into the system.

For Example John has 10 records, his next will be 11
Mary has 3 records, her next record will be four

(Im dont mean to sound patronising but I cant think of another way of explaining it)

My criteria is defined by the persons login name which is copied into every record when its created so it is possible to count:

If Temp IS NEW Temp.AgencyCode = LoggedInAgency.LoginName

My Code to generate the unique ID is as follows

If Temp IS NEW Then
Temp.indexAgency=MAX Temp.indexAgency WHERE (Temp.AgencyCode=LoggedInAgency.LoginName)+ 1

My problem is that the new records keep missing one everytime a record is created, for example two records created one after the other have a Temp.indexAgency value of 1 then 3 (wheres 2)

I am sorry to be a pain but I had to ask because its only a small piece of code and I have been staring at it for hours - can you see anything wrong with it? If I dont put the +1 on the end my records repeat the same value very odd?

I hope that all makes sense

Best Regards
John
aware_support
Posts: 7525
Joined: Sun Apr 24, 2005 12:36 am
Contact:

Post by aware_support »

Please keep in mind that MAX expression INCLUDES the object being created. This can potentially screw up your calculations if you are not careful. Not sure if this is the problem here.

If you don't want to include the object being created add the following condition:

If Temp IS NEW Then
Temp.indexAgency=MAX Temp.indexAgency WHERE (Temp.AgencyCode=LoggedInAgency.LoginName AND Temp.ID<>ThisTemp.ID)+ 1
Aware IM Support Team
john
Posts: 113
Joined: Tue Jul 25, 2006 10:48 am
Location: UK

Post by john »

A ha

It looks like that was it, I never considered that the MAX function included the record you were currently submitting. I have used this before but it didnt seem to matter as the unique ID's were not in series for each agency.

My other example reads:
If CreditChecks IS NEW Then
CreditChecks.index=MAX CreditChecks.index+1

This seems to work ok, I am assuming this is becase there are no paramters passed to the MAX function. For both examples however I am now wondering what the consequences are for two people trying to submit the form at once?

My Credit Checks seems to handle this ok but as I have now added the extra bit of code (provided by youselves) is this likely to become a problem? I ask because if the code only counts the records already in the system then surely this would mean, if the same form is opened by two people there will be a repeated value?

I am a little confused, could you advise a little on this as I have to get this right

Best Regards
John
john
Posts: 113
Joined: Tue Jul 25, 2006 10:48 am
Location: UK

Post by john »

Further to my previous note I have just tested that theory out and it seems to be ok even though I was logged in as the same user twice, so your system has surprised me again! (any explanation on how this works would be great)

Also just as an extra bit of protection for my piece of mind could you advise on how to get the system to recognise when a particular user is logged in and therefore not allow the same credentials to be used on another machine. This is something I have been keen to achieve anyway but not managed to figure out or got round to asking

Thanks again
John
aware_support
Posts: 7525
Joined: Sun Apr 24, 2005 12:36 am
Contact:

Post by aware_support »

Further to my previous note I have just tested that theory out and it seems to be ok even though I was logged in as the same user twice, so your system has surprised me again! (any explanation on how this works would be great)

This works because Aware IM internally uses database transactions and transaction isolation levels. This is a relatively complex subject and if you are not familiar with it you can look it up in the appropriate books (the explanation of this is beyond the scope of this forum, probably).

Also just as an extra bit of protection for my piece of mind could you advise on how to get the system to recognise when a particular user is logged in and therefore not allow the same credentials to be used on another machine.

You need to keep track of users that log into the system. You can do this by intercepting the Login Notification and attaching the appropriate rules to this notification (see User Manual about handling login events). The rules should create an object that represents a logged in user (the rules attached to Logout notification can delete the object). Then you should add an initialization process (a process that is specified as "initialization" in a visual perspective). The process can check whether the currently logged in user is already registered and REPORT ERROR in this case.
Aware IM Support Team
john
Posts: 113
Joined: Tue Jul 25, 2006 10:48 am
Location: UK

Post by john »

Thanks for this you have been a great help

John
Post Reply