Workflow javascript to parse first name and last name fields from query to add as seperate columns in a table

I'm trying to use javascript on a web hook to add key value pairs for first name and last name that will be pulled from the primary contact name (if present) or from the project name if there is not a primary contact for the current Company Cam Project

  • not every project contains a primary contact
  • if a primary contact key is present then use the part of the primary contact before the first space and set it as firstname
  • similar process to declare lastname from the section after the space
var firstname = ''
var lastname = ''
if (query4.data.hasOwnProperty('primary_contact')) {firstname = query4.data.primary_contact.name.substring(0, query4.data.primary_contact.name.indexOf(' '))} 
else{ firstname = query4.data.name //will parse later 
    }
query4.push(["firstname" , firstname], ["lastname",lastname]);

return query4.data

needing a way of adding key value pairs into the mix for firstname and lastname. It seems to be declaring them now and adding the string values to them but how can i add this data into the response to be used later for the insert to table on the next step?

then if the query4.data.primary_contact key does not exist I'll parse the project name rather then primary contact name

Solved, Isaac is the best!!!
here is the code to separate the first and last name based on the "Primary_Contact" key if available or the job name if not.

var firstname = ''
var lastname = ''
if (query4.data.hasOwnProperty('primary_contact')) {firstname = query4.data.primary_contact.name.substring(0, query4.data.primary_contact.name.indexOf(' '))
lastname = query4.data.primary_contact.name.substring( query4.data.primary_contact.name.indexOf(' ')+1)}
else {firstname = query4.data.name.substring(0, query4.data.name.indexOf(' '));
lastname = query4.data.name.substring( query4.data.name.indexOf(' ')+1)}
var fullName = query4.data
fullName.firstname = firstname;
fullName.lastname = lastname;
return fullName

1 Like