May 4, 2022 9:37 AM - edited May 5, 2022 5:27 AM
Hey, I am encountering an isssue with API v3 for Batch Creating a Custom Objects.
Custom object required property:
prop_one - field type: Dropdown select
All works while I try to do it from API tool - site endpoints and Postman.
Issue here is that I need to provide code for Node.js Meteor based App, therefore I went for AzureFunctions. No issue with AZF integration etc. so far until now:
Code form HS APIv3 endpoints:
const hubspot = require('@hubspot/api-client');
const hubspotClient = new hubspot.Client({"apiKey":"xxx"});
const BatchInputSimplePublicObjectInput = { inputs: [{"properties":{"prop_one":"prop name","prop_two":"5:30"}},{"properties":{"prop_one":"prop name","prop_two":"6:30"}}] };
const objectType = "2-104078180";
try {
const apiResponse = await hubspotClient.crm.batchApi.create(objectType, BatchInputSimplePublicObjectInput);
console.log(JSON.stringify(apiResponse.body, null, 2));
} catch (e) {
e.message === 'HTTP request failed'
? console.error(JSON.stringify(e.response, null, 2))
: console.error(e)
}
RES:
HTTP 201
{
"status": "COMPLETE",
"results": [
{
"id": "249662907",
"properties": {
"hs_createdate": "2022-05-04T12:49:50.879Z",
"hs_lastmodifieddate": "2022-05-04T12:49:50.879Z",
"hs_object_id": "249662907",
"prop_two": "6:30",
"prop_one": "prop name"
},
"createdAt": "2022-05-04T12:49:50.879Z",
"updatedAt": "2022-05-04T12:49:50.879Z",
"archived": false
},
{
"id": "249662906",
"properties": {
"hs_createdate": "2022-05-04T12:49:50.879Z",
"hs_lastmodifieddate": "2022-05-04T12:49:50.879Z",
"hs_object_id": "249662906",
"prop_two": "5:30",
"prop_one": "prop name"
},
"createdAt": "2022-05-04T12:49:50.879Z",
"updatedAt": "2022-05-04T12:49:50.879Z",
"archived": false
}
],
"startedAt": "2022-05-04T12:49:50.866Z",
"completedAt": "2022-05-04T12:49:50.996Z"
}
my function App:
the same code from endpoint throw an error:
*** Error throw: "Cannot read property 'create' of undefined"
modified code ( while try catch block is handled in AZFunction )
and Not wrapped endpoint(s) recipies of '@hubspot/api-client' package:
const result = await hubspotClient.apiRequest({
method: 'POST',
path: `/crm/v3/objects/${objectType}/batch/create?`,
body: inputBody,
});
return result.body;
Works, creates custom object however ->
Body params are hardcoded here unfortunately:
let inputBody = { "inputs": [{"properties":{"prop_one":"prop name","prop_two":"5:30"}},
{"properties":
{"prop_one":"prop name","prop_two":"6:30"}}] }
but I need to have control over body params:
let inputBody = { "inputs": [{"properties":{"prop_one":body.prop_one,"prop_two":body.prop_two}},
{"properties":
{"prop_one":body.prop_one,"prop_two":body.prop_two}}] }
and req body has not changed it is still in Postman:
{ "inputs": [{"properties":{"prop_one":"prop name","prop_two":"5:30"}},
{"properties":
{"prop_one":"prop name","prop_two":"6:30"}}] }
While doing so:
*** Error throw: "Cannot create object with type: {fullyQualifiedName}. The following required properties were missing: [prop_one]" "HTTP request failed"
any thoughts?
Thank you in advance.
Jul 18, 2022 5:08 AM
Unless i am too late to this party, the call on the documentation is incorrect. Which it is for mosty of these calls as they have left out a vital part of the line.
So you have
await hubspotClient.crm.batchApi.create
which will bring up the error of cannot read property of create on undefined.
All these calls need an extra part to them
await hubspotClient.crm.objects.batchApi.create
so here i have added objects, this will make the call work.
You can find these out by digging into the code of the node api provided to see where the calls actually go and what needs to be in them
This will make it work as normal
Simon
May 5, 2022 3:11 AM
Hi,
No sure about how to, i just see some diferences in your code. please check
Const BatchInputSimplePublicObjectInput = { inputs: [{"properties":{"prop_one":"prop mane","prop_two":"5:30"}},{"properties":{"prop_one":"prop mane","prop_two":"6:30"}}] };
const objectType = "2-104078180";
As per instructions https://developers.hubspot.com/docs/api/crm/crm-custom-objects
Looks like ( i guess) could be one of this points.
1.- The input declaration you create are two times the same
Find a list of all properties for an object type using the CRM Object Properties API. e.g. GET https://api.hubapi.com/properties/v2/companies/properties
. Change the properties returned in the response using the properties
array in the request body.
So in
May 5, 2022 6:27 AM - edited May 20, 2022 12:24 PM
Hey @jpsanchez Thanks for your reply, thanks for your time.
I believe issue is more complex, this was quite dificult to explain maybe, not to post any sensitive data. Lets recap.
I use this to Create Batch of Custom Objects:
POST
/crm/v3/objects/{objectType}/batch/create
Custom object required property in HS instance declared as follow:
prop_one (internal value)
field type: Dropdown select
1 - this might be the issue, because of de-duplication, I thought that first, but Batch Creation takes, as I understand, this under consideration because it is about creating instance custom object in batches like so:
{ "inputs": [
{"properties":{"prop_one":"prop name","prop_two":"5:30"}},
{"properties":{"prop_one":"prop name","prop_two":"6:30"}}] }
de-duplication will fire while prop_two will be the same as I have investigated already:
so response return only one instance of object ( as API thoughts this is duplicate - not ideal, but I can live with this eventually)
code from HS endpoints example:
const BatchInputSimplePublicObjectInput = { inputs: [{"properties":{"prop_one":"prop name","prop_two":"5:30"}},{"properties":{"prop_one":"prop name","prop_two":"5:30"}}] };
const objectType = "2-104078180";
try {
const apiResponse = await hubspotClient.crm.batchApi.create(objectType, BatchInputSimplePublicObjectInput);
console.log(JSON.stringify(apiResponse.body, null, 2));
} catch (e) {
e.message === 'HTTP request failed'
? console.error(JSON.stringify(e.response, null, 2))
: console.error(e)
}
{ "inputs": [
{"properties":{"prop_one":"prop name","prop_two":"5:30"}},
{"properties":{"prop_one":"prop name","prop_two":"5:30"}}
]
}
RES
{
"status": "COMPLETE",
"results": [
{
"id": "252238555",
"properties": {
"hs_createdate": "2022-05-05T09:38:24.863Z",
"hs_lastmodifieddate": "2022-05-05T09:38:24.863Z",
"hs_object_id": "252238555",
"prop_two": "5:30",
"prop_one": "prop name"
},
"createdAt": "2022-05-05T09:38:24.863Z",
"updatedAt": "2022-05-05T09:38:24.863Z",
"archived": false
}
],
"startedAt": "2022-05-05T09:38:24.830Z",
"completedAt": "2022-05-05T09:38:25.084Z"
}
while there is difference with at lease one prop value this will be fine, two instances of the custom object will be created.
2.
const objectType = "2-104078180" - this is my custom objectType id Please find:
Retrieve existing custom objects
To retrieve all custom objects, make a GET request to /crm/v3/schemas.To retrieve a specific custom object, make a GET request to one of the following endpoints:
/crm/v3/schemas/{objectTypeId}
/crm/v3/schemas/{fullyQualifiedName}. You can find an object's
fullyQualifiedName in its schema, which is derived from p{portal_id}_{object_name}.For example, your request URL may look like the following:
Issue here is different nature:
I am not able to define body.param for body input other than hardcoded values using Batch Create API v3 for custom object:
https://developers.hubspot.com/docs/api/crm/crm-custom-objects
POST
/crm/v3/objects/{objectType}/batch/create
and code that works in endpoint example HS API dev page:
const hubspot = require('@hubspot/api-client');
const hubspotClient = new hubspot.Client({"apiKey":"xxx"});
const BatchInputSimplePublicObjectInput = { inputs: [{"properties":{"prop_one":"prop name","prop_two":"5:30"}},{"properties":{"prop_one":"prop name","prop_two":"6:30"}}] };
const objectType = "2-104078180";
try {
const apiResponse = await hubspotClient.crm.batchApi.create(objectType, BatchInputSimplePublicObjectInput);
console.log(JSON.stringify(apiResponse.body, null, 2));
} catch (e) {
e.message === 'HTTP request failed'
? console.error(JSON.stringify(e.response, null, 2))
: console.error(e)
}
node.js code - does not work in node.js application.
my function App:
the same code from endpoint throw an error:
*** Error throw: "Cannot read property 'create' of undefined"
I have worked this around using:
modified code ( while try catch block is handled in AZFunction )
and Not wrapped endpoint(s) recipies of '@hubspot/api-client' package:
async function searchForShiftsbyMonth(context, query, body) {
const hubspot = require('@hubspot/api-client');
const hubspotClient = new hubspot.Client({"apiKey":"xxx"});
let inputBody = { "inputs": [{"properties":{"prop_one":"prop name","prop_two":"5:30"}},
{"properties":
{"prop_one":"prop name","prop_two":"6:30"}}] }
const objectType = "2-104078180";
//also have tried with fullyQualifiedName
//which is derived from p{portal_id}_{object_name}
const result = await hubspotClient.apiRequest({
method: 'POST',
path: `/crm/v3/objects/${objectType}/batch/create?`,
body: inputBody,
});
return result.body;
}
So as you can see I have an Azure Functions that will hit endpoint, and return RES
and this one works.
Issue here - why code rfom copied from HS Guide does work on HS page but in Node.js app throws error?
*** Error throw: "Cannot read property 'create' of undefined"
But like I said this was overcome.
What I am not able to figure out is how to code params to be passed as body.params in HTTP POST request:
instead of hardcodding this in the App:
like so
let inputBody = { "inputs": [
{"properties":{"prop_one":"prop name","prop_two":"5:30"}},
{"properties":{"prop_one":"prop name","prop_two":"6:30"}}
] }
I would like to be able to change peops making HTTP request, so in code I am lookind for something like that:
let inputBody = { "inputs": [
{"properties":{"prop_one":body.prop_one,"prop_two":body.prop_two}},
{"properties":{"prop_one":body.prop_one,"prop_two":body.prop_two}}
]}
(and yes I am aware of de-duplication as I have mentioned on top of the post)
to be able to create various requests with varions body params passed by another app as body params to this function:
for example in POSTMAN:
{ "inputs": [
{"properties":{"prop_one":"prop name","prop_two":"5:30"}},
{"properties":{"prop_one":"prop name","prop_two":"6:30"}}
] }
or
{ "inputs": [
{"properties":{"prop_one":"prop name but different one","prop_two":"8:30"}},
{"properties":{"prop_one":"prop name","prop_two":"9:30"}}
] }
but so far I am getting this code errot throws:
*** Error throw: "Cannot create object with type: {fullyQualifiedName of my custom property here} . The following required properties were missing: [prop_one]" "HTTP request failed"
I will appreciate help.
Tanks in advance.
May 4, 2022 4:01 PM
Hey, @Kamil_KK 👋 Thanks for sharing all those details. Let's see if @Bryantworks or @jpsanchez have any experience here.
Thank you! – Jaycee