It has been awhile since I have used custom code in Hubspot, back when it was API vs. Private Apps. I am tyring to use the following code to populate data into Hubspot for a referred contact. I think my error is in how I am trying to use the Private App Toekn. Any help is greaterly appreciated.
I undestand I can't post certain words in the post, I assume this word is not allow in context of geenrating busniness. I changed the format of it to refer(ral), as this code is about a refe(ral) propgram I saw in the Hubspot Community.
The post also automatically removed invalid HTML from the error message from the log posted below, so it may read different.
The code is pasted below and the error from the logs below it.
Code:
/*
Operations Hub Workshop 3: Workflow 2 Code
This code is used to attribute a refer(ral) back to the correct contact. This is done by querying the CRM to find a contact matching the referrer value associated to the enrolled contact. We then update the appropriate properties on both the referring and referred contact.
*/
//1. Import required libraries
const hubspot = require('@hubspot/api-client'); // HubSpot Node Client will allow us to make calls to HubSpot API
exports.main = (event, callback) => {
//2. Create our client
const hubspotClient = new hubspot.Client({
accessToken: process.env.HAPIKEY // Reference our Private App Token - this should have been added as a secret
});
//3. Store the referrer ID linked to the enrolled contact in a variable
const referrer = event.inputFields['referrer'];
//4. Configure our search query
const filterGroup = {
filters: [{ // filters are used to limit the results returned. We will simply filter for any contact who has a matching value for the referrer ID
propertyName: 'referrer_id',
operator: 'EQ',
value: referrer
}]
}
// We specify what properties we'd like to return from this search
let contactId = results.body.results[0].id; // Id of the referring contact
let referred_by = results.body.results[0].properties.firstname + " " + results.body.results[0].properties.lastname; // full name of the referring contact
let totalReferrals = 0;
console.log("referred_by" + referred_by);
//7. Check to see if this is their first refer(ral). If it is we set "totalRefer(rals)" property to "0"
WARNING: The logs for this function have exceeded the 4KB limit.
...
,\"correlationId\":\"50501b8e-c256-4242-820d-116b493ff4a3\"}","Headers: {\"access-control-allow-credentials\":\"false\",\"cf-cache-status\":\"DYNAMIC\",\"cf-ray\":\"898f8b4a695d0584-IAD\",\"connection\":\"keep-alive\",\"content-length\":\"123\",\"content-type\":\"application/json;charset=utf-8\",\"date\":\"Mon, 24 Jun 2024 20:42:23 GMT\",\"nel\":\"{\\\"success_fraction\\\":0.01,\\\"report_to\\\":\\\"cf-nel\\\",\\\"max_age\\\":604800}\",\"report-to\":\"{\\\"endpoints\\\":[{\\\"url\\\":\\\"https:\\\\/\\\\/a.nel.cloudflare.com\\\\/report\\\\/v4?s=%2BsTIoc08tLHRT%2FOnsRv3aNpTFJSKK7OZ5j0VDMmaKhYJjOgeAx%2F1cnREWjrEZ1UtMfO%2FemV6gaQsP9Xse978urSQYGSrjVMuJ9DhUwOVIO%2Bx7z4Wlekk4rb0%2Fgl6yq4k\\\"}],\\\"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\":\"70\",\"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-7dd59b876-zb9w4\",\"x-evy-trace-virtual-host\":\"all\",\"x-hubspot-correlation-id\":\"50501b8e-c256-4242-820d-116b493ff4a3\",\"x-hubspot-ratelimit-daily\":\"500000\",\"x-hubspot-ratelimit-daily-remaining\":\"499998\",\"x-request-id\":\"50501b8e-c256-4242-820d-116b493ff4a3\"}"," at SearchApiResponseProcessor.<anonymous> (/opt/nodejs/node_modules/@hubspot/api-client/lib/codegen/crm/contacts/apis/SearchApi.js:59:23)"," at Generator.next (<anonymous>)"," at fulfilled (/opt/nodejs/node_modules/@hubspot/api-client/lib/codegen/crm/contacts/apis/SearchApi.js:5:58)"," at process.processTicksAndRejections (node:internal/process/task_queues:95:5)"]},"promise":{},"stack":["Runtime.UnhandledPromiseRejection: Error: HTTP-Code: 400","Message: An error occurred.","Body: {\"status\":\"error\",\"message\":\"There was a problem with the request.\",\"correlationId\":\"50501b8e-c256-4242-820d-116b493ff4a3\"}","Headers: {\"access-control-allow-credentials\":\"false\",\"cf-cache-status\":\"DYNAMIC\",\"cf-ray\":\"898f8b4a695d0584-IAD\",\"connection\":\"keep-alive\",\"content-length\":\"123\",\"content-type\":\"application/json;charset=utf-8\",\"date\":\"Mon, 24 Jun 2024 20:42:23 GMT\",\"nel\":\"{\\\"success_fraction\\\":0.01,\\\"report_to\\\":\\\"cf-nel\\\",\\\"max_age\\\":604800}\",\"report-to\":\"{\\\"endpoints\\\":[{\\\"url\\\":\\\"https:\\\\/\\\\/a.nel.cloudflare.com\\\\/report\\\\/v4?s=%2BsTIoc08tLHRT%2FOnsRv3aNpTFJSKK7OZ5j0VDMmaKhYJjOgeAx%2F1cnREWjrEZ1UtMfO%2FemV6gaQsP9Xse978urSQYGSrjVMuJ9DhUwOVIO%2Bx7z4Wlekk4rb0%2Fgl6yq4k\\\"}],\\\"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\":\"70\",\"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-7dd59b876-zb9w4\",\"x-evy-trace-virtual-host\":\"all\",\"x-hubspot-correlation-id\":\"50501b8e-c256-4242-820d-116b493ff4a3\",\"x-hubspot-ratelimit-daily\":\"500000\",\"x-hubspot-ratelimit-daily-remaining\":\"499998\",\"x-request-id\":\"50501b8e-c256-4242-820d-116b493ff4a3\"}"," at process.<anonymous> (file:///var/runtime/index.mjs:1276:17)"," at process.emit (node:events:519:28)"," at emit (node:internal/process/promises:150:20)"," at processPromiseRejections (node:internal/process/promises:284:27)"," at process.processTicksAndRejections (node:internal/process/task_queues:96:32)"]}
Unknown application error occurred
Runtime.Unknown
Memory: 21/128 MB
Runtime: 2254.24 ms
I solved the issues I was having with the code from Jack Coldrick's Creating a Refewrral Session in Hubspot. There were three issues resolved by changes to the coe originally provided in the session.
1. The solution above changing "properties:" to "properties:properties".
2. Removing ".body" from the the orginal code as it is no loinger required (found in a different post)
3. Being sure the property internal names I had created matched the ones I was using in the code.
I solved the issues I was having with the code from Jack Coldrick's Creating a Refewrral Session in Hubspot. There were three issues resolved by changes to the coe originally provided in the session.
1. The solution above changing "properties:" to "properties:properties".
2. Removing ".body" from the the orginal code as it is no loinger required (found in a different post)
3. Being sure the property internal names I had created matched the ones I was using in the code.
Thanks for your fast response and assistance. I made the change, unfortunately it is still throwing an error and not populating the data back into Hubspot.
Did I used the Private App/Token correctly where the Secret "HAPIKey" is the Token from the private app.:
"//2. Create our client const hubspotClient = new hubspot.Client({ accessToken: process.env.HAPIKEY // Reference our Private App Token - this should have been added as a secret
I see a yellow highlight with a message for line of code: exports.main = (event, callback) => { with the message 'Callback' is defined but never used.
Here is the verbiage from the Logs:
WARNING: The logs for this function have exceeded the 4KB limit.
...
2-4631-8e1c-d1b75bdc5730\"}","Headers: {\"access-control-allow-credentials\":\"false\",\"cf-cache-status\":\"DYNAMIC\",\"cf-ray\":\"8997aed16e9757be-IAD\",\"connection\":\"keep-alive\",\"content-length\":\"123\",\"content-type\":\"application/json;charset=utf-8\",\"date\":\"Tue, 25 Jun 2024 20:24:45 GMT\",\"nel\":\"{\\\"success_fraction\\\":0.01,\\\"report_to\\\":\\\"cf-nel\\\",\\\"max_age\\\":604800}\",\"report-to\":\"{\\\"endpoints\\\":[{\\\"url\\\":\\\"https:\\\\/\\\\/a.nel.cloudflare.com\\\\/report\\\\/v4?s=dlwUzWMg%2BEgHWkGwCgBXG%2BsZr9bfWgU3BxWUs%2Fg1WQHaBHkCpaswB%2B32YAwXfJq%2F8YELdAfog%2By2kS3G4toOlN%2BQcgfLrjA9wZrAtUjxS6LrUYYEMW9vYIZVHKms%2B1zp\\\"}],\\\"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\":\"85\",\"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-7dd59b876-smxcv\",\"x-evy-trace-virtual-host\":\"all\",\"x-hubspot-correlation-id\":\"f8e0e34a-62e2-4631-8e1c-d1b75bdc5730\",\"x-hubspot-ratelimit-daily\":\"500000\",\"x-hubspot-ratelimit-daily-remaining\":\"499999\",\"x-request-id\":\"f8e0e34a-62e2-4631-8e1c-d1b75bdc5730\"}"," at SearchApiResponseProcessor.<anonymous> (/opt/nodejs/node_modules/@hubspot/api-client/lib/codegen/crm/contacts/apis/SearchApi.js:59:23)"," at Generator.next (<anonymous>)"," at fulfilled (/opt/nodejs/node_modules/@hubspot/api-client/lib/codegen/crm/contacts/apis/SearchApi.js:5:58)"," at process.processTicksAndRejections (node:internal/process/task_queues:95:5)"]},"promise":{},"stack":["Runtime.UnhandledPromiseRejection: Error: HTTP-Code: 400","Message: An error occurred.","Body: {\"status\":\"error\",\"message\":\"There was a problem with the request.\",\"correlationId\":\"f8e0e34a-62e2-4631-8e1c-d1b75bdc5730\"}","Headers: {\"access-control-allow-credentials\":\"false\",\"cf-cache-status\":\"DYNAMIC\",\"cf-ray\":\"8997aed16e9757be-IAD\",\"connection\":\"keep-alive\",\"content-length\":\"123\",\"content-type\":\"application/json;charset=utf-8\",\"date\":\"Tue, 25 Jun 2024 20:24:45 GMT\",\"nel\":\"{\\\"success_fraction\\\":0.01,\\\"report_to\\\":\\\"cf-nel\\\",\\\"max_age\\\":604800}\",\"report-to\":\"{\\\"endpoints\\\":[{\\\"url\\\":\\\"https:\\\\/\\\\/a.nel.cloudflare.com\\\\/report\\\\/v4?s=dlwUzWMg%2BEgHWkGwCgBXG%2BsZr9bfWgU3BxWUs%2Fg1WQHaBHkCpaswB%2B32YAwXfJq%2F8YELdAfog%2By2kS3G4toOlN%2BQcgfLrjA9wZrAtUjxS6LrUYYEMW9vYIZVHKms%2B1zp\\\"}],\\\"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\":\"85\",\"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-7dd59b876-smxcv\",\"x-evy-trace-virtual-host\":\"all\",\"x-hubspot-correlation-id\":\"f8e0e34a-62e2-4631-8e1c-d1b75bdc5730\",\"x-hubspot-ratelimit-daily\":\"500000\",\"x-hubspot-ratelimit-daily-remaining\":\"499999\",\"x-request-id\":\"f8e0e34a-62e2-4631-8e1c-d1b75bdc5730\"}"," at process.<anonymous> (file:///var/runtime/index.mjs:1276:17)"," at process.emit (node:events:519:28)"," at emit (node:internal/process/promises:150:20)"," at processPromiseRejections (node:internal/process/promises:284:27)"," at process.processTicksAndRejections (node:internal/process/task_queues:96:32)"]}
Unknown application error occurred
Runtime.Unknown
Memory: 86/128 MB
WARNING: The logs for this function have exceeded the 4KB limit.
...
2-4631-8e1c-d1b75bdc5730\"}","Headers: {\"access-control-allow-credentials\":\"false\",\"cf-cache-status\":\"DYNAMIC\",\"cf-ray\":\"8997aed16e9757be-IAD\",\"connection\":\"keep-alive\",\"content-length\":\"123\",\"content-type\":\"application/json;charset=utf-8\",\"date\":\"Tue, 25 Jun 2024 20:24:45 GMT\",\"nel\":\"{\\\"success_fraction\\\":0.01,\\\"report_to\\\":\\\"cf-nel\\\",\\\"max_age\\\":604800}\",\"report-to\":\"{\\\"endpoints\\\":[{\\\"url\\\":\\\"https:\\\\/\\\\/a.nel.cloudflare.com\\\\/report\\\\/v4?s=dlwUzWMg%2BEgHWkGwCgBXG%2BsZr9bfWgU3BxWUs%2Fg1WQHaBHkCpaswB%2B32YAwXfJq%2F8YELdAfog%2By2kS3G4toOlN%2BQcgfLrjA9wZrAtUjxS6LrUYYEMW9vYIZVHKms%2B1zp\\\"}],\\\"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\":\"85\",\"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-7dd59b876-smxcv\",\"x-evy-trace-virtual-host\":\"all\",\"x-hubspot-correlation-id\":\"f8e0e34a-62e2-4631-8e1c-d1b75bdc5730\",\"x-hubspot-ratelimit-daily\":\"500000\",\"x-hubspot-ratelimit-daily-remaining\":\"499999\",\"x-request-id\":\"f8e0e34a-62e2-4631-8e1c-d1b75bdc5730\"}"," at SearchApiResponseProcessor.<anonymous> (/opt/nodejs/node_modules/@hubspot/api-client/lib/codegen/crm/contacts/apis/SearchApi.js:59:23)"," at Generator.next (<anonymous>)"," at fulfilled (/opt/nodejs/node_modules/@hubspot/api-client/lib/codegen/crm/contacts/apis/SearchApi.js:5:58)"," at process.processTicksAndRejections (node:internal/process/task_queues:95:5)"]},"promise":{},"stack":["Runtime.UnhandledPromiseRejection: Error: HTTP-Code: 400","Message: An error occurred.","Body: {\"status\":\"error\",\"message\":\"There was a problem with the request.\",\"correlationId\":\"f8e0e34a-62e2-4631-8e1c-d1b75bdc5730\"}","Headers: {\"access-control-allow-credentials\":\"false\",\"cf-cache-status\":\"DYNAMIC\",\"cf-ray\":\"8997aed16e9757be-IAD\",\"connection\":\"keep-alive\",\"content-length\":\"123\",\"content-type\":\"application/json;charset=utf-8\",\"date\":\"Tue, 25 Jun 2024 20:24:45 GMT\",\"nel\":\"{\\\"success_fraction\\\":0.01,\\\"report_to\\\":\\\"cf-nel\\\",\\\"max_age\\\":604800}\",\"report-to\":\"{\\\"endpoints\\\":[{\\\"url\\\":\\\"https:\\\\/\\\\/a.nel.cloudflare.com\\\\/report\\\\/v4?s=dlwUzWMg%2BEgHWkGwCgBXG%2BsZr9bfWgU3BxWUs%2Fg1WQHaBHkCpaswB%2B32YAwXfJq%2F8YELdAfog%2By2kS3G4toOlN%2BQcgfLrjA9wZrAtUjxS6LrUYYEMW9vYIZVHKms%2B1zp\\\"}],\\\"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\":\"85\",\"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-7dd59b876-smxcv\",\"x-evy-trace-virtual-host\":\"all\",\"x-hubspot-correlation-id\":\"f8e0e34a-62e2-4631-8e1c-d1b75bdc5730\",\"x-hubspot-ratelimit-daily\":\"500000\",\"x-hubspot-ratelimit-daily-remaining\":\"499999\",\"x-request-id\":\"f8e0e34a-62e2-4631-8e1c-d1b75bdc5730\"}"," at process.<anonymous> (file:///var/runtime/index.mjs:1276:17)"," at process.emit (node:events:519:28)"," at emit (node:internal/process/promises:150:20)"," at processPromiseRejections (node:internal/process/promises:284:27)"," at process.processTicksAndRejections (node:internal/process/task_queues:96:32)"]}
Unknown application error occurred
Runtime.Unknown
Memory: 86/128 MB
Runtime: 2181.08 ms