Testing for relations question...

Hi,

I’m testing to see if a record has any related records before deleting. I have a way of doing it but I’m wondering why I can’t do it in a simpler way. Basically I’m putting all the relationships in a variable and then looping through those testing whether they are true or not. So this works:

var availRelations = allrelations
var currRecord =  foundset.getRecord(foundset.getSelectedIndex())
for ( var i = 0 ; i < availRelations.length ; i++ )
{	
	var currRelation = availRelations[i]
	if(databaseManager.hasRecords(currRecord[currRelation]))
	{
		doSomething;
		return;
	}
}

But I’d like to understand better why this doesn’t work when I just grab the name of the relation in a variable and test that directly like this:

var availRelations = allrelations
for ( var i = 0 ; i < availRelations.length ; i++ )
{	
	var currRelation = availRelations[i]
	if(databaseManager.hasRecords(currRelation))
	{
		doSomething;
		return;
	}
}

If I hardcode the relationship name into .hasRecords(namedRelation) then that works, so why doesn’t it work putting that name into a variable and then putting that variable in hasRecords? Is there a way I can loop through those relationship names and use them directly? I’m just trying to understand exactly what is going on so that I’ll remember it better! :) Any help appreciated!

Comming from memory here, but I think if you put the name between , then it works.

Paul

eval() the name of the relation:

var availRelations = allrelations 
for ( var i = 0 ; i < availRelations.length ; i++ ) 
{    
   var currRelation = availRelations[i] 
   if(databaseManager.hasRecords(eval(currRelation))) 
   { 
      //doSomething; 
      return; 
   } 
}

Thanks, David, that works of course. I’m always forgetting about ‘eval’, partly because it doesn’t show up anywhere in the method tree and mainly because I never have quite understood it. In playing around with eval() I guess in this case it turns ‘currRelation’ from a string to an object and I guess that is what hasRecords is looking for rather than a string.

When I ‘follow the logic’ I would say that you are one step behind when you put the relation in a variable.

The test ‘hasRecords’ tests, by heart: is the relationship defined, is there something on the left side, is there something on the right side.

When you do it per your suggestion the variable can be null.
Although the result is the same I guess it ‘does not feel right to me’…

Hi John,

Are all your relations in the context of this table ? If not then your method might result in false positives.
Maybe you should pass the method a specific set of relations.

Hope this helps.

i do prefer the current record way…
its faster then eval and way more explicit!
Now you are testing the current selected record of the foundset so it will be ok. But doing the first loop is better it also works for records that are not the selected in the foundset.