⚙ Operations Hub

ShlomiGani
Participant | Diamond Partner
Participant | Diamond Partner

Custom code / Format Data - split full name property

SOLVE

Hi everyone,

Unfortunately I didn't find any split command in the format data option.

We do pay for OPS enterprise and I would like to keep this action in house instead of exporting it to Make.com / Zapier.

Has anyone ever tried to create custom code for this split and can share it with me? (I usually work with no code / low code platforms)

1 Accepted solution
ChrisoKlepke
Solution
Key Advisor | Elite Partner
Key Advisor | Elite Partner

Custom code / Format Data - split full name property

SOLVE

Hey @ShlomiGani , 

 

sure, let me show you how to set up the custom code action with some screenshots.

 

The most important stuff is the following:

  • Language: Python 3.9
  • Properties to include: Choose the property that holds your Full Name values and give it key input_string
  • Code: Copy and paste the provided code
  • Data output: Create two data outputs as string values and call the first_name and last_name 

Screenshot 2023-01-12 at 09.13.03.png

Screenshot 2023-01-12 at 09.13.17.png

You then can test it with a contact. 

 

To use it in the First Name and Last Name properties, create two additional workflow actions with Copy Property Value. Here you can copy the value from the output strings to the respective property. 

Screenshot 2023-01-12 at 09.14.07.png

 

If you found this post helpful, consider helping others in the community to find answers faster by marking this as a solution. I'd really appreciate it. 

 

Cheers, 

Chriso

View solution in original post

9 Replies 9
ChrisoKlepke
Key Advisor | Elite Partner
Key Advisor | Elite Partner

Custom code / Format Data - split full name property

SOLVE

Just noticing the title of the thread 😅 Here it is for full name getting first and last name. 

 

def main(event):

    full_name = event.get("inputFields").get("input_string")

    name_list = full_name.split(" ")

    first_name = name_list[0]

    last_name = name_list[-1]

    return {
        "outputFields": {
            "first_name": first_name,
            "last_name": last_name,
        }
    }
ShlomiGani
Participant | Diamond Partner
Participant | Diamond Partner

Custom code / Format Data - split full name property

SOLVE

Hi Chrisotoph, Tnx for your quick answers.

Should I replace the green quets with the property lables?

As it is I get an eror .

[ERROR] AttributeError: 'NoneType' object has no attribute 'get'
Traceback (most recent call last)

 

ChrisoKlepke
Solution
Key Advisor | Elite Partner
Key Advisor | Elite Partner

Custom code / Format Data - split full name property

SOLVE

Hey @ShlomiGani , 

 

sure, let me show you how to set up the custom code action with some screenshots.

 

The most important stuff is the following:

  • Language: Python 3.9
  • Properties to include: Choose the property that holds your Full Name values and give it key input_string
  • Code: Copy and paste the provided code
  • Data output: Create two data outputs as string values and call the first_name and last_name 

Screenshot 2023-01-12 at 09.13.03.png

Screenshot 2023-01-12 at 09.13.17.png

You then can test it with a contact. 

 

To use it in the First Name and Last Name properties, create two additional workflow actions with Copy Property Value. Here you can copy the value from the output strings to the respective property. 

Screenshot 2023-01-12 at 09.14.07.png

 

If you found this post helpful, consider helping others in the community to find answers faster by marking this as a solution. I'd really appreciate it. 

 

Cheers, 

Chriso

MHarp
Participant

Custom code / Format Data - split full name property

SOLVE

Hi Christoph, I have done as you show in the screenshots and get this error message : 

 

Property to include in code is input_string
My two outputs are first_name, last_name
Language is python 3.9 and I have the property correctly mapped to include in code. Copied and pasted verbatim from above.

[ERROR] AttributeError: 'NoneType' object has no attribute 'split'
Traceback (most recent call last):
  File "/var/task/hubspotHandler.py", line 4, in hubspot_handler
    return file.main(event)
  File "/var/task/file.py", line 5, in main
    name_list = full_name.split(" ")
Memory: 37/128 MB
Runtime: 10.59 ms

 

0 Upvotes
LRiley32
Participant

Custom code / Format Data - split full name property

SOLVE

@ChrisoKlepke 

when implementing the code and "first name" contains only one name, it duplicates the first name to the last name.

 

Is there a way to prevent the code from running if there is no space and last name?

ShlomiGani
Participant | Diamond Partner
Participant | Diamond Partner

Custom code / Format Data - split full name property

SOLVE

@ChrisoKlepke 
There are some names that have more than one word, so I changed the code so the first name will be the first word, but the last name will be the rest  

 

 

last_name = full_name.replace(first_name, "")

 

 

ChrisoKlepke
Key Advisor | Elite Partner
Key Advisor | Elite Partner

Custom code / Format Data - split full name property

SOLVE

@ShlomiGani yeah, I was expecting such a problem. The issue I see is that you never know if it is two first names or last names. Maybe depending on the culture you're operating in, one or the other is more likely. But in any case: The way you're suggesting makes sure nothing gets lost.

The other approach could be to write an if condition in the code that returns the main function and notifies a team member to take care of that manually. Of course, doesn't scale that well. 

What I'm trying to say is that no solution is entirely foolproof and there are advantages and disadvantages to everything. Thank you for the feedback and conversation, though. 

 

Cheers, 

Chriso

ShlomiGani
Participant | Diamond Partner
Participant | Diamond Partner

Custom code / Format Data - split full name property

SOLVE

Amazing.

Thank you very much 🙂 

I really appreciate it 

ChrisoKlepke
Key Advisor | Elite Partner
Key Advisor | Elite Partner

Custom code / Format Data - split full name property

SOLVE

Hey @ShlomiGani , 

 

what is it that you need to split and what results do you need. For example, in this short Python code for a custom code action, we use an input field (assuming it is a string) and split it at spaces. The result then would be a list. So if the input string is:

 

"Never gonna give you up Never gonna let you down"

 

The result would be:

 

['Never', 'gonna', 'give', 'you', 'up', 'Never', 'gonna', 'let', 'you', 'down']
 
Here is the code:
def main(event):

    input_string = event.get("inputFields").get("input_string")

    output_list = input_string.split(" ")

    return {"outputFields": {"output": output_list}}

 

But I assume there might be more to it depending on what your input data is and what output you expect. 

 

Cheers, 

Chriso