Hi,
Im using the below custom code for a workflow. The API token has been changed to now show it, but is correctly inserted.
The script runs successfully, but the data outputs show as not defined in code. What am I doing wrong?
# Import required modules
import hubspot
from hubspot.crm.deals import ApiException
def main(event):
# Initialize HubSpot client with hardcoded API token
hubspot_client = hubspot.Client.create(access_token="PRIVATE")
debug_outputs = {} # Dictionary to collect debug logs
try:
# Input data from the workflow
deal_identifier = event.get("inputs", {}).get("deal_identifier") # Retrieve the deal identifier from workflow inputs
debug_outputs["input_deal_identifier"] = deal_identifier # Log the deal identifier passed in the input
if not deal_identifier:
debug_outputs["error"] = "deal_identifier is not being passed correctly from the workflow."
return debug_outputs
try:
# Fetch the current deal data
deal_api = hubspot_client.crm.deals
deal = deal_api.basic_api.get_by_id(deal_identifier, properties=["pipeline", "deal_identifier"])
# Debugging: Log the current pipeline and deal_identifier values
pipeline = deal.properties.get("pipeline")
current_value = deal.properties.get("deal_identifier")
debug_outputs["debug_pipeline"] = pipeline
debug_outputs["debug_current_value"] = current_value
# Ensure the deal is in the specified pipeline
if pipeline != "602537446": # Replace with your target pipeline ID
debug_outputs["skipped"] = True
debug_outputs["message"] = "Deal not in the target pipeline."
return debug_outputs
# Increment the deal_identifier value
if current_value is None:
current_value = 3000 # Start counting at 3000 if the property is not set
else:
current_value = int(current_value) # Convert current value to integer
# Debugging: Log the incremented value
new_value = current_value + 1
debug_outputs["debug_new_value"] = new_value
# Update the deal property
deal_api.basic_api.update(
deal_identifier,
{"properties": {"deal_identifier": new_value}}
)
# Return success message and the new value
debug_outputs["message"] = "Success"
debug_outputs["new_value"] = new_value
return debug_outputs
except ApiException as e:
# Handle API exceptions
debug_outputs["error"] = f"API exception occurred: {e.status} - {e.reason}"
debug_outputs["details"] = str(e.body)
return debug_outputs
except Exception as e:
# Handle unexpected exceptions
debug_outputs["error"] = f"An unexpected error occurred: {str(e)}"
return debug_outputs

