APIs & Integrations

lnglkcpx
Participant

API deal creation call not associating contact

Hi All,

 

We have an API between out internal systems and Hubspot. We have it in c#. Currently we create a deal in our system which then pushes and creates a new deal with a number of properties in Hubspot. Of note - we associate the company record to the deal. Additionally we seperately call for the contact and company id without issue. This works perfectly without issue.

request.AddBody(new
            {
                associations = (new { associatedCompanyIds = new long[] { lCompany } }),
                properties = new[] {
                    new { name = "hubspot_owner_id", value = dealOwner},
                    new { name = "dealname", value = dealName},
                    new { name = "dealstage", value = dealStage},
                    new { name = "amount", value = dealAmount},
                    new { name = "closedate", value = ldt}
                }
            });

 

Now we tried adding the contact association:

 request.AddBody(new
            {
                associations = (new { associatedCompanyIds = new long[] { lCompany } }, new { associatedVids = new long[] { lVid } }),
                properties = new[] {
                    new { name = "hubspot_owner_id", value = dealOwner},
                    new { name = "dealname", value = dealName},
                    new { name = "dealstage", value = dealStage},
                    new { name = "amount", value = dealAmount},
                    new { name = "closedate", value = ldt}
                }
            });

With this updated code deployed and a deal created in our internal system, a deal is successfully created in Hubspot. BUT with neither a company OR contact association.

 

Here is the entirew code block if helpful, with the code that doesn't work commented out:

public static string CreateDeal(string companyID, string dealName, string dealStage, string dealOwner, string dealAmount, string closedDate, string emailAddress)
        {
            string retString = "";
            string strEndpointURL = string.Format("https://api.hubapi.com/deals/v1/deal?hapikey={0}", hapikey);

            var client = new RestClient(strEndpointURL);
            var request = new RestRequest(Method.POST);
            request.RequestFormat = DataFormat.Json;

            request.AddHeader("cache-control", "no-cache");
            request.AddHeader("Content-Type", "application/json");

            ////get the email vid associated with the buyers id
            //string vid = getEmailVid(emailAddress);
            
            //long lVid = 0;
            //long.TryParse(vid, out lVid);
            
            DateTime dt;
            DateTime.TryParse(closedDate, out dt);
            string ldt = ToJavaTimeStampToDateTime(dt).ToString();

            long lCompany = 0;
            long.TryParse(companyID, out lCompany);

            request.AddBody(new
            {
                associations = (new { associatedCompanyIds = new long[] { lCompany } }),
                properties = new[] {
                    new { name = "hubspot_owner_id", value = dealOwner},
                    new { name = "dealname", value = dealName},
                    new { name = "dealstage", value = dealStage},
                    new { name = "amount", value = dealAmount},
                    new { name = "closedate", value = ldt}
                }
            });

            //request.AddBody(new
            //{
            //    associations = (new { associatedCompanyIds = new long[] { lCompany } }, new { associatedVids = new long[] { lVid } }),
            //    properties = new[] {
            //        new { name = "hubspot_owner_id", value = dealOwner},
            //        new { name = "dealname", value = dealName},
            //        new { name = "dealstage", value = dealStage},
            //        new { name = "amount", value = dealAmount},
            //        new { name = "closedate", value = ldt}
            //    }
            //});

            IRestResponse response = client.Execute(request);
            try
            {
                CreateDeal obj = ParseJsonObject<CreateDeal>(response.Content);
                retString = obj.dealId.ToString();
            }
            catch
            {
                retString = "Error: " + response.ErrorMessage;
            }
            return retString;
        }

 

Any thoughts would be much appreciated!

0 Upvotes
9 Replies 9
WendyGoh
HubSpot Employee
HubSpot Employee

API deal creation call not associating contact

Hey @lnglkcpx,

 

When looking at the code you shared:

 

{
            //    associations = (new { associatedCompanyIds = new long[] { lCompany } }, new { associatedVids = new long[] { lVid } }),
            //    properties = new[] {

 

 

I noticed that it doesn't quite exactly fit the required JSON output:

 

{
  "associations": {
    "associatedCompanyIds": [
      8954037
    ],
    "associatedVids": [
      27136
    ]
  },
  "properties": [

Could you print out the JSON output and see if it matches the required JSON output stated here - Create a deal | Deals API? If it does, could you share with me:

1. Your portal ID

2. The deal ID that was created successfully

3. The company ID that you'd like to associated with the deal

 

0 Upvotes
lnglkcpx
Participant

API deal creation call not associating contact

Hi @WendyGoh!! Thanks for your response and your assistance!

 

We actually use c# so I would need c# syntax, not native JSON. I will also add that piece of info to my initial post, too.

 

With that piece of important data now known, any thoughts?

 

Thank you!

0 Upvotes
WendyGoh
HubSpot Employee
HubSpot Employee

API deal creation call not associating contact

Hey @lnglkcpx,

 

Comparing the two codes you shared - the working one with companyId and the non working one with both the companyId and Vid, I wasn't able to spot anything major. 

 

In this case, could you share with me:

1. Your portal Id

2. The company id that you're looking to associated with the deal

3. The vid that you're looking to associated with the deal

 

So that I can try it out on my end and see if it works.

0 Upvotes
lnglkcpx
Participant

API deal creation call not associating contact

Hi @WendyGoh As requested!

 

Deal Example where we would do the association

Portal ID: 3268599

Company ID: 2974511602

Vid: 39117001

0 Upvotes
WendyGoh
HubSpot Employee
HubSpot Employee

API deal creation call not associating contact

Hey @lnglkcpx,

 

Strange. I tried the following code:

 

{
  "associations": {
    "associatedCompanyIds": [
      2974511602
    ],
    "associatedVids": [
      39117001
    ]
  },
  "properties": [
    {
      "value": "Hstest deal",
      "name": "dealname"
    },
    {
      "value": "appointmentscheduled",
      "name": "dealstage"
    },
    {
      "value": "default",
      "name": "pipeline"
    },
    {
      "value": 1409443200000,
      "name": "closedate"
    },
    {
      "value": "60000",
      "name": "amount"
    },
    {
      "value": "newbusiness",
      "name": "dealtype"
    }
  ]
}

And I was able to see that the deal I created was successfully associated with both company Id - 2974511602 and contact Id - 39117001

 

associatedSuccessfully.png

 

Can we try using a fixed value on the request body instead of passing a variable?

0 Upvotes
PenRick
Member

API deal creation call not associating contact

Hey WendyGoh, Any thoughts on how I would connect deal with contact using the HubSpot.NET wrapper?  Righ now my Deal.Create code using HubSpot.NET looks like this...


DealHubSpotModel deal = HsAPI.Deal.Create(new DealHubSpotModel()
{
Name = arHdr[i].ArInvoiceHistoryDetails[d].ItemCodeDesc,
Sku = arHdr[i].ArInvoiceHistoryDetails[d].ItemCode,
Amount = Convert.ToDouble(arHdr[i].ArInvoiceHistoryDetails[d].ExtensionAmt),
ProductId = arHdr[i].ArInvoiceHistoryDetails[d].ItemCode
});

 

And I want to  add the Contact Vid so it links to the Deal.Create.

 

Thanks

0 Upvotes
WendyGoh
HubSpot Employee
HubSpot Employee

API deal creation call not associating contact

Hey @PenRick,

 

If you'd like to add association to contact when creating deal, you would need to add the following code:

 

associations = (new { associatedCompanyIds = new long[] { lCompany } }),

under request.AddBody

0 Upvotes
PenRick
Member

API deal creation call not associating contact

That is for a Company association.  I was aksing for a Contact association, but thanks, they are probably very similiar.

0 Upvotes
PenRick
Member

API deal creation call not associating contact

Got it to work with HubSpot.NET...

DealHubSpotModel deal = HsAPI.Deal.Create(new DealHubSpotModel()
{
Associations = { AssociatedContacts = new[] { lHsVid } }, /* lHsVid is Contact Vid */
Name = "Super Product"
});
lDealid = (long)deal.Id; // Get back deal.id