Globalised Show All method

I’ve been looking around on the forum for a method that could resolve the problem of combining Filemakers GTRR ( go to related record) and show all functionalities, leaving you on the “destination” record but not with only one record in the foundset.

The first part is obvious

form[destinationform].show(name of the relation pointing to the desired record)

Nothing special here

However the problem was that I was left on the desired form with only 1 record in the foundset. What I wanted was being on the desired form (that part was already taken care of), but combined with the loadAllrecords command AND remaining on the previously selected record, instead of going to the first record of the table.

For this I needed to use the foundset.selectRecord(pk) technique. Problem was I wanted a generic (global) method capable of functioning for all tables I wanted to relate to.
I think I cracked that nut. This is the code I used

var ThisTableName = currentcontroller.getTableName();
var ThisServerName = currentcontroller.getServerName();
var ThisJSTable = databaseManager.getTable(ThisServerName,ThisTableName);

var arColumnNames = ThisJSTable.getColumnNames();
var NrOfColumns = arColumnNames.length

var PKColumn = 'nop'
for ( var i = 0 ; i < NrOfColumns ; i++ )
{
 	aJSColumn = ThisJSTable.getColumn(arColumnNames[i])
	if(aJSColumn.isRowIdentifier())
	{
	PKColumn = arColumnNames[i]
	}
}
if( PKColumn != 'nop')
{
	var ThisForm = currentcontroller.getName()
	var ThisRecord = forms[ThisForm].foundset.getRecord(forms[ThisForm].foundset.getSelectedIndex())

	var thePK = ThisRecord[PKColumn]

	currentcontroller.loadAllRecords()
	forms[ThisForm].foundset.selectRecord(thePK)
}
else
{
 var thePressedBtn = plugins.dialogs.showWarningDialog( 'Groepeer Probleem',  'Show All werkt niet voor deze tabel, waarschuw Bart','OK')
}

Obviously this code can still do with some optimalisation (breaking out the for loop, testing for exceptions), but I think the concept is clear.

This code is working well for me (but for the moment still on limited datasets) and I hope somebody else can put it to use.

If there seems to be problems with the code, please post them, because I want to make this my generic “show all” method :)