updateContact( )
Updates a contact's properties.
Description
The updateContact()
function returns a Promise that resolves
when the specified contact's information is updated.
Each time the contact is updated, revision
increments by 1. The existing revision
must be included when updating the contact. This ensures you're working with the latest contact information, and prevents unintended overwrites.
This function requires elevated permissions to run. This function is not universal and runs only on the backend.
Syntax
function updateContact(contactId: string, info: ContactInfo, revision: number, options: UpdateContactOptions): Promise<UpdateContactResponse>
updateContact Parameters
NAME
TYPE
DESCRIPTION
ID of the contact to update.
Contact info.
Revision number.
When updating, include the existing revision
to prevent conflicting updates.
Contact update options.
Returns
Updated contact.
Return Type:
NAME
TYPE
DESCRIPTION
Updated contact.
Was this helpful?
1import { contacts } from 'wix-crm.v2';23/* Sample contactId value: '2712ae1d-3f64-46c2-ac3a-94a6c2e29847'4 *5 * Sample revision value: '5'6 *7 * Sample info object:8 * {9 * phones: {10 * items: [11 * {12 * countryCode:"US",13 * phone:"6465676359",14 * primary:true,15 * tag:"MOBILE"16 * },17 * {18 * phone: "2421642341",19 * countryCode: "US",20 * primary: false,21 * tag: "HOME"22 * }23 * ]24 * }25 * }26 */2728export async function myUpdateContactFunction(contactId, info, revision, options) {2930 try {31 const updatedContact = await contacts.updateContact(contactId, info, revision, options);32 console.log('Successfully updated contact:', updatedContact)3334 return updatedContact;35 } catch (error) {36 console.log(error);37 // Handle the error38 }39}404142/* Promise resolves to:43 * {44 * "contact": {45 * "revision": 17,46 * "source": {47 * "sourceType": "WIX_CODE",48 * "appId": "151e476a-715e-ec33-db9a-a7ff4d51f70a"49 * },50 * "lastActivity": {51 * "activityDate": "2023-12-24T11:50:46.048Z",52 * "activityType": "CONTACT_CREATED"53 * },54 * "primaryInfo": {55 * "email": "johndoe1@example.com",56 * "phone": "646-567-6359"57 * },58 * "info": {59 * "name": {60 * "first": "John",61 * "last": "Doe"62 * },63 * "emails": {64 * "items": [65 * {66 * "tag": "UNTAGGED",67 * "email": "johndoe1@example.com",68 * "primary": true,69 * "_id": "37db3bde-83c0-4ca2-8097-88964a2f343b"70 * },71 * {72 * "tag": "UNTAGGED",73 * "email": "john.doe.at.home@example.com",74 * "primary": false,75 * "_id": "06eef938-13d2-49f3-8097-60686ab4ddd2"76 * }77 * ]78 * },79 * "phones": {80 * "items": [81 * {82 * "tag": "MOBILE",83 * "countryCode": "US",84 * "phone": "646-567-6359",85 * "e164Phone": "+16465676359",86 * "primary": true,87 * "_id": "01e597c0-76c6-4791-bb77-8b3309f893d1"88 * },89 * {90 * "tag": "HOME",91 * "countryCode": "US",92 * "phone": "2421642341",93 * "primary": false,94 * "_id": "cbbae95c-e1d5-48c4-a3ec-531ee2bf51fb"95 * }96 * ]97 * },98 * "addresses": {99 * "items": [100 * {101 * "tag": "HOME",102 * "address": {103 * "formatted": "3 Park Ave\nNY, New York 10010\nUnited States",104 * "addressLine1": "3 Park Ave",105 * "city": "NY",106 * "subdivision": "US-NY",107 * "country": "US",108 * "postalCode": "10010"109 * },110 * "_id": "eeb4e174-fd0f-4ce8-8cac-dc152f284228"111 * }112 * ]113 * },114 * "company": "Wix",115 * "jobTitle": "Writer",116 * "birthdate": "1995-04-25",117 * "locale": "en-us",118 * "labelKeys": {119 * "items": [120 * "custom.contact"121 * ]122 * },123 * "extendedFields": {124 * "items": {125 * "contacts.displayByLastName": "Doe John",126 * "invoices.vatId": "",127 * "emailSubscriptions.deliverabilityStatus": "NOT_SET",128 * "emailSubscriptions.subscriptionStatus": "UNSUBSCRIBED",129 * "contacts.displayByFirstName": "John Doe",130 * "custom.nickname": "Jonny"131 * }132 * }133 * },134 * "_id": "2712ae1d-3f64-46c2-ac3a-94a6c2e29847",135 * "_createdDate": "2023-12-24T11:50:46.049Z",136 * "_updatedDate": "2023-12-25T07:31:48.270Z"137 * }138 * }139 */140
1import { Permissions, webMethod } from 'wix-web-module';2import { contacts } from 'wix-crm.v2';3import { elevate } from 'wix-auth';45/* Sample contactId value: '2712ae1d-3f64-46c2-ac3a-94a6c2e29847'6 *7 * Sample revision value: '5'8 *9 * Sample info object:10 * {11 * phones: {12 * items: [13 * {14 * countryCode:"US",15 * phone:"6465676359",16 * primary:true,17 * tag:"MOBILE"18 * },19 * {20 * phone: "2421642341",21 * countryCode: "US",22 * primary: false,23 * tag: "HOME"24 * }25 * ]26 * }27 * }28 */2930export const myUpdateContactFunction = webMethod(Permissions.Anyone, async (contactId, info, revision, options) => {3132 try {33 const elevatedUpdateContact = elevate(contacts.updateContact);34 const updatedContact = await elevatedUpdateContact(contactId, info, revision, options);35 console.log('Successfully updated contact:', updatedContact)3637 return updatedContact;38 } catch (error) {39 console.log(error);40 // Handle the error41 }42});434445/* Promise resolves to:46 * {47 * "contact": {48 * "revision": 17,49 * "source": {50 * "sourceType": "WIX_CODE",51 * "appId": "151e476a-715e-ec33-db9a-a7ff4d51f70a"52 * },53 * "lastActivity": {54 * "activityDate": "2023-12-24T11:50:46.048Z",55 * "activityType": "CONTACT_CREATED"56 * },57 * "primaryInfo": {58 * "email": "johndoe1@example.com",59 * "phone": "646-567-6359"60 * },61 * "info": {62 * "name": {63 * "first": "John",64 * "last": "Doe"65 * },66 * "emails": {67 * "items": [68 * {69 * "tag": "UNTAGGED",70 * "email": "johndoe1@example.com",71 * "primary": true,72 * "_id": "37db3bde-83c0-4ca2-8097-88964a2f343b"73 * },74 * {75 * "tag": "UNTAGGED",76 * "email": "john.doe.at.home@example.com",77 * "primary": false,78 * "_id": "06eef938-13d2-49f3-8097-60686ab4ddd2"79 * }80 * ]81 * },82 * "phones": {83 * "items": [84 * {85 * "tag": "MOBILE",86 * "countryCode": "US",87 * "phone": "646-567-6359",88 * "e164Phone": "+16465676359",89 * "primary": true,90 * "_id": "01e597c0-76c6-4791-bb77-8b3309f893d1"91 * },92 * {93 * "tag": "HOME",94 * "countryCode": "US",95 * "phone": "2421642341",96 * "primary": false,97 * "_id": "cbbae95c-e1d5-48c4-a3ec-531ee2bf51fb"98 * }99 * ]100 * },101 * "addresses": {102 * "items": [103 * {104 * "tag": "HOME",105 * "address": {106 * "formatted": "3 Park Ave\nNY, New York 10010\nUnited States",107 * "addressLine1": "3 Park Ave",108 * "city": "NY",109 * "subdivision": "US-NY",110 * "country": "US",111 * "postalCode": "10010"112 * },113 * "_id": "eeb4e174-fd0f-4ce8-8cac-dc152f284228"114 * }115 * ]116 * },117 * "company": "Wix",118 * "jobTitle": "Writer",119 * "birthdate": "1995-04-25",120 * "locale": "en-us",121 * "labelKeys": {122 * "items": [123 * "custom.contact"124 * ]125 * },126 * "extendedFields": {127 * "items": {128 * "contacts.displayByLastName": "Doe John",129 * "invoices.vatId": "",130 * "emailSubscriptions.deliverabilityStatus": "NOT_SET",131 * "emailSubscriptions.subscriptionStatus": "UNSUBSCRIBED",132 * "contacts.displayByFirstName": "John Doe",133 * "custom.nickname": "Jonny"134 * }135 * }136 * },137 * "_id": "2712ae1d-3f64-46c2-ac3a-94a6c2e29847",138 * "_createdDate": "2023-12-24T11:50:46.049Z",139 * "_updatedDate": "2023-12-25T07:31:48.270Z"140 * }141 * }142 */143
The page code passes a contact ID with updated contact information to the backend overwriteContactInfo()
function. The overwriteContactInfo()
function retrieves the contact, extracts the revision number, then passes it to contacts.updateContact()
. This allows the contact to be updated without needing to specify the revision. >Note: This code example always retrieves the latest revision before updating a contact.
1/**********************************2 * Backend code - contacts.web.js *3 **********************************/45import { Permissions, webMethod } from 'wix-web-module';6import { contacts } from 'wix-crm.v2';7import { elevate } from 'wix-auth';89export const overwriteContactInfo = webMethod(Permissions.Anyone, async (contactId, updatedContactInfo) => {1011 try {12 // Retrieve the contact13 const elevateGetContact = elevate(contacts.getContact);14 const contact = await elevateGetContact(contactId);15 // Extract revision number16 const revisionNumber = { revision: contact.revision };17 // Update the contact18 const elevateUpdateContact = elevate(contacts.updateContact);19 const updatedContact = await elevateUpdateContact(contactId, updatedContactInfo, revisionNumber);2021 return updatedContact;22 } catch (error) {23 console.log(error);24 // Handle the error25 }26});272829/*************30 * Page code *31 *************/3233import { overwriteContactInfo } from 'backend/contacts.web';3435$w.onReady(async function () {36 const contactId = "2712ae1d-3f64-46c2-ac3a-94a6c2e29847";37 const updatedContactInfo = {38 name: {39 first: "John",40 last: "Mitchell"41 }42 };43 async function updateContactName(){4445 try {46 const updatedContact = await overwriteContactInfo(contactId, updatedContactInfo);4748 return updatedContact;49 } catch(error){50 console.log(error);51 // Handle the error52 }53 }54 const result = await updateContactName();55 const first = result.contact.info.name.first;56 const last = result.contact.info.name.last;57 console.log(`Updated contact name: ${first} ${last}`);58});59