⚙ Operations Hub

thomasvbe
Contributor

Custom object search API not correct?

SOLVE

Hi

 

I wanted to search for a custom object that I've created and wanted to use the API in a custom code workflow. The documentation states that you have to use the following: 

 

const apiResponse = await hubspotClient.crm.searchApi.doSearch(objectType, PublicObjectSearchRequest);

 

 

When I tested this out, I got: 

TypeError: Cannot read property 'doSearch' of undefined

After some testing a landed on this giving me the wanted result: 

 

const apiResponse = await hubspotClient.crm.objects.searchApi.search(objectType, PublicObjectSearchRequest);

 

 

My question: is this the correct way and is the documentation on https://developers.hubspot.com/docs/api/crm/crm-custom-objects outdated?

 

Full code:

 

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

exports.main = async (event, callback) => {

  const hubspotClient = new hubspot.Client({
    apiKey: process.env.HAPIKEY
  });

	const PublicObjectSearchRequest = { filterGroups: [{"filters":[{"value":true,"propertyName":"used","operator":"NEQ"}]}], sorts:["couponcode"], properties: ["couponcode"], limit: 1, after: 0 };
	const objectType = "coupon";

  try {
    
  	//const apiResponse = await hubspotClient.crm.objects.searchApi.search(objectType, PublicObjectSearchRequest);
    const apiResponse = await hubspotClient.crm.searchApi.doSearch(objectType, PublicObjectSearchRequest);

  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)
}



  callback({
    outputFields: {
    
    }
  });
}

 

0 Upvotes
1 Accepted solution
nikodev
Solution
Top Contributor | Elite Partner
Top Contributor | Elite Partner

Custom object search API not correct?

SOLVE

Hey there @thomasvbe

 

I've run into this rather frustrating problem in the past as well. You're absolutely correct, the documentation is outdated. Also, there's a cool method you can use to check out the object structure of the hubspot API client whenever you're in doubt, but just using console.log() to print out that hierarchy. They seem to have done some restructuring. Take the below, for example: 

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

exports.main = async (event, callback) => {
	/*****
    How to use secrets
    Secrets are a way for you to save API keys and set them as a variable to use anywhere in your code
    Each secret needs to be defined like the example below
  *****/

	const hubspotClient = new hubspot.Client({
		apiKey: process.env.API_KEY
	});

	try {
		console.log(hubspotClient.crm.objects)
	} catch (err) {
		console.error(err);
		// We will automatically retry when the code fails because of a rate limiting error from the HubSpot API.
		throw err;
	}
};


which produced the following output: 

Screen Shot 2022-04-14 at 6.06.55 PM.png

Screen Shot 2022-04-14 at 6.06.47 PM.png

All that being said, I think any method that gets you the result you're looking for in cases like these, should be considered a viable and acceptable solution, so I'd say full speed ahead with what you've come up with. Going forward, if you ever run into a similar issue, I recommend logging the client's structure so you know exactly what you're working with and what your options are.

 

A8 Labs

View solution in original post

1 Reply 1
nikodev
Solution
Top Contributor | Elite Partner
Top Contributor | Elite Partner

Custom object search API not correct?

SOLVE

Hey there @thomasvbe

 

I've run into this rather frustrating problem in the past as well. You're absolutely correct, the documentation is outdated. Also, there's a cool method you can use to check out the object structure of the hubspot API client whenever you're in doubt, but just using console.log() to print out that hierarchy. They seem to have done some restructuring. Take the below, for example: 

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

exports.main = async (event, callback) => {
	/*****
    How to use secrets
    Secrets are a way for you to save API keys and set them as a variable to use anywhere in your code
    Each secret needs to be defined like the example below
  *****/

	const hubspotClient = new hubspot.Client({
		apiKey: process.env.API_KEY
	});

	try {
		console.log(hubspotClient.crm.objects)
	} catch (err) {
		console.error(err);
		// We will automatically retry when the code fails because of a rate limiting error from the HubSpot API.
		throw err;
	}
};


which produced the following output: 

Screen Shot 2022-04-14 at 6.06.55 PM.png

Screen Shot 2022-04-14 at 6.06.47 PM.png

All that being said, I think any method that gets you the result you're looking for in cases like these, should be considered a viable and acceptable solution, so I'd say full speed ahead with what you've come up with. Going forward, if you ever run into a similar issue, I recommend logging the client's structure so you know exactly what you're working with and what your options are.

 

A8 Labs