related function error

These two functions (2.01, Mac 10.3.3):

var count = relation_name.recordIndex;
var count = relation_name.getMaxRecordIndex();

both return:

“TypeError: Cannot convert null to an object.”

if there are no related records. I need null or zero to be returned!

  • David

Is this helpfull?

if(relation_name) //works like a bolean
{
var count = "one or more"
}
else
{
var count = "zero, noppes, nada!"
}

That does it! Many thanks HJK.

Hopefully a function fix can be worked out so I can cut back on a few lines of code…

  • David
//set display label for tab 'related contacts'

var labelText = 'Related Contacts';

if (globals.g_similar == 'Company')
{
	if (similar_company)
	{
		var count = similar_company.getMaxRecordIndex();
		if (count > 1)
		{
			return labelText = labelText + ': ' + count + ' by Company'
		}
		else
		{
			return 'No similar records';	
		}
	}
	else
	{
		return 'No similar records';	
	}
}

if (globals.g_similar == 'Name')
{
	if (similar_name)
	{
		var count = similar_name.getMaxRecordIndex();
		if (count > 1)
		{
			return labelText = labelText + ': ' + count + ' by Name'
		}
		else
		{
			return 'No similar records';	
		}
	}
	else
	{
		return 'No similar records';	
	}
}

if (globals.g_similar == 'City')
{
	if (similar_city)
	{
		var count = similar_city.getMaxRecordIndex();
		if (count > 1)
		{
			return labelText = labelText + ': ' + count + ' by City'
		}
		else
		{
			return 'No similar records';	
		}
	}
	else
	{
		return 'No similar records';	
	}
}

//else default text
if (!globals.g_similar)
{
	return 'Locate similar contacts';
}

Checkout the databaseManager.hasRecords(<a (related) foundset>) in Servoy 2.1beta3, this should reduce your code to one ‘if’

   if (databaseManager.hasRecords(similar_company))
   { 
     return labelText = labelText + ': ' + count + ' by Company' 
   } 
   else 
   { 
      return 'No similar records';    
   }