I am working to bring over Customers as contacts into our Salesforce instance from Stripe. There is a field mismatch which I am trying to overcome and I am sure has an easy solution, but I need to essentially transform a full name to instead be First Name and Last Name so I can feed them through the flow. I assume this will fall under the handlebars expressions, but could you a point in the right direction for a quick sample or guide. Appreciate any feedback in advance
Hello @timkrull,
I've moved your post to the Troubleshoot custom flows section for more visibility. Thanks!
@timkrull You'll want to use the split helper. The documentation uses an odd example of a hyphenated name, but you would probably use a space as the delimiter. The helper splits AND returns the element identified by index. It's possible you could experience data loss if there is a FML name "Joe Grant Lee" and your mapping assumes 2 elements generated from split, you'll end up store "Joe" and "Grant". You can create more complex expressions with compare and probe to find a null element (and thus your array size), but it becomes ugly quick which is why..... I always use script for anything like this.
Here's an example of the middle name problem I'm describing:
Input data:
{
"name": "Joe Grant Lee"
}
Handlebars:
{
"firstName": "{{split name " " 0}}",
"lastName": "{{split name " " 1}}"
}
Produces:
{
"firstName": "Joe",
"lastName": "Grant",
}
To get the last name you'd need to somehow know there are 3 elements.
@steveklett one-upped my last handlebar expression so here is my attempt to be level the field.
@timkrull this should just about cover each possible combination of converting a name field to first and last. It handles null, spaces at the beginning and end of the name, normal names, greater than 2 word names (everything after the first gets put into last name field), and names where there is only one word.
{
"firstName": "{{#if (trim name)}}{{#if (substring (trim name) 0 (regexSearch (trim name) "\s"))}}{{substring (trim name) 0 (regexSearch (trim name) "\s")}}{{else}}{{trim name}}{{/if}}{{else}}Default First Name{{/if}}",
"lastName": "{{#if (trim name)}}{{#if (substring (trim name) 0 (regexSearch (trim name) "\s"))}}{{substring (trim name) (add (regexSearch (trim name) "\s") 1)}}{{else}}Default Last Name{{/if}}{{else}}Default Last Name{{/if}}"
}
@tylerlamparter Nice work, regex is a great option. In your example lastName has the middle (Grant) and last name (Lee) instead of just "Lee".
Let's see what you got!
@steveklett yeah multi name fields can be tough because you don't know if the extra part of the name should be in the first name, middle name, or last name. Because of that you just end up assuming what it is. That being said, I took your additional challenge and here is a sample for first, middle, and last name!
{
"firstName": "{{#if (trim name)}}{{#if (substring (trim name) 0 (regexSearch (trim name) "\s"))}}{{substring (trim name) 0 (regexSearch (trim name) "\s")}}{{else}}{{trim name}}{{/if}}{{else}}Default First Name{{/if}}",
"middleName": "{{#if (trim name)}}{{#if (substring (trim name) 0 (regexSearch (trim name) "\s"))}}{{#compare (regexSearch (trim name) "\s(?=[^\s]*$)") ">" (regexSearch (trim name) "\s")}}{{substring (trim name) (add (regexSearch (trim name) "\s") 1) (regexSearch (trim name) "\s(?=[^\s]*$)")}}{{else}}Default Middle Name{{/compare}}{{else}}Default Middle Name{{/if}}{{else}}Default Middle Name{{/if}}",
"lastName": "{{#if (trim name)}}{{#if (substring (trim name) 0 (regexSearch (trim name) "\s"))}}{{substring (trim name) (add (regexSearch (trim name) "\s(?=[^\s]*$)") 1)}}{{else}}Default Last Name{{/if}}{{else}}Default Last Name{{/if}}"
}