queryContacts( )
Creates a query to retrieve a list of contacts.
Description
The queryContacts()
function builds a query to retrieve a list of contacts
and returns a
ContactsQueryBuilder
object.
The returned object contains the query definition,
which is typically used to run the query using the
find()
function.
You can refine the query
by chaining ContactsQueryBuilder
functions onto the query.
ContactsQueryBuilder
functions enable you to sort, filter,
and control the results queryContacts()
returns.
queryContacts()
runs with these ContactsQueryBuilder
defaults,
which you can override:
The functions that are chained to queryContacts()
are applied in the order they are called.
For example, if you apply ascending('info.company')
and then descending('info.name.last')
,
the results are sorted first by the company name, and then,
if there are multiple results with the same company,
the items are sorted by last name.
Note: Only visitors with Manage Contacts permissions can query contacts. You can override the permissions by setting the
suppressAuth
option totrue
in thefind()
function.
For property support for filters and sorting, see Query contacts: Supported filters, sorting, and search.
Syntax
function queryContacts(): ContactsQueryBuilder
queryContacts Parameters
This function does not take any parameters.
Returns
A ContactsQueryBuilder
object that contains the refined query.
Return Type:
Was this helpful?
1import { contacts } from 'wix-crm-backend';23/* Sample options value:4 * {5 * suppressAuth: true6 * }7 */89export async function myQueryContactsFunction(options) {10 try {1112 const queryResults = await contacts.queryContacts().find(options);13 const items = queryResults.items;14 const firstItem = items[0];15 const pageSize = queryResults.pageSize;16 const hasNext = queryResults.hasNext();17 const hasPrev = queryResults.hasPrev();18 const length = queryResults.length;19 const query = queryResults.query;2021 return items;2223 } catch (error) {2425 console.error(error);26 // Handle the error2728 }29}3031/* Returns items:32 * [33 * {34 * "_id": "f1a481a0-ec7d-48e4-b578-80535bac3e58",35 * "_createdDate": "2021-04-08T14:15:46.428Z",36 * "_updatedDate": "2021-04-08T14:17:01.628Z",37 * "revision": 4,38 * "primaryInfo": {39 * "email": "tom.watts@example.com",40 * "phone": "0769-469-693"41 * },42 * "info": {43 * "name": {44 * "first": "Tom",45 * "last": "Watts"46 * },47 * "profilePicture": "https://wixmp-8be454c954980f083caba37c.wixmp.com/sites/7b940519-404c-4972-9f03-9a430b68d52c/f1a481a0-ec7d-48e4-b578-80535bac3e58/avatar.jpeg?token=eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJ1cm46YXBwOmY1ODg2NzI1MzJlMzQxZmFhZDQwZjFiMDc3MTZhMjU3IiwiYXVkIjpbInVybjpzZXJ2aWNlOmZpbGUuZG93bmxvYWQiXSwib2JqIjpbW3sicGF0aCI6Ii9zaXRlcy83Yjk0MDUxOS00MDRjLTQ5NzItOWYwMy05YTQzMGI2OGQ1MmMvZjFhNDgxYTAtZWM3ZC00OGU0LWI1NzgtODA1MzViYWMzZTU4L2F2YXRhci5qcGVnIn1dXSwiaXNzIjoidXJuOmFwcDpmNTg4NjcyNTMyZTM0MWZhYWQ0MGYxYjA3NzE2YTI1NyIsImV4cCI6MTYxNzg5MjE0NCwiaWF0IjoxNjE3ODkxNTM0LCJqdGkiOiIyQzg3NkZFMDNDNDUifQ.iJxQyNnvpHMLhDiZ-l5hFLr6vMnT10r2Ly6X25U-v-0",48 * "addresses": [49 * {50 * "address": {51 * "formatted": "1105 Albert Road\nCoventry, England\nTZ7 3JQ\nUnited Kingdom",52 * "city": "Coventry",53 * "subdivision": "GB-ENG",54 * "country": "GB",55 * "postalCode": "TZ7 3JQ",56 * "addressLine1": "1105 Albert Road"57 * },58 * "_id": "be7eb0fd-78b7-4e3e-ac5e-483925dc9221",59 * "tag": "HOME"60 * }61 * ],62 * "emails": [63 * {64 * "_id": "09ea3abc-02e0-44c2-a139-f68ce70b9dcc",65 * "tag": "MAIN",66 * "email": "tom.watts@example.com",67 * "primary": true68 * }69 * ],70 * "phones": [71 * {72 * "_id": "8c8d0f51-41fa-48a1-a68e-739d75fb64e0",73 * "tag": "MOBILE",74 * "phone": "0769-469-693",75 * "primary": true76 * }77 * ],78 * "extendedFields": {79 * "emailSubscriptions.deliverabilityStatus": "NOT_SET",80 * "emailSubscriptions.subscriptionStatus": "UNSUBSCRIBED",81 * "contacts.displayByFirstName": "Tom Watts",82 * "contacts.displayByLastName": "Watts Tom"83 * }84 * },85 * "lastActivity": {86 * "activityDate": "2021-04-08T14:17:01.211Z",87 * "activityType": "INBOX_MESSAGE_TO_CUSTOMER"88 * },89 * "source": {90 * "sourceType": "ADMIN"91 * }92 * },93 * {94 * "_id": "bb9d8ff5-aa46-483c-ad22-077927b28092",95 * "_createdDate": "2021-04-08T13:58:32.258Z",96 * "_updatedDate": "2021-04-08T14:03:56.405Z",97 * "revision": 2,98 * "primaryInfo": {99 * "email": "victoria.patterson@example.com",100 * "phone": "013873 00176"101 * },102 * "info": {103 * "name": {104 * "first": "Victoria",105 * "last": "Patterson"106 * },107 * "profilePicture": "https://wixmp-8be454c954980f083caba37c.wixmp.com/sites/7b940519-404c-4972-9f03-9a430b68d52c/bb9d8ff5-aa46-483c-ad22-077927b28092/avatar.jpeg?token=eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJ1cm46YXBwOmY1ODg2NzI1MzJlMzQxZmFhZDQwZjFiMDc3MTZhMjU3IiwiYXVkIjpbInVybjpzZXJ2aWNlOmZpbGUuZG93bmxvYWQiXSwib2JqIjpbW3sicGF0aCI6Ii9zaXRlcy83Yjk0MDUxOS00MDRjLTQ5NzItOWYwMy05YTQzMGI2OGQ1MmMvYmI5ZDhmZjUtYWE0Ni00ODNjLWFkMjItMDc3OTI3YjI4MDkyL2F2YXRhci5qcGVnIn1dXSwiaXNzIjoidXJuOmFwcDpmNTg4NjcyNTMyZTM0MWZhYWQ0MGYxYjA3NzE2YTI1NyIsImV4cCI6MTYxNzg5MjE0NCwiaWF0IjoxNjE3ODkxNTM0LCJqdGkiOiIyNTIwNTZEQkExNTEifQ._4JAr9X_g6yPHj_N4JG3OKk33ZbveMMyPbx2D5U2Z4E",108 * "addresses": [109 * {110 * "address": {111 * "formatted": "4109 London Road\nArmagh, Northern Ireland\nM0N 4GU\nUnited Kingdom",112 * "city": "Armagh",113 * "subdivision": "GB-NIR",114 * "country": "GB",115 * "postalCode": "M0N 4GU",116 * "addressLine1": "4109 London Road"117 * },118 * "_id": "9e5afe34-6919-432b-bd01-91ee6ead9121",119 * "tag": "UNTAGGED"120 * }121 * ],122 * "emails": [123 * {124 * "_id": "af757e47-df85-4815-8c99-f1e11033fcc6",125 * "tag": "UNTAGGED",126 * "email": "victoria.patterson@example.com",127 * "primary": true128 * }129 * ],130 * "phones": [131 * {132 * "_id": "a590ae95-8f06-4d71-afcd-1f6f4b5de1b9",133 * "tag": "HOME",134 * "phone": "013873 00176",135 * "primary": true136 * },137 * {138 * "_id": "d6370be9-386d-4362-add0-b464b818a942",139 * "tag": "MOBILE",140 * "phone": "0753-893-447",141 * "primary": false142 * }143 * ],144 * "labelKeys": [145 * "custom.incoming-leads"146 * ],147 * "extendedFields": {148 * "contacts.displayByLastName": "Patterson Victoria",149 * "emailSubscriptions.deliverabilityStatus": "NOT_SET",150 * "emailSubscriptions.subscriptionStatus": "NOT_SET",151 * "emailSubscriptions.effectiveEmail": "victoria.patterson@example.com",152 * "contacts.displayByFirstName": "Victoria Patterson"153 * }154 * },155 * "lastActivity": {156 * "activityDate": "2021-04-08T13:58:32.256Z",157 * "activityType": "CONTACT_CREATED"158 * },159 * "source": {160 * "sourceType": "ADMIN"161 * }162 * },163 * {164 * "_id": "a52852d7-e0d6-484b-93fe-bffdb670f08f",165 * "_createdDate": "2021-03-30T20:18:06.874Z",166 * "_updatedDate": "2021-03-31T20:24:12.312Z",167 * "revision": 5,168 * "info": {169 * "name": {170 * "first": "Ari",171 * "last": "Thereyet"172 * },173 * "extendedFields": {174 * "contacts.displayByLastName": "Thereyet Ari",175 * "emailSubscriptions.deliverabilityStatus": "NOT_SET",176 * "emailSubscriptions.subscriptionStatus": "NOT_SET",177 * "emailSubscriptions.effectiveEmail": "ari.thereyet.appended.email@example.com",178 * "contacts.displayByFirstName": "Ari Thereyet"179 * },180 * "emails": [181 * {182 * "_id": "e6f2733e-32b8-4d99-95ce-7d2e63a39108",183 * "tag": "HOME",184 * "email": "ari.thereyet@example.com",185 * "primary": true186 * },187 * {188 * "_id": "703f6faf-7d88-42ce-8e28-54f8d5f6e5e4",189 * "tag": "UNTAGGED",190 * "email": "ari.thereyet.appended.email@example.com",191 * "primary": false192 * }193 * ],194 * "phones": [195 * {196 * "_id": "970380bb-57e0-4b3a-8f40-ccc93be1ab58",197 * "tag": "MOBILE",198 * "phone": "601-081-124",199 * "primary": true200 * }201 * ],202 * "labelKeys": [203 * "custom.stale-lead"204 * ]205 * },206 * "lastActivity": {207 * "activityDate": "2021-03-30T20:18:06.872Z",208 * "activityType": "CONTACT_CREATED"209 * },210 * "primaryInfo": {211 * "email": "ari.thereyet@example.com",212 * "phone": "601-081-124"213 * },214 * "source": {215 * "sourceType": "WIX_APP"216 * }217 * },218 * {219 * "_id": "bc0ae72b-3285-485b-b0ad-c32c769a4daf",220 * "_createdDate": "2021-03-30T13:12:39.650Z",221 * "_updatedDate": "2021-04-07T21:03:39.481Z",222 * "revision": 2,223 * "primaryInfo": {224 * "email": "gene.lopez.at.home@example.com",225 * "phone": "(722)-138-3099"226 * },227 * "info": {228 * "name": {229 * "first": "Gene",230 * "last": "Lopez"231 * },232 * "birthdate": "1981-11-02",233 * "jobTitle": "Senior Staff Attorney",234 * "company": "Borer and Sons, Attorneys at Law",235 * "locale": "en-us",236 * "profilePicture": "https://randomuser.me/api/portraits/men/0.jpg",237 * "emails": [238 * {239 * "_id": "5bdcce4a-37c2-46ed-b49c-d562c6e3c4ce",240 * "tag": "HOME",241 * "email": "gene.lopez.at.home@example.com",242 * "primary": true243 * },244 * {245 * "_id": "78e5f398-e148-448d-b490-7c0b7d2ab336",246 * "tag": "WORK",247 * "email": "gene.lopez@example.com",248 * "primary": false249 * }250 * ],251 * "phones": [252 * {253 * "_id": "820e4640-ffe0-4980-a097-62a715e73135",254 * "tag": "MOBILE",255 * "countryCode": "US",256 * "phone": "(722)-138-3099",257 * "primary": true258 * },259 * {260 * "_id": "8506549e-e4f8-42f6-b6fc-9db155b582ef",261 * "tag": "HOME",262 * "countryCode": "US",263 * "phone": "(704)-454-1233",264 * "e164Phone": "+17044541233",265 * "primary": false266 * }267 * ],268 * "addresses": [269 * {270 * "address": {271 * "formatted": "9834 Bollinger Rd\nEl Cajon, WY 97766\nUS",272 * "location": {273 * "latitude": 84.1048,274 * "longitude": -116.8836275 * },276 * "city": "El Cajon",277 * "subdivision": "US-WY",278 * "country": "US",279 * "postalCode": "97766",280 * "streetAddress": {281 * "name": "Bollinger Rd",282 * "number": "9834",283 * "apt": ""284 * }285 * },286 * "_id": "8532051f-91f2-42d9-9a97-9f2c39e64f7a",287 * "tag": "HOME"288 * }289 * ],290 * "extendedFields": {291 * "contacts.displayByLastName": "Lopez Gene",292 * "emailSubscriptions.deliverabilityStatus": "NOT_SET",293 * "emailSubscriptions.subscriptionStatus": "NOT_SET",294 * "custom.event-we-met-at": "LegalBigData",295 * "emailSubscriptions.effectiveEmail": "gene.lopez.at.home@example.com",296 * "contacts.displayByFirstName": "Gene Lopez"297 * },298 * "labelKeys": [299 * "contacts.contacted-me",300 * "custom.new-lead"301 * ]302 * },303 * "lastActivity": {304 * "activityDate": "2021-03-30T13:12:39.649Z",305 * "activityType": "CONTACT_CREATED"306 * },307 * "source": {308 * "sourceType": "OTHER"309 * }310 * },311 * {312 * "_id": "41b1c8e7-5ddd-4186-ad85-269985d6b296",313 * "_createdDate": "2021-03-29T21:16:54.801Z",314 * "_updatedDate": "2021-03-30T20:14:16.256Z",315 * "revision": 3,316 * "primaryInfo": {317 * "email": "renata.kirsten@example.com",318 * "phone": "0177-3234750"319 * },320 * "info": {321 * "name": {322 * "first": "Renata",323 * "last": "Kirsten"324 * },325 * "emails": [326 * {327 * "_id": "500569e4-144d-4f25-83a1-4a59956b698d",328 * "tag": "UNTAGGED",329 * "email": "renata.kirsten@example.com",330 * "primary": true331 * }332 * ],333 * "phones": [334 * {335 * "_id": "3ea63681-598a-45bc-88fb-97fefdfd5a74",336 * "tag": "UNTAGGED",337 * "phone": "0177-3234750",338 * "primary": true339 * }340 * ],341 * "addresses": [342 * {343 * "address": {344 * "formatted": "6677 Schlossstraße\n48435 Wittenberg\nGermany",345 * "city": "Wittenberg",346 * "subdivision": "DE-TH",347 * "country": "DE",348 * "postalCode": "48435",349 * "addressLine1": "6677 Schlossstraße"350 * },351 * "_id": "b36d1a2d-9a6b-48c3-a463-c8b116303e9f",352 * "tag": "SHIPPING"353 * },354 * {355 * "address": {356 * "formatted": "6677 Schlossstraße\n48435 Wittenberg\nGermany",357 * "city": "Wittenberg",358 * "subdivision": "DE-TH",359 * "country": "DE",360 * "postalCode": "48435",361 * "addressLine1": "6677 Schlossstraße"362 * },363 * "_id": "2a3fc791-cbad-4ecd-8bf6-8072b2793e3a",364 * "tag": "BILLING"365 * }366 * ],367 * "labelKeys": [368 * "contacts.customers"369 * ],370 * "extendedFields": {371 * "contacts.displayByLastName": "Kirsten Renata",372 * "emailSubscriptions.deliverabilityStatus": "NOT_SET",373 * "ecom.lastPurchaseDate": "2021-03-29T21:17:01.122Z",374 * "ecom.numOfPurchases": 1,375 * "emailSubscriptions.subscriptionStatus": "NOT_SET",376 * "ecom.totalSpentAmount": 120,377 * "emailSubscriptions.effectiveEmail": "renata.kirsten@example.com",378 * "contacts.displayByFirstName": "Renata Kirsten",379 * "ecom.totalSpentCurrency": "ILS"380 * }381 * },382 * "lastActivity": {383 * "activityDate": "2021-03-29T21:17:01.122Z",384 * "activityType": "ECOM_PURCHASE"385 * },386 * "source": {387 * "appId": "1380b703-ce81-ff05-f115-39571d94dfcd",388 * "sourceType": "WIX_STORES"389 * }390 * },391 * {392 * "_id": "67bcea88-b748-4cc8-a584-bb991b6e5d82",393 * "_createdDate": "2021-03-29T21:06:53.976Z",394 * "_updatedDate": "2021-03-29T21:06:53.976Z",395 * "revision": 0,396 * "primaryInfo": {397 * "email": "ljiljana.francois@example.com"398 * },399 * "info": {400 * "name": {401 * "first": "Ljiljana",402 * "last": "Francois"403 * },404 * "birthdate": "1981-09-13",405 * "jobTitle": "Creative Director",406 * "company": "Happy Mouse Productions",407 * "locale": "en-us",408 * "emails": [409 * {410 * "_id": "c030ed4c-e127-4f02-8256-8984415e0321",411 * "tag": "MAIN",412 * "email": "ljiljana.francois@example.com",413 * "primary": true414 * }415 * ],416 * "extendedFields": {417 * "contacts.displayByLastName": "Francois Ljiljana",418 * "emailSubscriptions.deliverabilityStatus": "NOT_SET",419 * "emailSubscriptions.subscriptionStatus": "NOT_SET",420 * "emailSubscriptions.effectiveEmail": "ljiljana.francois@example.com",421 * "contacts.displayByFirstName": "Ljiljana Francois"422 * }423 * },424 * "lastActivity": {425 * "activityDate": "2021-03-29T21:06:53.975Z",426 * "activityType": "CONTACT_CREATED"427 * },428 * "source": {429 * "sourceType": "OTHER"430 * }431 * },432 * {433 * "_id": "1626081c-3b3c-4b44-855b-1acb1122e080",434 * "_createdDate": "2021-03-29T20:51:21.216Z",435 * "_updatedDate": "2021-03-30T11:50:42.040Z",436 * "revision": 6,437 * "primaryInfo": {438 * "email": "celine.rousseau@example.com",439 * "phone": "0177-7912572"440 * },441 * "info": {442 * "name": {443 * "first": "Celine",444 * "last": "Rousseau"445 * },446 * "locale": "en",447 * "emails": [448 * {449 * "tag": "UNTAGGED",450 * "email": "celine.rousseau@example.com",451 * "primary": true452 * }453 * ],454 * "phones": [455 * {456 * "tag": "UNTAGGED",457 * "phone": "0177-7912572",458 * "primary": true459 * }460 * ],461 * "addresses": [462 * {463 * "address": {464 * "formatted": "2451Rue des Cuirassiers\n9502 Grosshöchstetten\nSwitzerland",465 * "city": "Grosshöchstetten",466 * "subdivision": "CH-VD",467 * "country": "CH",468 * "postalCode": "9502",469 * "addressLine1": "2451Rue des Cuirassiers"470 * },471 * "tag": "SHIPPING"472 * },473 * {474 * "address": {475 * "formatted": "2451Rue des Cuirassiers\n9502 Grosshöchstetten\nSwitzerland",476 * "city": "Grosshöchstetten",477 * "subdivision": "CH-VD",478 * "country": "CH",479 * "postalCode": "9502",480 * "addressLine1": "2451Rue des Cuirassiers"481 * },482 * "tag": "BILLING"483 * },484 * {485 * "address": {486 * "formatted": "Israel",487 * "country": "IL"488 * },489 * "tag": "UNTAGGED"490 * }491 * ],492 * "labelKeys": [493 * "contacts.customers"494 * ],495 * "extendedFields": {496 * "contacts.displayByLastName": "Rousseau Celine",497 * "emailSubscriptions.deliverabilityStatus": "BOUNCED",498 * "ecom.lastPurchaseDate": "2021-03-29T21:00:35.835Z",499 * "members.membershipStatus": "APPROVED",500 * "ecom.numOfPurchases": 2,501 * "emailSubscriptions.subscriptionStatus": "NOT_SET",502 * "ecom.totalSpentAmount": 27.5,503 * "members.mobile": false,504 * "contacts.displayByFirstName": "Celine Rousseau",505 * "ecom.totalSpentCurrency": "ILS"506 * }507 * },508 * "lastActivity": {509 * "activityDate": "2021-03-29T20:58:47.548Z",510 * "activityType": "MEMBER_STATUS_CHANGED"511 * },512 * "source": {513 * "appId": "1380b703-ce81-ff05-f115-39571d94dfcd",514 * "sourceType": "WIX_STORES"515 * }516 * },517 * {518 * "_id": "b508466c-dcd3-4284-917f-3ff678762940",519 * "_createdDate": "2021-03-29T17:19:43.786Z",520 * "_updatedDate": "2021-03-29T18:04:02.082Z",521 * "revision": 3,522 * "primaryInfo": {523 * "email": "zackary.sirko@example.com",524 * "phone": "796-685-0846"525 * },526 * "info": {527 * "name": {528 * "first": "Zackary",529 * "last": "Sirko"530 * },531 * "emails": [532 * {533 * "_id": "00a88829-c21c-46b9-ab28-aaaae7f2f057",534 * "tag": "UNTAGGED",535 * "email": "zackary.sirko@example.com",536 * "primary": true537 * }538 * ],539 * "phones": [540 * {541 * "_id": "2f68e6c0-10b5-4c4c-b117-f2d7f8663475",542 * "tag": "UNTAGGED",543 * "phone": "796-685-0846",544 * "primary": true545 * }546 * ],547 * "addresses": [548 * {549 * "address": {550 * "formatted": "8768 King St\nCumberland, Z1D 6P8\nCanada",551 * "city": "Cumberland",552 * "country": "CA",553 * "postalCode": "Z1D 6P8",554 * "addressLine1": "8768 King St"555 * },556 * "_id": "816f720d-f329-4774-90f7-1d5c75936e47",557 * "tag": "UNTAGGED"558 * }559 * ],560 * "extendedFields": {561 * "contacts.displayByLastName": "Sirko Zackary",562 * "emailSubscriptions.deliverabilityStatus": "NOT_SET",563 * "members.membershipStatus": "APPROVED",564 * "emailSubscriptions.subscriptionStatus": "NOT_SET",565 * "members.mobile": false,566 * "emailSubscriptions.effectiveEmail": "zackary.sirko@example.com",567 * "contacts.displayByFirstName": "Zackary Sirko"568 * }569 * },570 * "lastActivity": {571 * "activityDate": "2021-03-29T17:19:44.747Z",572 * "activityType": "MEMBER_REGISTER"573 * },574 * "source": {575 * "sourceType": "ADMIN"576 * }577 * },578 * {579 * "_id": "73904dbd-94ca-4ac1-a593-ef0627673e59",580 * "_createdDate": "2021-03-29T13:17:19.169Z",581 * "_updatedDate": "2021-03-29T15:16:40.971Z",582 * "revision": 5,583 * "primaryInfo": {584 * "email": "ines.brunet@example.com",585 * "phone": "01-27-29-38-74"586 * },587 * "info": {588 * "name": {589 * "first": "Inès",590 * "last": "Brunet"591 * },592 * "profilePicture": "https://www.wix.com/_api/contacts/v4/media-gateway/redirect/JWE.eyJhbGciOiJBMTI4S1ciLCJlbmMiOiJBMTI4Q0JDLUhTMjU2Iiwia2lkIjoiUTBub2RfZ1gifQ.44GkOpjoCNjHRGpKesfyoDZEPxnmEGaqBw-6Vl_YZum8VLFKf274lQ.3K4URkUMNfHgdLs56Ub1Dg.bu3tcHR1VDqaRvcVU3dxlYIL5u0K2TDWYE9XVasxOq02r5FOeBzw1mZ_TGlOmYv0jIhffFXU7IhTUWzZIfGholGsjCVe5KRYzUyx4D2jQPh4sERYxLKluD6f3J6-yHEuhH_UWu9GiZynqRHD3SCsjpLbewnQDM0BxJdXnze0qGakJl44KhCdvRpEtUz9aboemu1WwEHc3sj5UQVHA78LDw.91QdN1ZuB6L7PyLppMCrcg",593 * "emails": [594 * {595 * "_id": "114a1f7f-18a6-4a90-b5a7-972e763337fb",596 * "tag": "WORK",597 * "email": "ines.brunet@example.com",598 * "primary": true599 * }600 * ],601 * "phones": [602 * {603 * "_id": "6268e2b9-1cbf-486d-b986-10a08af53a8a",604 * "tag": "HOME",605 * "phone": "01-27-29-38-74",606 * "primary": true607 * },608 * {609 * "_id": "f8da8843-7e52-4010-9e40-d21821a1745f",610 * "tag": "MOBILE",611 * "phone": "06-48-73-26-70",612 * "primary": false613 * }614 * ],615 * "addresses": [616 * {617 * "address": {618 * "formatted": "8416 E Sandy Lake Rd\n69922, Ardennes\nFrance",619 * "city": "Ardennes",620 * "country": "FR",621 * "postalCode": "69922",622 * "addressLine1": "8416 E Sandy Lake Rd"623 * },624 * "_id": "4fa3e416-8924-4125-8922-0db7fe960666",625 * "tag": "UNTAGGED"626 * }627 * ],628 * "labelKeys": [629 * "custom.marketing-your-business-on-social-media",630 * "contacts.contacted-me"631 * ],632 * "extendedFields": {633 * "contacts.displayByLastName": "Brunet Inès",634 * "emailSubscriptions.deliverabilityStatus": "NOT_SET",635 * "emailSubscriptions.subscriptionStatus": "SUBSCRIBED",636 * "emailSubscriptions.effectiveEmail": "ines.brunet@example.com",637 * "contacts.displayByFirstName": "Inès Brunet"638 * }639 * },640 * "lastActivity": {641 * "activityDate": "2021-03-29T13:17:19.166Z",642 * "activityType": "CONTACT_CREATED"643 * },644 * "source": {645 * "sourceType": "ADMIN"646 * }647 * }648 * ]649 */
You can use queryContacts()
to find a contact with specified details, and then extract the contact's ID.
With the contact ID, you can use emailContact()
to send a triggered email.
1import { contacts, triggeredEmails } from 'wix-crm-backend';23/* Sample emailToFind value:4 * 'zackary.sirko@example.com'5 */67export async function sendEmailToContact(emailToFind) {89 let contactId;10 const queryResults = await contacts.queryContacts()11 .eq('info.emails.email', emailToFind)12 .find();13 const contactsWithEmail = queryResults.items;1415 if (contactsWithEmail.length === 1) {1617 console.log('Found 1 contact');18 contactId = contactsWithEmail[0]._id;1920 } else if (contactsWithEmail.length > 1) {2122 console.log('Found more than 1 contact');23 // Handle when more than one contact is found2425 } else {2627 console.log('No contacts found');28 // Handle when no contacts are found2930 }3132 const triggeredEmailTemplate = 'welcome_email';3334 try {35 await triggeredEmails.emailContact(triggeredEmailTemplate, contactId);36 console.log('Email sent to contact');37 } catch (error) {38 console.error(error);39 // Handle the error40 }41}42
1import { contacts } from 'wix-crm-backend';23export async function getRecentNewYorkMembers() {4 try {56 const queryResults = await contacts.queryContacts()7 .gt('_createdDate', new Date('2022-01-01T00:00:00.000Z'))8 .eq('info.addresses.city', 'New York')9 .eq('info.extendedFields.members.membershipStatus', 'APPROVED')10 .limit(25)11 .find()1213 return queryResults;1415 } catch (error) {1617 console.error(error);18 // Handle the error1920 }21}2223/* Returns items:24 * [25 * {26 * "_createdDate": "2022-02-01T13:13:50.963Z",27 * "_id": "f29baf24-943f-4402-b7cc-5658ed5f8867",28 * "info": {29 * "addresses": [30 * {31 * "address": {32 * "formatted": "9267 Adams St\nNew York, New York 10002\nUnited States",33 * "city": "New York",34 * "subdivision": "US-NY",35 * "country": "US",36 * "postalCode": "10002",37 * "addressLine1": "9267 Adams St"38 * },39 * "_id": "3586b551-92ef-49cf-88f4-17790369afa3",40 * "tag": "UNTAGGED"41 * }42 * ],43 * "emails": [44 * {45 * "_id": "eb6e1d0a-a54f-4612-af60-3ad15894927f",46 * "tag": "UNTAGGED",47 * "email": "sergiop565@example.com",48 * "primary": true49 * }50 * ],51 * "extendedFields": {52 * "emailSubscriptions.deliverabilityStatus": "NOT_SET",53 * "members.membershipStatus": "APPROVED",54 * "emailSubscriptions.subscriptionStatus": "NOT_SET",55 * "emailSubscriptions.effectiveEmail": "sergiop565@example.com",56 * },57 * "name": {58 * "first": "Sergio",59 * "last": "Patterson"60 * },61 * "phones": [62 * {63 * "_id": "b3fc65c7-1870-4458-a989-d6f9fdb97c86",64 * "tag": "UNTAGGED",65 * "phone": "(212)-665-2602",66 * "formattedPhone": "(212)-665-2602",67 * "primary": true68 * }69 * ]70 * },71 * "lastActivity": {72 * "activityDate": "2022-02-01T13:13:51.364Z",73 * "activityType": "MEMBER_REGISTER"74 * },75 * "primaryInfo": {76 * "email": "sergiop565@example.com",77 * "phone": "(212)-665-2602"78 * },79 * "revision": 2,80 * "source": {81 * "appId": "eb377299-86b4-4a86-a1b5-774a2d1d374b",82 * "sourceType": "WIX_SITE_MEMBERS"83 * },84 * "_updatedDate": "2022-02-03T13:13:41.263Z"85 * },86 * {87 * "_createdDate": "2022-02-01T13:24:27.733Z",88 * "_id": "315cb333-b8cd-4f03-a772-d3614dbcc282",89 * "info": {90 * "addresses": [91 * {92 * "address": {93 * "formatted": "6489 E Sandy Lake Rd\nNew York, 10011\nUnited States",94 * "city": "New York",95 * "country": "US",96 * "postalCode": "10011",97 * "addressLine1": "6489 E Sandy Lake Rd"98 * },99 * "_id": "efd7e4e8-e515-41a4-ba85-4bcdf1449a51",100 * "tag": "UNTAGGED"101 * }102 * ],103 * "emails": [104 * {105 * "_id": "6a8b204a-dbbf-4834-98e9-139701f535ff",106 * "tag": "UNTAGGED",107 * "email": "megan.moreno@example.com",108 * "primary": true109 * }110 * ],111 * "extendedFields": {112 * "emailSubscriptions.deliverabilityStatus": "NOT_SET",113 * "members.membershipStatus": "APPROVED",114 * "emailSubscriptions.subscriptionStatus": "NOT_SET",115 * "emailSubscriptions.effectiveEmail": "megan.moreno@example.com",116 * },117 * "name": {118 * "first": "Megan",119 * "last": "Moreno"120 * },121 * "phones": [122 * {123 * "_id": "21b2363e-5cc5-4071-b5ac-16697c4d8872",124 * "tag": "UNTAGGED",125 * "phone": "(266)-333-9487",126 * "formattedPhone": "(266)-333-9487",127 * "primary": true128 * }129 * ]130 * },131 * "lastActivity": {132 * "activityDate": "2022-02-01T13:24:28.139Z",133 * "activityType": "MEMBER_REGISTER"134 * },135 * "primaryInfo": {136 * "email": "megan.moreno@example.com",137 * "phone": "(266)-333-9487"138 * },139 * "revision": 2,140 * "source": {141 * "appId": "eb377299-86b4-4a86-a1b5-774a2d1d374b",142 * "sourceType": "WIX_SITE_MEMBERS"143 * },144 * "_updatedDate": "2022-02-03T13:10:30.174Z"145 * }146 * ]147 */