return unique values in getFoundSetDataProviderAsArray

I have a donations report that returns records where the contact is not unique (as in, one or often more donations per contact, each in a separate row). I’d like to use that found set (in the donations table) to then set the found set of the contact table.

Using getFoundSetDataProviderAsArray(forms.report, ‘fk_contact’), fk_contact may not be unique and the array will contain duplicate values. How can I constrain it so that only unique values are in the array?

Thanks,

Jim

Hi Jim,

If you can you select the foundset via a distinct on that field. But that would give you a different foundset than you have now.
If that is not an option you need to do it yourself. Like this:

var aContact = databaseManager.getFoundSetDataProviderAsArray(forms.report, 'fk_contact'),
	nCurID = null,
	aUniqueContact = new Array();

aContact.sort();

for ( var i = 0 ; i < aContact.length ; i++ )
{
	if ( aContact[i] != nCurID )
	{
		aUniqueContact.push(aContact[i]);
		nCurID = aContact[i];
	}
}
// aUniqueContact will have the unique FK's

Hope this helps.

Hi Jim/Robert,

Not sure that this will work with duplicate keys in the array so an untested answer is to convert the array to a dataset and then load the records using that dataset.

Assuming that the original array is called ‘donation’ then you could have:

//Returns a foundset dataprovider (normally a column) as JavaScript array
var donation = databaseManager.getFoundSetDataProviderAsArray(forms.report,'fk_contact');

//converts an array to a dataset
var dataset = databaseManager.convertToDataSet(donation);

//load records using the dataset
forms.contact.controller.loadRecords(dataset)

Cheers
Harry

Hi Harry,

Also untested but I am pretty sure that having duplicate keys will give you duplicate records in your foundset.

Hi Robert,

Guess it comes down to someone testing it then :lol:

I am just curious enough to want to know so I’ll have a go and get back to you (and Jim !)

Cheers
Harry

I did the following test and got duplicate records.
Tested this in 2.2.7.

var ds = databaseManager.convertToDataSet([17,17,18,18,19,19])
controller.loadRecords(ds);

That was my experience, as well (duplicate records). I was hoping that there was an argument of the function that would constrain to unique values. I’m pretty sure there such a thing in getDataSetByQuery, but perhaps not in getFoundSetDataProviderAsArray.

Is it possible to have a an argument for getFoundSetDataProviderAsArray for “unique value”? Worth a feature request?

Jim

p.s. Robert, your approach looks like a good solution! Thanks also to Harry!

FWIW, Robert’s code + Harry’s code =

var aContact = databaseManager.getFoundSetDataProviderAsArray(forms.search.foundset,‘fk_contact’);
nCurID = null,
aUniqueContact = new Array();
aContact.sort();
for ( var i = 0 ; i < aContact.length ; i++ )
{
if ( aContact != nCurID )

  • {*
    _ aUniqueContact.push(aContact*);_
    _ nCurID = aContact;
    }
    }*
    var dataSet = databaseManager.convertToDataSet(aUniqueContact)
    forms.contact.controller.loadRecords(dataSet)[/quote]
    and it works as expected._

Hi Guys,

Yes, tested myself with same result.

Jim, always worth putting a feature request in.

Cheers
Harry

Hey, how do I get the indents to show in quotes?

(Compare quotes by Robert & Harry with mine; formatting was not preserved…)

Thanks,

Jim

I guess it’s the difference in used tags. we used code, you used quote.

Thanks!