Thanks Yisrael for all your help.
I used import { contacts } from ‘wix-crm’ ;
The page has a simple form I made with email, FirstName, LastName and CellNumber. I want to send an email when the form is submitted, I use TriggeredEmail so a CONTACT is required. I found examples for this, but had trouble. The table is not CONTACTS but CONTACT, under databases. I assume it existed by default or somehow importing wix-crm created it? Here’s my code that executes when you submit the form.
import wixLocation from 'wix-location';
import wixCRM from 'wix-crm';
import { triggeredEmails } from 'wix-crm';
import {memory} from 'wix-storage';
import { contacts } from 'wix-crm';
$w.onReady(function () {
let TeamId = memory.getItem("TeamId");
let TeamName = memory.getItem("TeamName");
$w("#text63").text = TeamName;
$w('#dataset1').onAfterSave( () => {
console.log("onReady dataset1.onAfterSave()");
let TeamName = memory.getItem("TeamName");
let InviteeName = $w("#input2").value + " " + $w("#input3").value;;
let firstName = $w('#input2').value;
let lastName = $w('#input3').value;
let email = $w('#input4').value;
let phone = $w('#input5').value;
console.log("SENDING EMAIL"+" mailto:"+email+" TeamName-"+TeamName+" phone="+phone+" InviteeName="+InviteeName);
let contactInfo = {
name: {
first: $w('#input2').value,
last: $w('#input3').value
},
emails: [
{
email: $w('#input4').value,
}
],
phones: [
{
tag: 'MOBILE',
countryCode: 'US',
phone: $w('#input5').value,
primary: true
}
]
};
console.log("contactInfo set "+contactInfo)
// if email exists already append if not creat it
contacts.appendOrCreateContact(contactInfo)
.then( (result) => {
console.log("Contact Created contactId="+result.contactId); // THIS MESSAGE SHOWS CORRECTLY
// I think TriggeredEmails is a wrapper for wixCRM ?
//triggeredEmails.emailContact('Sosgvzd', contactId, { //SrhtD9P // Sosgvzd
wixCRM.emailContact('Sosgvzd', result.contactId, { //SrhtD9P // Sosgvzd SsLTwsg
"variables": {
"member":"a HelpGrandma member",
"InviteesName": InviteeName,
"TeamName": TeamName
}
})
.then( () => {
console.log("Triggered Email SENT"); // BEFORE, THIS MESSAGE NEVER SHOWS, I GOT ERROR "FAILED TO FETCH" -- NOW FIXED
$w("#text63").text = "Email SENT";
// FOLLOWING CODE IS EXECUTED ON SUCCESS OF EMAIL SEND, I CALL THIS PAGE FROM 2 PLACES, AND IT MUST RETURN ONE OF 2 PLACES BASED ON "from" in memory
if(memory.getItem("from") == "Create-Team"){
//import wixLocation from 'wix-location';
wixLocation.to("/account/Create-a-Team-3");
}else{
wixLocation.to("/account/Edit-Team");
}
})
} )
.catch( (err) =>{
let errorMsg = err;
console.log("TRIGGERED EMAIL ERROR-->",errorMsg);
});
}); // onAfterSave
$w("#button2").onClick( (event) => {
// first save data, then onAfterSave() event will handle the rest
$w('#dataset1').setFieldValues( {
"title": TeamName,
"firstname": $w("#input2").value,
"lastname": $w("#input3").value,
"email": $w("#input4").value,
"cell": $w("#input5").value
});
$w('#dataset1').save();
}); // BUTTON2 CLICK END
}); // ONREADY END
I have another problem Yisrael…
I need to tag my Triggered Email with some data, specifically the MemberID and TeamName from the Member who sends this INVITATION email. It’s an invitation to be part of a TEAM.
When the email recipient clicks on the button in the email, I want to send this data back to the Members page like this (or somehow)
…/account/TeamAccept/${MemberID},${TeamName}
and I want to use this to lookup the proper Team to REGISTER the Invitee to the TEAM. I figure just a regular Query of the TEAMMEMBERS table to do this.
My problem is how to make the URL hold the data or send it back from the email. then what code EXTRACTS it from the URL? Do I need a RESTAPI? or use HTTP get and put? or JSON?