Skip to content

Commit 1ee3100

Browse files
author
Warren Buckley
committed
POST data to HubSpot API
1 parent 5ab42f4 commit 1ee3100

File tree

1 file changed

+85
-2
lines changed

1 file changed

+85
-2
lines changed

src/Umbraco.Forms.Extensions.Crm.Hubspot/HubspotWorkflow.cs

Lines changed: 85 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
using Newtonsoft.Json;
2+
using Newtonsoft.Json.Linq;
23
using System;
34
using System.Collections.Generic;
45
using System.Net.Http;
5-
using System.Net.Http.Json;
6+
using System.Text;
67
using Umbraco.Core.Composing;
78
using Umbraco.Core.Logging;
89
using Umbraco.Forms.Core;
@@ -38,8 +39,69 @@ private Uri HubspotContactApiUrl
3839
}
3940

4041
static readonly HttpClient client = new HttpClient();
42+
4143
public override WorkflowExecutionStatus Execute(Record record, RecordEventArgs e)
4244
{
45+
// Check Hubspot key is not empty
46+
if (string.IsNullOrWhiteSpace(HubspotApiKey))
47+
{
48+
// Missing an API Key
49+
// TODO: Can I bubble up a specific message as to why
50+
Current.Logger.Warn<HubspotWorkflow>("Workflow {WorkflowName}: No API key has been set for the Hubspot workflow for the form {FormName} ({FormId})", Workflow.Name, e.Form.Name, e.Form.Id);
51+
return WorkflowExecutionStatus.NotConfigured;
52+
}
53+
54+
var fieldMappingsRawJson = FieldMappings;
55+
var fieldMappings = JsonConvert.DeserializeObject<List<MappedProperty>>(fieldMappingsRawJson);
56+
if(fieldMappings.Count == 0)
57+
{
58+
Current.Logger.Warn<HubspotWorkflow>("Workflow {WorkflowName}: Missing Hubspot field mappings for workflow for the form {FormName} ({FormId})", Workflow.Name, e.Form.Name, e.Form.Id);
59+
return WorkflowExecutionStatus.NotConfigured;
60+
}
61+
62+
// Map data from the workflow setting Hubspot fields
63+
// From the form field values submitted for this form submission
64+
var postData = new PropertiesPost();
65+
66+
foreach (var mapping in fieldMappings)
67+
{
68+
var fieldId = mapping.FormField;
69+
var recordField = record.GetRecordField(new Guid(fieldId));
70+
if (recordField != null)
71+
{
72+
// TODO:
73+
// What about different field types in forms & Hubspot that are not simple text ones ?
74+
postData.Properties.Add(mapping.HubspotField, recordField.ValuesAsString(false));
75+
}
76+
else
77+
{
78+
// There field mapping value could not be found.
79+
// Write a warning in the log
80+
Current.Logger.Warn<HubspotWorkflow>("Workflow {WorkflowName}: The field mapping with Id, {FieldMappingId}, did not match any record fields. This is probably caused by the record field being marked as sensitive and the workflow has been set not to include sensitive data", Workflow.Name, mapping.FormField);
81+
}
82+
}
83+
84+
// Serialise dynamic JObject to a string for StringContent to POST to URL
85+
var objAsJson = JsonConvert.SerializeObject(postData);
86+
var content = new StringContent(objAsJson, Encoding.UTF8, "application/json");
87+
88+
// POST data to hubspot
89+
// https://api.hubapi.com/crm/v3/objects/contacts?hapikey=YOUR_HUBSPOT_API_KEY
90+
var postResponse = client.PostAsync(HubspotContactApiUrl, content).Result;
91+
92+
// Depending on POST status fail or mark workflow as completed
93+
if(postResponse.IsSuccessStatusCode == false)
94+
{
95+
// LOG THE ERROR
96+
Current.Logger.Warn<HubspotWorkflow>("Workflow {WorkflowName}: Error submitting data to Hubspot for the form {FormName} ({FormId})", Workflow.Name, e.Form.Name, e.Form.Id);
97+
return WorkflowExecutionStatus.Failed;
98+
}
99+
100+
// TODO:
101+
// Is it worth logging the success that it got created in HubSpot with its ID etc in response
102+
var rawResult = postResponse.Content.ReadAsStringAsync().Result;
103+
104+
43105
return WorkflowExecutionStatus.Completed;
44106
}
45107

@@ -57,7 +119,7 @@ public override List<Exception> ValidateSettings()
57119
// Make a super simple GET request to fetch contacts in HubSpot
58120
// This way with we can verify that the API key is valid
59121
// https://developers.hubspot.com/docs/api/crm/contacts
60-
var testResponse = client.GetAsync(HubspotApiUrl).Result;
122+
var testResponse = client.GetAsync(HubspotContactApiUrl).Result;
61123

62124
if (testResponse.IsSuccessStatusCode == false)
63125
{
@@ -78,6 +140,27 @@ public override List<Exception> ValidateSettings()
78140
}
79141
}
80142

143+
public class PropertiesPost
144+
{
145+
public PropertiesPost()
146+
{
147+
// Ensure we an init an empty object to add straight away
148+
Properties = new JObject();
149+
}
150+
151+
[JsonProperty(PropertyName = "properties")]
152+
public JObject Properties { get; set; }
153+
}
154+
155+
public class MappedProperty
156+
{
157+
[JsonProperty(PropertyName = "formField")]
158+
public string FormField { get; set; }
159+
160+
[JsonProperty(PropertyName = "hubspotField")]
161+
public string HubspotField { get; set; }
162+
}
163+
81164
public class ErrorResponse
82165
{
83166
public string message { get; set; }

0 commit comments

Comments
 (0)