Custom Code Actions - extracting first three digits from a phone number string
Hi everyone, I am currently trying to get the first three digits from a phone number property in CRM. I need the first three digits of the phone numbers to build a report breaking down call to connect ratios by country. Therefore I was trying to create the following Python script as a custom code action, but I keep getting the same error message.
This is the code:
# Import the required libraries
import hubspot
from hubspot.crm.contacts import SimplePublicObjectInput
from hubspot.crm.contacts.exceptions import ApiException
# Define the function that extracts the first three characters
def extract_country_code(event, context):
# Initialize HubSpot client
api_client = hubspot.Client.create()
# Get the contact ID and phone number from the event data
contact_id = event["objectId"]
mobile_phone_number = event["properties"].get("phone", "")
# Check if the phone number exists and starts with "+"
if mobile_phone_number and mobile_phone_number.startswith("+"):
# Extract the first three characters (country code)
country_code = mobile_phone_number[:3]
else:
# If the format is unexpected, assign an empty string or handle as needed
phone_country_code = ""
# Define the property to update
properties_to_update = {
"phone_country_code": phone_country_code # Replace "phone_country_code" with the actual property name
}
# Update the contact in HubSpot
try:
api_response = api_client.crm.contacts.basic_api.update(
contact_id,
SimplePublicObjectInput(properties=properties_to_update)
)
return {"message": "Country code extracted successfully", "country_code": country_code}
except ApiException as e:
return {"message": f"Failed to update contact: {e}"}
This is the error message:
LAMBDA_WARNING: Unhandled exception. The most likely cause is an issue in the function code. However, in rare cases, a Lambda runtime update can cause unexpected function behavior. For functions using managed runtimes, runtime updates can be triggered by a function change, or can be applied automatically. To determine if the runtime has been updated, check the runtime version in the INIT_START log entry. If this error correlates with a change in the runtime version, you may be able to mitigate this error by temporarily rolling back to the previous runtime version. For more information, see https://docs.aws.amazon.com/lambda/latest/dg/runtimes-update.html
[ERROR] AttributeError: module 'file' has no attribute 'main'
Traceback (most recent call last):
File "/var/task/hubspotHandler.py", line 6, in hubspot_handler
return file.main(event)
Memory: 51/128 MB
Runtime: 10.47 ms
Any ideas of what my mistake is here? Also, if there's an easier way to achieve my report goal, happy to learn :))
You could try reviewing the way you extract and handle the phone number property in the event. Check that the property name for the phone number is correctly mapped, and ensure you have the correct API setup with your HubSpot account. It's important to verify that the "phone_country_code" property exists in HubSpot as well.