APIs & Integrations

Ronnie1
Participant | Elite Partner
Participant | Elite Partner

Problem with associated deal objects.

 

Hey everyone,

 

My problem is that when I request deal object's associated "line items" like this

 

import hubspot
from pprint import pprint
from hubspot.crm.deals import ApiException

client = hubspot.Client.create(api_key="YOUR_HUBSPOT_API_KEY")

try:
api_response = client.crm.deals.basic_api.get_by_id(deal_id="dealId",
associations=["line items"], archived=False)
pprint(api_response)
except ApiException as e:
print("Exception when calling basic_api->get_by_id: %s\n" % e)

 

It works perfectly, however when the deal object doesn't have any associated "line items" things go wrong.

 

I was wondering if anyone could help me how i could wrap it properly in if else statement like:

 

pseudo code:

if associated line items exists:

   return parse_associated_line_item_id's()

else:

    pass

 

Thanks in advance!

0 Upvotes
3 Replies 3
Ronnie1
Participant | Elite Partner
Participant | Elite Partner

Problem with associated deal objects.

Hey JBeatty,

 

Thanks for your answer.

 

 I modified your code and came up with a partially working solution.

 

<pre>

try:

  api_response = client.crm.deals.basic_api.get_by_id(deal_id="deal-id", associations=["LINE_ITEM"], archived=False).to_dict()

  if (api_response['associations'] == None):

    print("No Line Items")

  if (api_response['associations'] != None):

    print("Has Line Items")

except ApiException as e:

  print("Exception when calling basic_api->get_by_id: %s\n" % e)

</pre>

 

It works when I don't fetch any other associated objects that are in the deal object.

 

I tried:

<pre>

if (api_response['associations']['line_item'] != None):

if (api_response.get('associations').get('line_item'!= None):

</pre>

 

But both give me an error.

 

Any suggestions on how could I handle multiple associated objects with similar functions?

 

Thanks for the help!

0 Upvotes
JBeatty
Guide | Diamond Partner
Guide | Diamond Partner

Problem with associated deal objects.

Hi @Ronnie1,

 

Something like this worked for me:

try:
   api_response = client.crm.deals.basic_api.get_by_id(deal_id="deal-id", associations=["LINE_ITEM"], archived=False)
   if(api_response.associations == None):
       print("No Line Items")
   else:
       print("Has Line Items")
except ApiException as e:
   print("Exception when calling basic_api->get_by_id: %s\n" % e)

✔️ Was I able to help answer your question? Help the community by marking it as a solution.

Joshua Beatty
Software Developer with Pearagon

Still have questions? Let's Talk

dennisedson
HubSpot Product Team
HubSpot Product Team

Problem with associated deal objects.

@JBeatty , @wfong , either of you able to help out here?

0 Upvotes