Custom Coded Workflow for Formatting Phone Number

I am trying to get the custom coded workflow example working for formatting a phone number but I am getting errors. Here is the code:

/**
* Transforms a 10-digit U.S. phone number into a standard format.
*/
const PHONE_NUMBER_PROP = “phone”;

const hubspot = require(‘@hubspot/api-client’);

exports.main = (event, callback) => {
callback(processEvent(event));
};

function processEvent(event) {
const hubspotClient = new hubspot.Client({ accessToken: process.env.Secrets });
let contactId = event.object.objectId;
hubspotClient.crm.contacts.basicApi
.getById(contactId, [PHONE_NUMBER_PROP])
.then(results => {
let phone = results.body.properties[PHONE_NUMBER_PROP];

hubspotClient.crm.contacts.basicApi
.update(
contactId,
{
properties: {
[PHONE_NUMBER_PROP]: formatPhoneNumber(phone)
}
}
)
.then(updateResults => null);
});
}

function formatPhoneNumber(phoneNumber) {
let cleaned = phoneNumber.replace(/\D/g, ‘’).trim();
let match = cleaned.match(/^(\d{3})(\d{3})(\d{4})$/);
if (match) {
// Format as (XXX) XXX-XXXX
return ‘(’ + match[1] + ') ’ + match[2] + ‘-’ + match[3];
// Alternatives (match[1] is the first three digits,
// match[2] is the second three, match[3] is the last four):
// - XXX-XXX-XXXX
// return match[1] + ‘-’ + match[2] + ‘-’ + match[3];
}
return phoneNumber;
}

Here is the error I am getting:

WARNING: The logs for this function have exceeded the 4KB limit.
...
99\",\"content-type\":\"application/json;charset=utf-8\",\"date\":\"Tue, 31 Oct 2023 16:32:45 GMT\",\"nel\":\"{\\\"success_fraction\\\":0.01,\\\"report_to\\\":\\\"cf-nel\\\",\\\"max_age\\\":604800}\",\"report-to\":\"{\\\"endpoints\\\":[{\\\"url\\\":\\\"https:\\\\/\\\\/a.nel.cloudflare.com\\\\/report\\\\/v3?s=v3mtygSkZEI51%2BqGr%2BREly12ulGuTsOBSfSYdbbSlh51XqVfum%2FadeBDEuRqi2qeucQ1OxHIr%2FujaudnSKNaPfc8VaMQQY383Coxsx0PrlKFKI8g5gCRIdlsrhf2uG9B\\\"}],\\\"group\\\":\\\"cf-nel\\\",\\\"max_age\\\":604800}\",\"server\":\"cloudflare\",\"strict-transport-security\":\"max-age=31536000; includeSubDomains; preload\",\"vary\":\"origin, Accept-Encoding\",\"x-content-type-options\":\"nosniff\",\"x-envoy-upstream-service-time\":\"3\",\"x-evy-trace-listener\":\"listener_https\",\"x-evy-trace-route-configuration\":\"listener_https/all\",\"x-evy-trace-route-service-name\":\"envoyset-translator\",\"x-evy-trace-served-by-pod\":\"iad02/hubapi-td/envoy-proxy-5b5c96c966-rmgdk\",\"x-evy-trace-virtual-host\":\"all\",\"x-hubspot-auth-failure\":\"401 Unauthorized\",\"x-hubspot-correlation-id\":\"1446a2fd-ea11-4171-9dce-059992fe5b41\",\"x-request-id\":\"1446a2fd-ea11-4171-9dce-059992fe5b41\",\"x-trace\":\"2B52064E753FD21E04CE05AAA12DABD3013C3CF9C0000000000000000000\"}"," at BasicApiResponseProcessor.<anonymous> (/opt/nodejs/node_modules/@hubspot/api-client/lib/codegen/crm/contacts/apis/BasicApi.js:247:23)"," at Generator.next (<anonymous>)"," at fulfilled (/opt/nodejs/node_modules/@hubspot/api-client/lib/codegen/crm/contacts/apis/BasicApi.js:5:58)"," at processTicksAndRejections (node:internal/process/task_queues:96:5)"]},"promise":{},"stack":["Runtime.UnhandledPromiseRejection: Error: HTTP-Code: 401","Message: An error occurred.","Body: {\"status\":\"error\",\"message\":\"Authentication credentials not found. This API supports OAuth 2.0 authentication and you can find more details at https://developers.hubspot.com/docs/methods/auth/oauth-overview\",\"correlationId\":\"1446a2fd-ea11-4171-9dce-059992fe5b41\",\"category\":\"INVALID_AUTHENTICATION\"}","Headers: {\"access-control-allow-credentials\":\"false\",\"cf-cache-status\":\"DYNAMIC\",\"cf-ray\":\"81ed4bbdcc26079c-IAD\",\"connection\":\"close\",\"content-length\":\"299\",\"content-type\":\"application/json;charset=utf-8\",\"date\":\"Tue, 31 Oct 2023 16:32:45 GMT\",\"nel\":\"{\\\"success_fraction\\\":0.01,\\\"report_to\\\":\\\"cf-nel\\\",\\\"max_age\\\":604800}\",\"report-to\":\"{\\\"endpoints\\\":[{\\\"url\\\":\\\"https:\\\\/\\\\/a.nel.cloudflare.com\\\\/report\\\\/v3?s=v3mtygSkZEI51%2BqGr%2BREly12ulGuTsOBSfSYdbbSlh51XqVfum%2FadeBDEuRqi2qeucQ1OxHIr%2FujaudnSKNaPfc8VaMQQY383Coxsx0PrlKFKI8g5gCRIdlsrhf2uG9B\\\"}],\\\"group\\\":\\\"cf-nel\\\",\\\"max_age\\\":604800}\",\"server\":\"cloudflare\",\"strict-transport-security\":\"max-age=31536000; includeSubDomains; preload\",\"vary\":\"origin, Accept-Encoding\",\"x-content-type-options\":\"nosniff\",\"x-envoy-upstream-service-time\":\"3\",\"x-evy-trace-listener\":\"listener_https\",\"x-evy-trace-route-configuration\":\"listener_https/all\",\"x-evy-trace-route-service-name\":\"envoyset-translator\",\"x-evy-trace-served-by-pod\":\"iad02/hubapi-td/envoy-proxy-5b5c96c966-rmgdk\",\"x-evy-trace-virtual-host\":\"all\",\"x-hubspot-auth-failure\":\"401 Unauthorized\",\"x-hubspot-correlation-id\":\"1446a2fd-ea11-4171-9dce-059992fe5b41\",\"x-request-id\":\"1446a2fd-ea11-4171-9dce-059992fe5b41\",\"x-trace\":\"2B52064E753FD21E04CE05AAA12DABD3013C3CF9C0000000000000000000\"}"," at process.<anonymous> (file:///var/runtime/index.mjs:1276:17)"," at process.emit (node:events:513:28)"," at emit (node:internal/process/promises:140:20)"," at processPromiseRejections (node:internal/process/promises:274:27)"," at processTicksAndRejections (node:internal/process/task_queues:97:32)"]}
Unknown application error occurred
Runtime.Unknown

Memory: 74/128 MB
Runtime: 1446.50 ms

Hey, @tnielsen :waving_hand: Did you get this resolved?

If not, based on the content of your error message, this is an authentication issue.

Questions:

  • Are you using a Private App or a Public App for your Secret?
  • Have you verified that your [Secret](http://Custom%20Coded Workflow for Formatting Phone Number I am trying to get the custom coded workflow example working for formatting a phone number but I am getting errors. Here is the code: /** * Transforms a 10-digit U.S. phone number into a standard format. */ const PHONE_NUMBER_PROP = “phone”; const hubspot = require(‘@hubspot/api-client’); exports.main = (event, callback) => { callback(processEvent(event)); }; function processEvent(event) { const hubspotClient = new hubspot.Client({ accessToken: process.env.Secrets }); let contactId = event.object.objectId; hubspotClient.crm.contacts.basicApi .getById(contactId, [PHONE_NUMBER_PROP]) .then(results => { let phone = results.body.properties[PHONE_NUMBER_PROP]; hubspotClient.crm.contacts.basicApi .update( contactId, { properties: { [PHONE_NUMBER_PROP]: formatPhoneNumber(phone) } } ) .then(updateResults => null); }); } function formatPhoneNumber(phoneNumber) { let cleaned = phoneNumber.replace(/\D/g, ‘’).trim(); let match = cleaned.match(/^(\d{3})(\d{3})(\d{4})$/); if (match) { // Format as (XXX) XXX-XXXX return ‘(’ + match[1] + ') ’ + match[2] + ‘-’ + match[3]; // Alternatives (match[1] is the first three digits, // match[2] is the second three, match[3] is the last four): // - XXX-XXX-XXXX // return match[1] + ‘-’ + match[2] + ‘-’ + match[3]; } return phoneNumber; } Here is the error I am getting: WARNING: The logs for this function have exceeded the 4KB limit. … 99","content-type":"application/json;charset=utf-8","date":"Tue, 31 Oct 2023 16:32:45 GMT","nel":"{\"success_fraction\":0.01,\"report_to\":\"cf-nel\",\"max_age\":604800}","report-to":"{\"endpoints\":[{\"url\":\"https:\\/\\/a.nel.cloudflare.com\\/report\\/v3?s=v3mtygSkZEI51%2BqGr%2BREly12ulGuTsOBSfSYdbbSlh51XqVfum%2FadeBDEuRqi2qeucQ1OxHIr%2FujaudnSKNaPfc8VaMQQY383Coxsx0PrlKFKI8g5gCRIdlsrhf2uG9B\"}],\"group\":\"cf-nel\",\"max_age\":604800}","server":"cloudflare","strict-transport-security":"max-age=31536000; includeSubDomains; preload","vary":"origin, Accept-Encoding","x-content-type-options":"nosniff","x-envoy-upstream-service-time":"3","x-evy-trace-listener":"listener_https","x-evy-trace-route-configuration":"listener_https/all","x-evy-trace-route-service-name":"envoyset-translator","x-evy-trace-served-by-pod":"iad02/hubapi-td/envoy-proxy-5b5c96c966-rmgdk","x-evy-trace-virtual-host":"all","x-hubspot-auth-failure":"401 Unauthorized","x-hubspot-correlation-id":"1446a2fd-ea11-4171-9dce-059992fe5b41","x-request-id":"1446a2fd-ea11-4171-9dce-059992fe5b41","x-trace":"2B52064E753FD21E04CE05AAA12DABD3013C3CF9C0000000000000000000"}“,” at BasicApiResponseProcessor. (/opt/nodejs/node_modules/@hubspot/api-client/lib/codegen/crm/contacts/apis/BasicApi.js:247:23)“,” at Generator.next ()“,” at fulfilled (/opt/nodejs/node_modules/@hubspot/api-client/lib/codegen/crm/contacts/apis/BasicApi.js:5:58)“,” at processTicksAndRejections (node:internal/process/task_queues:96:5)“]},“promise”:{},“stack”:[“Runtime.UnhandledPromiseRejection: Error: HTTP-Code: 401”,“Message: An error occurred.”,“Body: {"status":"error","message":"Authentication credentials not found. This API supports OAuth 2.0 authentication and you can find more details at Accounts Dashboard | HubSpot“Headers: {"access-control-allow-credentials":"false","cf-cache-status":"DYNAMIC","cf-ray":"81ed4bbdcc26079c-IAD","connection":"close","content-length":"299","content-type":"application/json;charset=utf-8","date":"Tue, 31 Oct 2023 16:32:45 GMT","nel":"{\"success_fraction\":0.01,\"report_to\":\"cf-nel\",\"max_age\":604800}","report-to":"{\"endpoints\":[{\"url\":\"https:\\/\\/a.nel.cloudflare.com\\/report\\/v3?s=v3mtygSkZEI51%2BqGr%2BREly12ulGuTsOBSfSYdbbSlh51XqVfum%2FadeBDEuRqi2qeucQ1OxHIr%2FujaudnSKNaPfc8VaMQQY383Coxsx0PrlKFKI8g5gCRIdlsrhf2uG9B\"}],\"group\":\"cf-nel\",\"max_age\":604800}","server":"cloudflare","strict-transport-security":"max-age=31536000; includeSubDomains; preload","vary":"origin, Accept-Encoding","x-content-type-options":"nosniff","x-envoy-upstream-service-time":"3","x-evy-trace-listener":"listener_https","x-evy-trace-route-configuration":"listener_https/all","x-evy-trace-route-service-name":"envoyset-translator","x-evy-trace-served-by-pod":"iad02/hubapi-td/envoy-proxy-5b5c96c966-rmgdk","x-evy-trace-virtual-host":"all","x-hubspot-auth-failure":"401 Unauthorized","x-hubspot-correlation-id":"1446a2fd-ea11-4171-9dce-059992fe5b41","x-request-id":"1446a2fd-ea11-4171-9dce-059992fe5b41","x-trace":"2B52064E753FD21E04CE05AAA12DABD3013C3CF9C0000000000000000000"}”,” at process. (file:///var/runtime/index.mjs:1276:17)”," at process.emit (node:events:513:28)“,” at emit (node:internal/process/promises:140:20)“,” at processPromiseRejections (node:internal/process/promises:274:27)“,” at processTicksAndRejections (node:internal/process/task_queues:97:32)"]} Unknown application error occurred Runtime.Unknown Memory: 74/128 MB Runtime: 1446.50 ms) is updated and correct?

Thanks for the additional information! — Jaycee

That is def an API issue as Jaycee mentioned above.

Also, you’ll need to remove ‘body’ from: let phone = results.body.properties[PHONE_NUMBER_PROP];

So it would be this: let phone = results.properties[PHONE_NUMBER_PROP];