Query contacts with wix-crm-backend with custom field filter

Hi there,

I also have another issue with the standard contacts query function.
I manually added a custom field to the contacts:


How can I query contacts based on the “SponsorUserId” ?

. eq ( “info.extendedFields.custom.sponsorUserId” , userId )
or
. eq ( “info.extendedFields.sponsorUserId” , userId )
does not work…

Thanks for your help!!

Best regards,
Simon

See the API article Supported Query Filters for Extended Fields for details on how to query extended fields.

@yisrael-wix This is what I already did but there is no information how to get the exact key from the extended field which have been created manually within the administration UI.

I always get an error that the key is not supported when using the key mentioned in my post.

The documentation states:
Note: When working directly with the Contact object in your code, data in extended fields must be accessed using bracket notation, like this: info.extendedFields[“{key}”]. However, when querying or sorting, extended field names are flattened with dot notation, like this: ascending(“info.extendedFields.{key}”).

So, you probably need something like this:

.eq("info.extendedFields.{sponsorUserId}", userId)

Not sure if this helps but this is the code I have in my backend and have been using to query the Contacts:

export function getContactInfo ( memNum ) {
const options = {
suppressAuth : true
};

return contacts . queryContacts ()
. eq ( “info.extendedFields.custom.member-number” , memNum )
. find ( options )
. then (( queryResults ) => {
const items = queryResults . items ;
return items ;
})
. catch (( error ) => {
console . error ( error );
});
}

If you are testing on your live site and you do not have the suppressAuth setting correct, you won’t get results. Or if you have site membership setup and your user is not logged in.