Hallo Everyone,
thanks for having a look on my problem.
My goal: I have a date property and want to increase that date on one day with the help of custom code.
For example: Datefield is 10/08/2023 before the Code and after running the code it’s 10/09/2023 (+ 1 day)
The Code works correctly and gives me the right output.
Problem: I dont get it to update the property (‘contract_start_date’) within the Deal. When I enroll the workflow, the property stays untouched.
My Workflow looks like this:
What am I doing wrong?
Huge thanks in advance for your help!
Update: I get this Workflow error, so maybe something with the code / correct date format is wrong?
The Code looks like this:
import os
from hubspot import HubSpot
from hubspot.crm.contacts import ApiException
from datetime import datetime, timedelta
def main(event):
# Use inputs to get data from any action in your workflow and use it in your code instead of having to use the HubSpot API.
contract_start_date = event.get("inputFields").get("contract_start_date")
# Convert timestamp to a datetime-object
contract_start_date = datetime.fromtimestamp(int(contract_start_date) / 1000)
# add 1 day to the current date
new_date = contract_start_date + timedelta(days=1)
# convert to the right format (MM/DD/YYYY)
new_date_formatted = new_date.strftime('%m/%d/%Y')
# Set new date as the value of the date-property
event['inputFields']['contract_start_date'] = new_date_formatted
return {
"outputFields": {
"contract_start_date": new_date_formatted # Hier verwenden wir den formatierten String
}
}
Hello @henryt1
change the code to update the outputFields object instead of the inputFields object. Here is the updated code.
import os
from hubspot import HubSpot
from hubspot.crm.contacts import ApiException
from datetime import datetime, timedelta
def main(event):
# Use inputs to get data from any action in your workflow and use it in your code instead of having to use the HubSpot API.
contract_start_date = event.get("inputFields").get("contract_start_date")
# Convert timestamp to a datetime-object
contract_start_date = datetime.fromtimestamp(int(contract_start_date) / 1000)
# add 1 day to the current date
new_date = contract_start_date + timedelta(days=1)
# convert to the right format (MM/DD/YYYY)
new_date_formatted = new_date.strftime('%m/%d/%Y')
# Set new date as the value of the date-property
event['outputFields']['contract_start_date'] = new_date_formatted
return {
"outputFields": {
"contract_start_date": new_date_formatted # Hier verwenden wir den formatierten String
}
}
After updating the code, you should be able to successfully update the contract_start_date property in the deal.
Hello Himanshu,
thank you really much for your quick answer! When I update the code, it gives me this error:
Is there somethin I could do?
I found the solution: the date-output from my code wasn’t formatted correctly:
It needed to be in this way: UTC midnight aligned unix millisecond timestamps (ex: May 16 2022 would be 1652659200000)
Here’s my updated Code:
import os
from hubspot import HubSpot
from hubspot.crm.contacts import ApiException
from datetime import datetime, timedelta
def main(event):
# Verwende die Eingaben, um Daten aus einer Aktion in deinem Workflow zu erhalten und im Code zu verwenden, anstatt die HubSpot API verwenden zu müssen.
contract_start_date = event.get("inputFields").get("contract_start_date")
# Konvertiere den Zeitstempel in ein datetime-Objekt
contract_start_date = datetime.fromtimestamp(int(contract_start_date) / 1000).date()
# Addiere 1 Tag zum aktuellen Datum
new_date = contract_start_date + timedelta(days=1)
# Konvertiere das neue Datum zurück in das erforderliche Format (UTC midnight aligned Unix millisecond timestamp)
new_date_formatted = int(new_date.strftime('%s')) * 1000
# Setze das neue Datum als Wert des Datum-Properties
event['inputFields']['contract_start_date'] = new_date_formatted
return {