Getting error on data outputs in custom code

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

Hi @ASund-Nielsen,

The custom coded actions in workflows expect the output to be returned in JSON format with “outputFields” nested dictionary.

For example, instead of this:

if not deal_identifier:
 debug_outputs["error"] = "deal_identifier is not being passed correctly from the workflow."
 return debug_outputs

Try replacing with this structure:

 if not deal_identifier:
 return {
 "outputFields": {
 "message": "deal_identifier is not being passed correctly from the workflow.",
 "debug_new_value": None
 }
 }

I replaced the “error” with “message” since I do not see “error” being one of your outputs, but you can customize to what your output fields need to be.

This would apply to all of the return statements for the outputs.

Hope this helps!

Hi @ASund-Nielsen

You have to define output fields in the code and make sure to use the same name defined
eg instead of directly returning the debug_outputs you have to write

return {
 "outputFields": {
 "debug_current_value": "value you want.",
 "debug_new _value": "value you want.",
 "debug_pipeline": "value you want.",
 "message": "value you want."
 }
 }

I hope this will help you out. Please mark it as Solution Accepted and upvote to help another Community member.
Thanks!