Friday, July 8, 2011

Servoy TIP: Working With Record Objects

In the last Servoy Tip we explained how to assign a new record object to a variable. Once you have any record object referenced in a variable - there are cool things you can do.
You can "grab" the current record and store it in a variable by using this syntax:

var myRec = foundset.getRecord(foundset.getSelectedIndex());

But there are a ton of other things you can as well. Let's say you have two tables - one for Companies and one for Contacts, and you have created a relationship between Companies and Contacts named "companies_to_contacts". Here's some interesting things you can do with record and foundset objects:

get the current company record: 
var curCompany = foundset.getRecord(foundset.getSelectedIndex()); 
number of related contacts:
var contactCount = curCompany.companies_to_contacts.getSize(); 
gets first related contact record:
var contactOne = curCompany.companies_to_contacts.getRecord(1); 
reference a field in the first related record 
var contactOneID = contactOne.contact_id;

Because your variable contains the entire record object, you can reference anything about that record - including calculations, aggregations, and even relations several relationships away (in the below case the number of orders that first related contact has made):

var orderCount = curCompany.companies_to_contacts.contacts_to_orders.getSize();

Do you use record objects in your coding? If not, give it a try - it's easy and extremely powerful!

No comments:

Post a Comment