I’m new with this community and I am trying to send data leads from Airtable to Gmail but I wan’t to set a Dynamics fields like for example:
__________________ Airtable Data:
First Name: John Last Name: Mike Address:1234
Age: Email: John@sample.com
________________ Gmail:
First Name: {{142106945__John}} Last Name: {{142106945__Mike}} Address: {{142106945__1234}}
(Age: - I want to remove this section if empty including the Age label.) Email: John@sample.com
_________
Thank you.
Best answer by GetUWired
Hi @Clyteroll
To add some more explanation to this, you will need to change your email body type to HTML as opposed to plain. Otherwise, you will end up with odd spacing when a field doesn’t exist.
Here is a real slick way you could write your code
*Add Code Step > Run Javascript above Gmail Step*
Configure your code block like the attached but with your own variables and keys. The code is written to handle any number of combinations.
The code (<br> denotes a line break in HTML):
let conditionalText = "";
for (const [key,value] of Object.entries(inputData)) { if (value) { conditionalText += key + ": " + value + "<br>"; } }
return {conditionalText}
This will return something like the below. Notice the Phone number field isn’t included since the value was blank.
which can be mapped into your email step. Please be sure to change body type to html and put a <br> anywhere you want a line break.
RESULTING EMAIL
Added Phone Value back in (no change needed to code or gmail configuration)
This post has been closed for comments. Please create a new post if you need help or have a question about this topic.
Thank you for responding, and this is an excellent solution, but the problem with this is I had multiple fields like ten fields; if I make this method, I will end up numerous paths, which is not practical because there is a scenario that 2 fields are missing that would be end up just another parth and also, if I tried to change in the future that means I will change all of the paths.
To add some more explanation to this, you will need to change your email body type to HTML as opposed to plain. Otherwise, you will end up with odd spacing when a field doesn’t exist.
Here is a real slick way you could write your code
*Add Code Step > Run Javascript above Gmail Step*
Configure your code block like the attached but with your own variables and keys. The code is written to handle any number of combinations.
The code (<br> denotes a line break in HTML):
let conditionalText = "";
for (const [key,value] of Object.entries(inputData)) { if (value) { conditionalText += key + ": " + value + "<br>"; } }
return {conditionalText}
This will return something like the below. Notice the Phone number field isn’t included since the value was blank.
which can be mapped into your email step. Please be sure to change body type to html and put a <br> anywhere you want a line break.
RESULTING EMAIL
Added Phone Value back in (no change needed to code or gmail configuration)