Question: How to create, update or append a contact with a user’s input values
Created contacts should appear on the Dash in Customers & Leads > Contacts right? But nothing has worked yet even at a basic level
Attempts (all failed!) :
Approach 1) Only front end code as per an older tutorial
import { contacts } from 'wix-crm';
$w.onReady(function () {
$w('#submitButton').onClick((event) => {
const contactInfo = {
name: {
first: $w('#input7').value,
last: "Thereyet"
},
emails: [{
email: "gene.lopez@example.com"
}]
};
contacts.appendOrCreateContact(contactInfo)
.then((resolvedContact) => {
return resolvedContact;
})
.catch((error) => {
console.error(error);
});
})
});
Approach 2) AI Velo Assistant on Velo Docs
// Backend code - contacts.jsw
import { contacts } from 'wix-crm.v2'; // Or 'wix-crm-backend'
import { elevate } from 'wix-auth';
import { webMethod, Permissions } from 'wix-web-module';
export const createNewContact = webMethod(Permissions.Anyone, async (contactInfo) => {
try {
const elevatedCreateContact = elevate(contacts.createContact);
const newContact = await elevatedCreateContact(contactInfo);
return newContact;
} catch (error) {
console.error('Error creating contact:', error);
throw new Error('Failed to create contact.');
}
});
// Page code
import { createNewContact } from 'backend/contacts.jsw';
$w.onReady(function () {
$w('#submitButton').onClick(async () => {
const firstName = $w('#input7').value;
const lastName = $w('#input16').value;
const email = $w('#input8').value;
const contactInfo = {
name: {
first: firstName,
last: lastName
},
emails: [{
email: email
}]
};
createNewContact(contactInfo);
});
});
Approach 3) GPT’s suggests only frontend
import { contacts } from 'wix-crm';
import { session } from 'wix-storage';
$w.onReady(function () {
$w('#submitButton').onClick(() => createContact());
});
async function createContact() {
const name = $w('#input7').value;
const email = $w('#input8').value;
try {
const contactInfo = {
name: { first: name },
emails: [{ email }]
};
const newContact = await contacts.appendOrCreateContact(contactInfo);
session.setItem("createdContactId", newContact.contactId);
} catch (error) {
console.error("Error creating contact:", error);
}
}
Many thanks!

