This is how you can handle names in Zapier like "John Smith Jr" and split them into "John" and "Smith Jr".
To watch the tutorial, view the video. Otherwise scroll past it to see the text instructions.
Splitting names into 'first' and 'last' can sometimes lead to inaccuracies, especially with multi-space or hyphenated names.
Splitting names, especially those with spaces, in Zapier can be a tricky endeavor. The internet is filled with tutorials that might get you halfway there, but they often stumble when complex names are thrown into the mix.
If you've ever tried to split a name like "John Smith Jr." or even more complex names like "Melissa Smith-Fremont", you'll know the pain of having parts of the name just disappear into the ether. This becomes a real problem, especially when feeding this data into other systems.
Before we dive into our approach, it's worth noting that there are some quality resources out there. One of them is by Jimmy Rose, who offers an excellent tutorial on splitting names into first, middle, and last. Our solution takes inspiration from his method, with a few tweaks for our specific challenge.
To understand our solution, let's first discuss the old approach:
This method can be useful for certain use cases, but oftentimes leaves some gaps when trying to move data between systems.
Here's where our solution truly shines:
1. Setting the Stage: Set up your input for the step (whether it's the initial trigger or prior action).For our tutorial, let's consider a column called 'full name' from a Google Sheet.
2. The Code Step: Instead of the traditional formatter, we're going to employ the "code by Zapier" step and use JavaScript. Don't fret! Even if you're not a developer, this solution is both simple and elegant.
Copy and paste into a Zapier code step. Make sure to configure the input correctly!
//MAKE SURE TO SET YOUR INPUT IN ZAPIER TO 'name' and the value mapped to the input field with the full name. var names = input.name.split(' '); if (names.length > 2) { output = [{firstname: names[0], lastname: names.slice(1).join(' ')}]; } else if (names.length < 2) { output = [{firstname: names[0], lastname: ''}]; } else { output = [{firstname: names[0], lastname: names[names.length - 1]}]; }
3. Testing: To ensure accuracy, you can test with various names. For instance, for "Melissa Smith-Fremont", the solution correctly splits it into "Melissa" as the first name and "Smith-Fremont" as the last, despite the hyphen.
Hope this was helpful! Code by Zapier steps can open up a lot of possibilities and save tremendous amounts of tasks.