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.
javascript | Copy Codecontacts.queryContacts().eq("info.extendedFields.emailSubscriptions.subscriptionStatus", "NOT_SET").gt("_createdDate", "2021-03-01T00:00:00.000Z");
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.
javascript | Copy Codeconst 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);
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
Returns
A ContactsQueryBuilder
object that contains the refined query.
Return Type:
Was this helpful?
and
filter to a query1const newQuery = query1.and(query2);
and
filter, and run itThe 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.
1import { Permissions, webMethod } from 'wix-web-module';2import { contacts } from 'wix-crm-backend';34export 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 );1112 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 );1819 const statusAndFreshnessQuery = subscriptionStatusQuery.and(freshnessQuery);2021 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;3132 return items;33 } else {34 // Handle case where no matching items found35 }36 })37 .catch((error) => {38 console.error(error);39 })4041});