Velo in automations: how to add custom label to contact based on postal code?

Hi

We are trying to group our members based on postal code, so we can email them offers of activities we give that are close to them. The best workflow I’ve found so far, is as follows with wix automations:

When first registering as a contact, the contact gets labeled as “Postalcodes XX00-XX99”, after, I can use segments to filter the contacts into groups based on this label.
For example, if we want to contact all people with postal codes 2400 - 3500, I can select labels “Postalcodes 2400-2499”, “Postalcodes 2500-2599”, …, “Postalcodes 3500-3599” for this segment.

My wix automations workflow is as follows:
Trigger: contact gets registered → Action:run wix velo code → End

Because postal codes are stored as strings, it does not seem possible to use the normal wix automations pipeline and I have to use Velo code. I’ve tried following code, but it does not seem to work:

/**
 * Autocomplete function declaration, do not delete
 * @param {import('./__schema__.js').Payload} options
 */

import { contacts } from "wix-crm-backend";

export const invoke = async ({payload}) => {

  const options = { suppressAuth: false };
  let postalCode = Number(payload.contact.address.postalCode)

  if (!(postalCode >= 1000 && postalCode <= 9999)) {
    contacts.labelContact(payload.contactId, ["custom.check-postal-code-manually"], options);
    return {}
  }

  let twoDigitCode = Math.trunc(postalCode /100);
  contacts.labelContact(payload.contactId, ["custom.postalcodes-".concat(twoDigitCode, "00-", twoDigitCode, "99")], options);
  return {}
}

I am probably doing something wrong labeling the contact, but I am not understanding what exactly. The contactId should already have been made, because this function triggers after registration of the contact. Either a “Check Postal Code Manually” or “Postalcodes XX00-XX99” label should be added, but neither are.

1 Like