I’ve set up a triggered email form using code with help from Steve(Mod) which is shown at the end of this post.
It basically saves the users inputs into a dataset and then creates a new contact with custom fields in contacts being filled in as well, then it sends a triggered email with variables back out to the user.
It all works fine apart from the date on the email variable is shown currently as date and time from the dataset field as we are using the following line:
"startDate": startDate.toString()
Whereas I would prefer to use one of these below so that the date is formatted to just show the date on the triggered email:
"startDate": startDate.toLocaleDateString('en-GB') // Shows as 29/01/2019
"startDate": startDate.toDateString() // Shows as Tue Jan 29 2019 (Prefer this to be Day-Date-Month-Year if possible using hook or js module)
However, when either of the above two lines are used in the code instead of the existing line, then for some reason it creates two identical contacts for that user.
One showing new contact with only the label shown, with the other one being their last activity on the website and all the inputs from the input form.
Now obviously it shouldn’t be splitting the one contact into two seperate contacts, plus currently there is no way of merging duplicate contacts. So, there must be a simple fix for this!
Can I just change the existing code somewhere or add something like this into it:
let item = $w("#JoinUsForm").getCurrentItem();
let startDate = item.startDate;
startDate = startDate.toDateString(); // or startDate.toLocaleDateString('en-GB');
Or can I add it somehow in a BeforeInsert hook through the dataset and have it as a data.js in my backend? (I currently have one already setup for making sure that first letter of first and last names are caps on different datasets).
Or can I install node packages in my backend and import either Moment.js or DatePicker.js into the page code or the hook in backend? (I have already tried installing both and testing them in my data.js in backend, however it is still creating duplicates, so no doubt the coding for it was wrong and it didn’t work soI have uninstalled both and taken back out the code.)
This is the code that I am using for my form, creating contact and sending triggered email:
import wixCRM from 'wix-crm';
$w.onReady(function () {
$w("#JoinUsForm").onAfterSave(() => {
let startDate = $w("#startDate").value;
let firstName = $w('#firstName').value;
let lastName = $w('#lastName').value;
let email = $w("#email").value;
let choirRole = $w("#choirRole").value;
let readMusic = $w("#readMusic").value;
let choirBefore = $w("#choirBefore").value;
let startNow = $w("#startNow").value;
wixCRM.createContact({
"firstName": firstName,
"lastName": lastName,
"emails": [email],
"Choir Role": choirRole,
"Read Music": readMusic,
"Choir Before": choirBefore,
"Start Now": startNow,
"Start Date": startDate
})
.then((contactId) => {
return wixCRM.emailContact('joiningusform', contactId, {
"variables": {
"firstName": firstName,
"email": email,
"choirRole": choirRole,
"readMusic": readMusic,
"choirBefore": choirBefore,
"startNow": startNow,
"startDate": startDate.toString()
}
});
})
.catch((err) => {
// handle the error if the email wasn't sent
console.log(`Error: ${err}`);
});
});
});