Search.../

and( )

Adds an and condition to the query.

Description

The and() function adds an and condition to a ContactsQueryBuilder. A query with an and returns all the items that match the query as defined up to the and function and also match the query or filter passed to the and function.

Note that when chaining multiple ContactsQueryBuilder functions to a query an and condition is assumed. In such cases, you do not need to add a call to the and() function. For example, this query returns results where subscription status is not set and the contact was created after March 1, 2021.

contacts.queryContacts()
.eq("info.extendedFields.emailSubscriptions.subscriptionStatus", "NOT_SET")
.gt("_createdDate", "2021-03-01T00:00:00.000Z");
javascript | Copy Code

The and() function is needed when performing compound queries. For example, the final query in this set of queries returns results where subscription status is either not set or pending and creation date is earlier than than March 1, 2021 or the contact is labeled as a stale lead.

const subscriptionStatusQuery = contacts.queryContacts()
.eq("info.extendedFields.emailSubscriptions.subscriptionStatus", "NOT_SET")
.or(
contacts.queryContacts()
.eq("info.extendedFields.emailSubscriptions.subscriptionStatus", "PENDING")
);
const freshnessQuery = contacts.queryContacts()
.lt("_updatedDate", "2021-03-01T00:00:00.000Z")
.or(
contacts.queryContacts()
.hasSome("info.labelKeys", ["custom.stale-lead"])
);
const statusAndFreshnessQuery = subscriptionStatusQuery.and(freshnessQuery);
javascript | Copy Code

The and() function is designed to work with 2 or more queries or filters. If you use it on its own, it will return all the items that meet the query criteria.

Syntax

function and(query: ContactsQueryBuilder): ContactsQueryBuilder

and Parameters

NAME
TYPE
DESCRIPTION
query

A query to add to the initial query as an and condition.

Returns

A ContactsQueryBuilder object that contains the refined query.

Was this helpful?

Add an and filter to a query

Copy Code
1const newQuery = query1.and(query2);
Create a compound query, add an and filter, and run it

The first query, subscriptionStatusQuery, returns results where the contact's subscription status is either "NOT_SET" or "PENDING". The second query, freshnessQuery returns results where the contact was last updated before March 1, 2021, or where the contact is labeled as a stale lead.

The final query, statusAndFreshnessQuery, combines the first 2 queries with an and filter, and returns contacts that satisfy both queries.

Copy Code
1import { Permissions, webMethod } from 'wix-web-module';
2import { contacts } from 'wix-crm-backend';
3
4export const myQueryContactsFunction = webMethod(Permissions.Anyone, () => {
5 const subscriptionStatusQuery = contacts.queryContacts()
6 .eq("info.extendedFields.emailSubscriptions.subscriptionStatus", "NOT_SET")
7 .or(
8 contacts.queryContacts()
9 .eq("info.extendedFields.emailSubscriptions.subscriptionStatus", "PENDING")
10 );
11
12 const freshnessQuery = contacts.queryContacts()
13 .lt("_updatedDate", "2021-03-01T00:00:00.000Z")
14 .or(
15 contacts.queryContacts()
16 .hasSome("info.labelKeys", ["custom.stale-lead"])
17 );
18
19 const statusAndFreshnessQuery = subscriptionStatusQuery.and(freshnessQuery);
20
21 return statusAndFreshnessQuery.find()
22 .then((results) => {
23 if (results.items.length > 0) {
24 const items = results.items;
25 const firstItem = items[0];
26 const pageSize = results.pageSize;
27 const hasNext = results.hasNext();
28 const hasPrev = results.hasPrev();
29 const length = results.length;
30 const query = results.query;
31
32 return items;
33 } else {
34 // Handle case where no matching items found
35 }
36 })
37 .catch((error) => {
38 console.error(error);
39 })
40
41});