Create Task
Tags: #hubspot #sales #crm #engagements #task
Author: Alok Chilka

Input

Import libraries

1
from datetime import datetime, timedelta
2
import requests
3
import json
Copied!

Setup your HubSpot

👉 Access your HubSpot API key
1
HS_API_TOKEN = "YOUR_HUBSPOT_API_KEY"
Copied!

Setup your task info

1
# Assign owner ID
2
owner_id = ""
3
4
# Time delay to set due date for tasks in days
5
time_delay = 10
6
7
# Task data
8
subject = "My first task"
9
body = "Call contacts"
10
status = "NOT_STARTED" # NOT_STARTED | COMPLETED | IN_PROGRESS | WAITING | DEFERRED
Copied!

Model

Function to create task

1
def create_task(owner_id,
2
time_delay,
3
subject,
4
body,
5
status,
6
asso_contactids=[],
7
asso_companyids=[],
8
asso_dealids=[],
9
asso_ownerids=[],
10
engagement="TASK"):
11
"""
12
Engagement type = TASK | NOTE | EMAIL | MEETING | CALL
13
"""
14
15
# Calc timestamp
16
tstampobj = datetime.now() + timedelta(days=time_delay)
17
tstamp = tstampobj.timestamp() * 1000
18
19
# Create payload
20
payload = json.dumps({
21
"engagement": {
22
"active": 'true',
23
"ownerId": owner_id,
24
"type": engagement,
25
"timestamp": timestamp
26
},
27
"associations": {
28
"contactIds": asso_contactids,
29
"companyIds": asso_companyids,
30
"dealIds": asso_dealids,
31
"ownerIds": asso_ownerids,
32
},
33
"metadata": {
34
"body": body,
35
"subject": subject,
36
"status": status,
37
}
38
})
39
url = "https://api.hubapi.com/engagements/v1/engagements"
40
params = {"hapikey": HS_API_TOKEN}
41
headers = {'Content-Type': "application/json"}
42
# Post requests
43
res = requests.post(url,
44
data=payload,
45
headers=headers,
46
params=params)
47
# Check requests
48
try:
49
res.raise_for_status()
50
except requests.HTTPError as e:
51
raise (e)
52
res_json = res.json()
53
54
# Fetch the task id of the current task created
55
task_id = res_json.get("engagement").get("id")
56
print("🎆 Task created successfully: ", task_id)
57
return res_json
Copied!

Output

Create task

1
create_task(owner_id,
2
time_delay,
3
subject,
4
body,
5
status)
Copied!
Copy link
Edit on GitHub