[✅ Solution] Email fields. How to call in a calculation?

Hey everyone,

There are two ways to collect email addresses

  • with the email block
  • with a single line text block

I’m looking for clarity as to when using the email block is a better idea. Historically, I have always used the single line text block but I am noticing that on import, my data came in using the email block.

The above is a screenshot from a “clients” app. I am calling the email field in an related estimate app. The estimate as an “optional email” field in case we need to email someone who is not the primary contact.

If I call the [@all of client email] field nothis is rendered, even though there is an email contained.

1 Like

In the process of writing this up I was able to solve my own problem. I decided to post anyway so everyone can reference it in the future.

You need to add the following after your [@all of email field]

.map(function (obj) { return obj.value}).join()

1 Like

Is there a way to return the first email address when there are multiple in the email field?

Hi @1F2Ns,

glad to hear you were able to figure this out - it’s indeed not too easy as the email field is provided as an array of objects to allow more complex manipulations inside the calculation field.

Let me give you some code examples; You already figured out how to show all email addresses.

In order to show only the first one, just use [0] instead of .join(...):

You could also use the additional info to yield the first work email address:

The final result would look like this:

Hope that helps!

PS: A simple and effective debugging advice for calculation fields: You may use JSON.stringify() to stringify any available token and see what value it has at runtime. The output is rather technical and not user friendly, but can help to solve issues and debug more quickly.

2 Likes

Thank you sir

I have a very broken understanding of JavaScript as most of what I’ve done over the years has been in PHP, HTML, and CSS.

1 Like

Hi @Tim it looks like, the ordering in the UI (which is drag and drop) will also sort the order in the array? Thats good to know, to we can define it as rule for syncinc apps, that only the first email will be synced at all times.

Could you please also add, how I can overwrite the first value in the array with the automation? The UI will not allow it in automations, I need a calculation then?

1 Like

@dirk_s,
The drag and drop order of multiple emails of an email field indeed causes the email addresses to be in that order inside the array that you get inside the automations and the calculation field.

Your second question is a little bit more tricky. It is currently only possible to get the first email of an email field via a calculation action: contact_field_email_iterable[0].value. The iterable variable gives you the array of the email addresses, we extract the first via [0] and access the value property (there is also a type property which contains the type of email “Work”, “Home” and so on).

Here is a screenshot of an automation that synchronizes two contact apps:

I hope I could answer your question!

Cheers,
Ben

Hi Ben,
I was also able to get the first E-Mail (top of the list) when using the first entry from field “…_email” with

const email = contact_field_e_mail_email.split(",")[0].trim();

For the second question, this might be a missunderstanding. I asked about writing into the e-mail field. Focus is to not create duplicates.

I receive the updated e-mail from the other system (only one, as there is only one). To complete the sync - based on the assumption I sync first position only - I would need to either add it to the first position, or move it to the first position (if its already in the array).

As I don’t want to blindly add, I would need to select “calculation”?

I could prepare the variable with the right syntax before, but what is expected for “E-Mail”? An array in the same style as the output of the “…_concatenated” field value?

Thanks for the clarification @dirk_s , now I understand the use-case.

Here is an updated version of the automation:

As you can see we have to check via the script action whether the email already exists in the array (needs to be pushed to the top) or if it does not exist yet (needs to be added at the top).
Here is the code to to this:

const new_email = demo_contact_field_email_email;
const existing_emails = collected_demo_contact_field_email_iterable;

// Check if the new email already exists
const index = existing_emails.findIndex(({ value }) => new_email === value)
if (index > -1) {
    // Email already exists, move to the beginning
    const [email] = existing_emails.splice(index, 1);
    var_result = [email, ...existing_emails]
}
// Email does not exist yet, add to the beginning
else {
    var_result = [{ type: 'work', email: new_email }, ...existing_emails]
}

We then use the var_result variable from the script action and use it to set the value of the email field. Note that we first clear the email field and then add the emails from var_result. The update action allows multiple operations to be performed on a single field to compute the final value of the update :slightly_smiling_face:

Note: you can see how the data needs to be shaped for the email update in the Developer documentation: Email Field Value | Tape Developers

Cheers,
Ben

2 Likes