Unable to createContact() with backend code

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
    }
  },
);
1 Like

Thanks. Was hopeful but afraid it doesn’t fix it.

On execution, my code successfully creates a new contact and returns a promise as newContact with the object:

{"contact":{"revision":1,"source":{"sourceType":"WIX_CODE","wixAppId":"151e476a-715e-ec33-db9a-a7ff4d51f71a","appId":"151e476a-715e-ec33-db9a-a7ff4d51f71a"},"lastActivity":{"activityDate":"2024-12-16T09:05:32.309Z","activityType":"CONTACT_CREATED","date":"2024-12-16T09:05:32.309Z","description":"Contact was created","icon":{"name":"User","url":"https://wixmp-8be454c954980f082caba37c.wixmp.com/activity-log/User.png"}},"primaryInfo":{"email":"example@example.com","phone":"0999999999"},"info":{"name":{"first":"John","last":"Doe"},"emails":{"items":[{"tag":"UNTAGGED","email":"example@example.com","primary":true,"_id":"474e532a-ff3e-42f4-a827-c8ec8b5e35a1"}]},"phones":{"items":[{"tag":"MOBILE","countryCode":"KH","phone":"0999999999","primary":true,"_id":"0e65cc4c-a5a2-4b19-b8ca-f53b37a2a1d2"}]},"extendedFields":{"items":{"contacts.displayByFirstName":"J D","contacts.displayByLastName":"D J"}}},"primaryEmail":{"email":"example@example.com","subscriptionStatus":"NOT_SET","deliverabilityStatus":"NOT_SET"},"primaryPhone":{"countryCode":"KH","formattedPhone":"0999999999","subscriptionStatus":"NO_SUBSCRIPTION_STATUS","deliverabilityStatus":"NO_PHONE_DELIVERABILITY_STATUS","phone":"0999999999"},"_id":"d1158604-0ddf-4e04-843d-963ec1832fd1","_createdDate":"2024-12-16T09:05:32.309Z","_updatedDate":"2024-12-16T09:05:32.309Z"}}

In this case, the contact ID that I’m trying to extract is: “_id”:“d1158604-0ddf-4e04-843d-963ec1832fd1”,

Note, this Create Contact call uses the newer wix-crm-v2 API (wix-crm-backend also has create contact APIs, one of which is deprecated).

I tried several different variants to extract the contact ID (newContact.id… newContact.contact.id… newContact._id) but all of them generate a “property _id does not exist on type ” error except one, newContact.contact._id, though it still inexplicably returns “undefined”.

Eventually I gave up on it and ended up and modifying my code to use the wix-crm-backend create contact API, which works fine.