Hello Eitan,
Want to give you a shout out and say you have an awesome YouTube channel! Helped me resolve issues several times. Thank you!
I am having a problem querying the Contacts collection. Basically, I created two custom fields called 'Group Name' and 'Allow Text Messages'. All I want to do is query the contacts collection and return the associated phone number for each item returned where the Group Name is a parameter to the function ("something like Communications would be the value") and the Allow Text Messages value can be hard-coded in the function like const allowTextMessages= "YES".
I could query the contacts using the default values as a parameter but I cannot figure out how to use the custom fields or extended fields.
ChatGPT just keeps send me hulicinations and it get worse each try.
Thank you for your help. Hope those sirens in NY aren't keeping you up at night.....
David Washington
Hi David,
Thanks so much for supporting the channel!
I'm sorry I couldn't help you with this one but I'm happy you figured it out. Thanks for sharing the answer!
(This often happens to me too - sometimes putting the problem in words gets the creative juices flowing)
I think the custom fields are a common challenge, so chances are you'll see a tutorial on this topic soon!
Best,
Eitan
Eitan,
After tinkering with the JSON and finding the exact names of the Extended Fields I figured it out.
So if you create a custom field in the Contacts (i created "Allow Text Messages" and "Group Name") you have to get the field key which is custom.allowsms and custom.group-name found by using the function queryExtendedFields. You also have to look at the JSON and make sure you use the entire identifier info.extendedFields.custom.allowsms and info.extendedFields.custom.group-name.
The code looks like this:
export async function getContactsForSMS(groupName) { const allowText = "YES" try { const queryresults = await contacts.queryContacts() .eq('info.extendedFields.custom.allowsms', allowText) .eq('info.extendedFields.custom.group-name', groupName) .find(); return queryresults.items; } catch (error) { console.error("Error querying contacts:", error); return []; } }