Search.../

appendOrCreateContact( )

Appends an existing contact or creates a contact if it doesn't exist.

Description

The appendOrCreateContact() function returns a Promise that resolves to a contact ID and identity type when a contact is found or created.

Note: This function replaces the deprecated wixCrm.createContact(). The deprecated function will continue to work, but it will not receive updates. To keep any existing code compatible with future changes, see the migration instructions.

When called, appendOrCreateContact() accepts an object with contact information and follows the steps below. When one of the conditions is met, this process ends and the specified data is handled:

  1. If the current visitor is a logged-in member: appendOrCreateContact() reconciles with the member's associated contact.
  2. If the visitor is not a logged-in member: appendOrCreateContact() tries to reconcile any specified email addresses and/or phone numbers with an existing contact.
  3. If no contact can be reconciled in the previous steps: appendOrCreateContact() tries to reconcile the visitor's session identity with an existing contact.
  4. If no contact can be reconciled and an email address, phone number, or name are provided: A new contact is created and appendOrCreateContact() reconciles with the new contact.
  5. If no contact can be created: The Promise is rejected.

appendOrCreateContact() does not require member authentication, so it does not return the entire contact object. The contact's data can be retrieved with getContact() from wix-crm-backend.

How the Data Is Handled

If an existing contact is found:

  • The current visitor's session identity is associated with the contact, if it isn't already.
  • If a specified property is empty in the existing contact, the property is updated with the specified information.
  • For arrays, any specified information is added as new array items. This includes emails, addresses, phones, and labelKeys.
  • No existing data is overwritten or deleted.

If a new contact is created:

  • The new contact's data is populated with the specified contact object.
  • The current visitor's session identity is associated with the contact.

Syntax

function appendOrCreateContact(contactInfo: ContactInfo): ContactIdentification

appendOrCreateContact Parameters

NAME
TYPE
DESCRIPTION
contactInfo

ContactInfo

Contact's information. To reconcile with an existing contact, a phone or email must be provided, or the current session identity must be attached to a contact or member.

If no existing contact can be found, a new contact is created with the specified information.

Returns

Return Type:

Promise<ContactIdentification>
NAME
TYPE
DESCRIPTION
contactId

string

ID of the contact that was found or created.

identityType

string

Identity type of the returned contact.

One of:

  • "CONTACT": The returned contact ID belongs to a new or existing contact.
  • "MEMBER": The returned contact ID belongs to the currently logged-in site member.
  • "NOT_AUTHENTICATED_MEMBER": The returned contact ID belongs to a site member who is not currently logged in.

Was this helpful?

Append or create a contact with name

Code Example

Copy Code
1import { contacts } from 'wix-crm';
2
3// ...
4
5const contactInfo = {
6 name: {
7 first: "Ari",
8 last: "Thereyet"
9 }
10};
11
12contacts.appendOrCreateContact(contactInfo)
13 .then((resolvedContact) => {
14 return resolvedContact;
15 })
16 .catch((error) => {
17 console.error(error);
18 });
Append or create a contact, full object

Code Example

Copy Code
1import { contacts } from 'wix-crm';
2
3// ...
4
5const contactInfo = {
6 name: {
7 first: "Ari",
8 last: "Thereyet"
9 },
10 emails: [
11 {
12 email: "ari.thereyet@example.com",
13 },
14 {
15 email: "ari.thereyet.appended.email@example.com"
16 }
17 ],
18 phones: [
19 {
20 tag: "MOBILE",
21 countryCode: "US",
22 phone: "601-081-124",
23 primary: true
24 }
25 ]
26};
27
28contacts.appendOrCreateContact(contactInfo)
29 .then((resolvedContact) => {
30 return resolvedContact;
31 })
32 .catch((error) => {
33 console.error(error);
34 });