Passing a form object to a function

Passing a form object to a function

Suppose I have a sort_order field in multiple tables and I want to increment it by +1 onCreate of a new record.

Is there a way to pass the form object as an argument into the function to keep things DRY?

The following function from Zia works, but I'd need to repeat it for every form, which results in a lot of code duplication.

int incrementByOne()
{
// Fetch the record with the highest sort_order from MyForm 
latestRecord = MyForm[ID != 0] sort by sort_order desc range from 0 to 0;

// Initialize nextSortOrder to 1 by default
nextSortOrder = 1;

// Check if a record exists and has a valid sort_order
if(latestRecord.count() > 0) {
if(latestRecord.sort_order.isValidObject()) {
nextSortOrder = latestRecord.sort_order + 1;
}
}
return nextSortOrder;
}