Search.../

onContactUpdated( )

An event that triggers when a contact is updated.

Description

The onContactUpdated() event handler runs when a contact is updated. The received ContactUpdatedEvent object contains information about the contact that was updated.

If a contact is updated as part of a merge, metadata.originatedFrom is sent as "merge". Otherwise, originatedFrom is omitted.

Note: Backend events don't work when previewing your site.

Syntax

function onContactUpdated(event: ContactUpdatedEvent): void

onContactUpdated Parameters

NAME
TYPE
DESCRIPTION
event
ContactUpdatedEvent

Information about the contact that was updated and metadata for the event.

Returns

This function does not return anything.

Return Type:

void

Was this helpful?

An event fired when a contact is updated

Copy Code
1// Place this code in the events.js file
2// of your site's Backend section.
3// Add the file if it doesn't exist.
4
5export function wixCrm_onContactUpdated(event) {
6 const contactId = event.metadata.entityId;
7 const contactName = `${event.entity.info?.name?.first} ${event.entity.info?.name?.last}`;
8 console.log('Contact updated', event);
9}
10
11/* Full event object:
12 * {
13 * "metadata": {
14 * "id": "74cb6d78-21c8-42a6-aa7b-e9128e1c77e6",
15 * "entityId": "bc0ae72b-3285-485b-b0ad-c32c769a4daf",
16 * "eventTime": "2021-04-07T21:03:39.731445Z",
17 * "triggeredByAnonymizeRequest": false
18 * },
19 * "entity": {
20 * "_id": "bc0ae72b-3285-485b-b0ad-c32c769a4daf",
21 * "_createdDate": "2021-03-30T13:12:39.649Z",
22 * "_updatedDate": "2021-04-07T21:03:39.481Z",
23 * "revision": 2,
24 * "primaryInfo": {
25 * "email": "gene.lopez.at.home@example.com",
26 * "phone": "(722)-138-3099"
27 * },
28 * "info": {
29 * "name": {
30 * "first": "Gene",
31 * "last": "Lopez"
32 * },
33 * "extendedFields": {
34 * "contacts.displayByLastName": "Lopez Gene",
35 * "emailSubscriptions.deliverabilityStatus": "NOT_SET",
36 * "emailSubscriptions.subscriptionStatus": "NOT_SET",
37 * "custom.event-we-met-at": "LegalBigData",
38 * "emailSubscriptions.effectiveEmail": "gene.lopez.at.home@example.com",
39 * "contacts.displayByFirstName": "Gene Lopez"
40 * },
41 * "locale": "en-us",
42 * "company": "Borer and Sons, Attorneys at Law",
43 * "phones": [
44 * {
45 * "tag": "MOBILE",
46 * "_id": "820e4640-ffe0-4980-a097-62a715e73135",
47 * "countryCode": "US",
48 * "primary": true,
49 * "phone": "(722)-138-3099"
50 * },
51 * {
52 * "tag": "HOME",
53 * "_id": "8506549e-e4f8-42f6-b6fc-9db155b582ef",
54 * "countryCode": "US",
55 * "e164Phone": "+17044541233",
56 * "primary": false,
57 * "phone": "(704)-454-1233"
58 * }
59 * ],
60 * "birthdate": "1981-11-02",
61 * "labelKeys": [
62 * "contacts.contacted-me",
63 * "custom.new-lead"
64 * ],
65 * "picture": {
66 * "image": "https://randomuser.me/api/portraits/men/0.jpg",
67 * "imageProvider": "EXTERNAL"
68 * },
69 * "jobTitle": "Senior Staff Attorney",
70 * "emails": [
71 * {
72 * "tag": "HOME",
73 * "email": "gene.lopez.at.home@example.com",
74 * "primary": true,
75 * "_id": "5bdcce4a-37c2-46ed-b49c-d562c6e3c4ce"
76 * },
77 * {
78 * "tag": "WORK",
79 * "email": "gene.lopez@example.com",
80 * "primary": false,
81 * "_id": "78e5f398-e148-448d-b490-7c0b7d2ab336"
82 * }
83 * ],
84 * "addresses": [
85 * {
86 * "_id": "8532051f-91f2-42d9-9a97-9f2c39e64f7a",
87 * "tag": "HOME",
88 * "address": {
89 * "city": "El Cajon",
90 * "location": {
91 * "latitude": 84.1048,
92 * "longitude": -116.8836
93 * },
94 * "streetAddress": {
95 * "number": "9834",
96 * "name": "Bollinger Rd"
97 * },
98 * "formatted": "9834 Bollinger Rd\nEl Cajon, WY 97766\nUS",
99 * "country": "US",
100 * "postalCode": "97766",
101 * "subdivision": "US-WY"
102 * }
103 * }
104 * ]
105 * },
106 * "lastActivity": {
107 * "activityDate": "2021-03-30T13:12:39.649Z",
108 * "activityType": "CONTACT_CREATED"
109 * },
110 * "source": {
111 * "appId": "v4.createContact"
112 * }
113 * }
114 * }
115 */
Ignore an update event if it originated from a merge

Copy Code
1// Place this code in the events.js file
2// of your site's Backend section.
3// Add the file if it doesn't exist.
4
5export function wixCrm_onContactUpdated(event) {
6
7 if (event.metadata.originatedFrom === 'merge') {
8
9 console.log('Contact is a target contact for a merge. Ignoring event.');
10 return;
11
12 } else {
13
14 const contactId = event.metadata.entityId;
15 const contactName = `${event.entity.info?.name?.first} ${event.entity.info?.name?.last}`;
16 console.log('Contact updated', event);
17
18 // Handle the event
19
20 }
21
22}
23
24/* Full event object:
25 * {
26 * "metadata": {
27 * "id": "74cb6d78-21c8-42a6-aa7b-e9128e1c77e6",
28 * "entityId": "bc0ae72b-3285-485b-b0ad-c32c769a4daf",
29 * "eventTime": "2021-04-07T21:03:39.731445Z",
30 * "originatedFrom": "merge",
31 * "triggeredByAnonymizeRequest": false
32 * },
33 * "entity": {
34 * "_id": "bc0ae72b-3285-485b-b0ad-c32c769a4daf",
35 * "_createdDate": "2021-03-30T13:12:39.649Z",
36 * "_updatedDate": "2021-04-07T21:03:39.481Z",
37 * "revision": 2,
38 * "primaryInfo": {
39 * "email": "gene.lopez.at.home@example.com",
40 * "phone": "(722)-138-3099"
41 * },
42 * "info": {
43 * "name": {
44 * "first": "Gene",
45 * "last": "Lopez"
46 * },
47 * "extendedFields": {
48 * "contacts.displayByLastName": "Lopez Gene",
49 * "emailSubscriptions.deliverabilityStatus": "NOT_SET",
50 * "emailSubscriptions.subscriptionStatus": "NOT_SET",
51 * "custom.event-we-met-at": "LegalBigData",
52 * "emailSubscriptions.effectiveEmail": "gene.lopez.at.home@example.com",
53 * "contacts.displayByFirstName": "Gene Lopez"
54 * },
55 * "locale": "en-us",
56 * "company": "Borer and Sons, Attorneys at Law",
57 * "phones": [
58 * {
59 * "tag": "MOBILE",
60 * "_id": "820e4640-ffe0-4980-a097-62a715e73135",
61 * "countryCode": "US",
62 * "primary": true,
63 * "phone": "(722)-138-3099"
64 * },
65 * {
66 * "tag": "HOME",
67 * "_id": "8506549e-e4f8-42f6-b6fc-9db155b582ef",
68 * "countryCode": "US",
69 * "e164Phone": "+17044541233",
70 * "primary": false,
71 * "phone": "(704)-454-1233"
72 * }
73 * ],
74 * "birthdate": "1981-11-02",
75 * "labelKeys": [
76 * "contacts.contacted-me",
77 * "custom.new-lead"
78 * ],
79 * "picture": {
80 * "image": "https://randomuser.me/api/portraits/men/0.jpg",
81 * "imageProvider": "EXTERNAL"
82 * },
83 * "jobTitle": "Senior Staff Attorney",
84 * "emails": [
85 * {
86 * "tag": "HOME",
87 * "email": "gene.lopez.at.home@example.com",
88 * "primary": true,
89 * "_id": "5bdcce4a-37c2-46ed-b49c-d562c6e3c4ce"
90 * },
91 * {
92 * "tag": "WORK",
93 * "email": "gene.lopez@example.com",
94 * "primary": false,
95 * "_id": "78e5f398-e148-448d-b490-7c0b7d2ab336"
96 * }
97 * ],
98 * "addresses": [
99 * {
100 * "_id": "8532051f-91f2-42d9-9a97-9f2c39e64f7a",
101 * "tag": "HOME",
102 * "address": {
103 * "city": "El Cajon",
104 * "location": {
105 * "latitude": 84.1048,
106 * "longitude": -116.8836
107 * },
108 * "streetAddress": {
109 * "number": "9834",
110 * "name": "Bollinger Rd"
111 * },
112 * "formatted": "9834 Bollinger Rd\nEl Cajon, WY 97766\nUS",
113 * "country": "US",
114 * "postalCode": "97766",
115 * "subdivision": "US-WY"
116 * }
117 * }
118 * ]
119 * },
120 * "lastActivity": {
121 * "activityDate": "2021-03-30T13:12:39.649Z",
122 * "activityType": "CONTACT_MERGED"
123 * },
124 * "source": {
125 * "appId": "v4.createContact"
126 * }
127 * }
128 * }
129 */