I have a backend routine that I want to pass info & options parameters to the createContact() API, and return the ID of the newly created contact (which I will use to send a Triggered email and/or option to create member).
I got it working so that a contact is created but I’m unable to get its ID from the object that is returned from the elevated createContact() call.
Sticking to the API documentation, I tried to get the new contact’s ID using newContact._id, but when I write the line in dev mode I get warning that “property _id does not exist on type ”. I also tried newContact.contact._id, which it accepts, but when run the console log shows the value as “undefined”, even though the contact has been created.
Here’s the code:
Info & options parameters set in backend (using details passed from the frontend):
const info = {
name: {
first: firstName,
last: lastName
},
emails: {
items: [
{
email: email
}
]
},
phones: {
items: [
{
phone: phone,
countryCode: 'KH',
primary: 'true',
tag: 'MOBILE'
}
]
}
}
let options = {
allowDuplicates: true
}
const contactResult = await CreateNewContact(info, options)
const newID = contactResult.contact_id;
console.log("New contact created " + contactResult;
console.log("contact ID: " + newID);
return newID
Backend file containing createContact() API almost verbatim off the Wix example
import { Permissions, webMethod } from "wix-web-module";
import { contacts } from "wix-crm.v2";
import { elevate } from "wix-auth";
export const CreateNewContact = webMethod(
Permissions.Anyone,
async (info, options) => {
try {
const elevatedCreateContact = elevate(contacts.createContact);
const newContact = await elevatedCreateContact(info, options);
console.log("Successfully created a new contact:", newContact);
console.log("new contact ID: ", + newContact.contact._id)
return newContact;
} catch (error) {
console.log(error);
// Handle the error
}
},
);