For a more general-purpose article on sending an email using a 3rd party email service, see How to Send an Email on Form Submission.
Prerequisites
- name
- Interest_Area
- At the top of the snippet there is an
statement. This line needs to be added to the very top of the code on the page where you will be using the rest of the snippet. It imports the library of functions that lets your code work with the Triggered Email functionality.import
- The contact ID and the values of the variables (
andname
) are reflected in the code snippet with placeholders. These placeholder values will need to be replaced with real values in your actual code.interest_area
Add a Custom Field to Your Contacts List
Form
Code
- Add the import statement to the top of the code where we use the snippet.
- Add an onClick event handler to the button. The code we add to that handler function will create the new contact and email them.
- Add code that creates the new contact and gives us the contact's ID.
- Add the snippet code to the handler function and replace the placeholder values.
- Add some code to handle success and errors.
Triggered Emails may not work properly when previewing your site. Publish your site to test the code found below.
import wixCrm from 'wix-crm';
to the top of the code in the page where you want to use the snippet.onClick()
event handler to the signUpButton that runs each time it's clicked.wixCRM.createContact({
"firstName": $w('#nameInput').value,
"emails": [$w("#emailInput").value],
"interest_area": $w("#interestArea").value
})
createContact
function returns the contactID
for the newly created contact. We'll use that ID in our snippet to identify the new contact and email them.createContact
function returns a promise, so we'll add our snippet after it. Now that we have the contactID
we will use it in the snippet in place of <enter-contact-ID-here>
.name
and interest_area
variables in the variables
object.export function signUpButton_click(event) {
wixCRM.createContact({
"firstName": $w('#nameInput').value,
"emails": [$w("#emailInput").value],
"interest_area": $w("#interestArea").value
})
.then((contactId) => {
wixCRM.emailContact("newsletter_signup", contactId, {
"variables": {
"name": $w('#nameInput').value,
"interest_area": $w("#interestArea").value
}
})
})
}
emailContact()
function returns a Promise, we can define what happens when the Promise resolves successfully or rejects with an error..then
and a .catch
to handle these.import wixCRM from 'wix-crm';
$w.onReady(function () {
});
export function signUpButton_click(event) {
wixCRM.createContact({
"firstName": $w('#nameInput').value,
"emails": [$w("#emailInput").value],
"interest_area": $w("#interestArea").value
})
.then((contactId) => {
wixCRM.emailContact("newsletter_signup", contactId, {
"variables": {
"name": $w('#nameInput').value,
"interest_area": $w("#interestArea").value
}
})
.then(() => {
// do something after the email was sent
})
.catch((err) => {
// handle the error if the email wasn't sent
});
});
}