Hey y’all. Hoping you can help me troubleshoot. I’ve got a button on dynamic page named ‘contactEmail’. The button’s label is connected via the dynamic dataset. The dynamic page pulls data from my ‘Projects’ collection, which has a reference field ‘customer’ that pulls data from the ‘Team’ collection. I’ve written the following code to dynamically set the onClick function to direct the user to the customers email. I was able to execute the same function successfully with a phone number, but for some reason I can’t get the email to work.
I did try formatting the email with and without the ‘<>’. The Velo Reference article has it written as such, so I followed this example:
mailto:<address>@<someplace.com>?subject=<subject>
The console log confirms that the button onClick event works, it is pulling the customers email from the collection correctly, and it is formatting the email and emailtoLink as intended. Any input on what may be wrong?
$w.onReady(function () {
// Add event handler for the onClick event of the contactEmail element
$w('#contactEmail').onClick(() => {
console.log('Email button clicked');
// Get the current item from the dynamic dataset (Projects collection)
const currentItem = $w('#dynamicDataset').getCurrentItem();
// Check if the current item exists and has a customer with an email address
if (currentItem && currentItem.customer) {
// Get the ID of the customer from the current project
const customerId = currentItem.customer._id;
// Query the Team collection to get the customer's email address
wixData.query('Team')
.eq('_id', customerId)
.find()
.then(customerResult => {
const customer = customerResult.items[0]; // Assuming there's only one customer for the given ID
const email = customer.email;
// Extract sender text and domain name from the email address
const atIndex = email.indexOf('@');
const senderText = email.substring(0, atIndex);
const domainName = email.substring(atIndex + 1);
// Construct the formatted email
const formattedEmail = `${senderText}>@<${domainName}`;
console.log('Formatted email:', formattedEmail);
// Construct the mailto link with subject enclosed in '<' and '>'
const mailtoLink = `mailto:<${formattedEmail}>?subject=<Hello>`;
console.log('Email link:', mailtoLink);
// Direct the user's window to the mailto link
wixLocationFrontend.to(mailtoLink);
})
.catch(err => {
console.error('Error fetching customer:', err);
});
} else {
console.error('Customer information or email address not available for this project.');
}
});
});