Transform full name to First and Last name

Comments

5 comments

  • Kathyana Queeman Community moderator Technical Writer
    Engaged
    Celigo University Level 2: Seasoned

    Hello Tim Krull

    I've moved your post to the Troubleshoot custom flows section for more visibility. Thanks! 

    0
  • Steve Klett Strategic Partner
    Celigo University Level 4: Legendary
    Answer Pro
    Great Answer
    Top Contributor
    Awesome Follow-up
    Engaged

    Tim Krull 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.

    0
  • Tyler Lamparter Principal Product Manager
    Awesome Follow-up
    Engaged
    Top Contributor
    Answer Pro
    Celigo University Level 4: Legendary

    Steve Klett one-upped my last handlebar expression so here is my attempt to be level the field.

     

    Tim Krull 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}}"
    }

    0
  • Steve Klett Strategic Partner
    Celigo University Level 4: Legendary
    Answer Pro
    Great Answer
    Top Contributor
    Awesome Follow-up
    Engaged

    Tyler Lamparter 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!

    0
  • Tyler Lamparter Principal Product Manager
    Awesome Follow-up
    Engaged
    Top Contributor
    Answer Pro
    Celigo University Level 4: Legendary

    Steve Klett 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}}"
    }

    0

Please sign in to leave a comment.