Triggered Email - On Insert

Hello! I am using a hook to insert data from a custom form into a database. I need to be alerted when a new task is submitted, and I would like a confirmation receipt to be sent to the user. My form is working inasmuch as the data is inserted into the database, but the triggered email is not working. The code I’ve pasted below is my attempt at sending the triggered email to the user. Can anyone help me fix this code AND add the code that will send me a copy of the same Triggered email? THANKS IN ADVANCE FOR YOUR HELP!

import wixUsers from ‘wix-users’;
import wixData from ‘wix-data’;
import wixLocation from ‘wix-location’;

$w.onReady( function () {
let user = wixUsers.currentUser;
let userId = user.id; // “r5cme-6fem-485j-djre-4844c49”
let isLoggedIn = user.loggedIn; // true
user.getEmail()
.then( (email) => {
let userEmail = email; // “user@something.com
} );
user.getPricingPlans()
.then( (pricingPlans) => {
let firstPlan = pricingPlans[0];
let planName = firstPlan.name; // “Gold”
let startDate = firstPlan.startDate; // Wed Aug 29 2018 09:39:41 GMT-0500 (Eastern Standard Time)
let expiryDate = firstPlan.expiryDate; // Thu Nov 29 2018 08:39:41 GMT-0400 (Eastern Daylight Time)
let date = startDate;
let day = date.getDate();
let month = date.getMonth()+1;
let year = date.getFullYear();
let dateStr = year + month + day;
console.log(dateStr); // show result in the developers console
$w(“#planId”).value = planName + “_” + dateStr;
} );
user.getEmail()
.then((email) => {
let userEmail = email;
$w(“#email”).value = userEmail;
});

$w("#submit").onClick(() => { 

let toInsert = {
“firstName”: $w(“#firstName”).value,
“lastName”: $w(“#lastName”).value,
“companyName”: $w(“#companyName”).text,
“email”: $w(“#email”).value,
“clientId”: $w(“#clientId”).text,
“requestType”: $w(“#requestType”).text,
“requestDetails”: $w(“#comments”).value,
“clientUpload”: $w(“#uploadButton1”).value,
“clientUploadImage”: $w(“#uploadButton2”).value,
“planId”: $w(“#planId”).value
};

    wixData.insert("taskForm", toInsert) 
        .then(() => { 
        wixLocation.to("/dashboard"); 
        wixUsers.emailUser("vaTaskReceipt", userId, { 
      variables: { 

“email”: $w(“#email”).value,
“firstName”: $w(“#firstName”).value,
“lastName”: $w(“#lastName”).value,
“companyName”: $w(“#companyName”).text,
“comments”: $w(“#comments”).value
}
} )
})
. catch ((err) => {
let errorMsg = err;
});
});
});

Looks like you’re trying to send the email after you send the browser to the dashboard page. Try moving the to() function to after sending the email.

If you want to send an email to yourself as well, you’ll need to send it using the backend version of emailUser(). From frontend code, you can only email the currently logged in user (who in this case is not you).

Thanks, Sam. I tried the first suggestion, but I must be missing something because after I rearranged the code, the to() function stopped working and an email still wasn’t sent to the user. Here’s what I changed it to:

  wixData.insert("taskForm", toInsert)
            .then(() => {
                wixUsers.emailUser("vaTaskReceipt", userId, {
                variables: {
 "email": $w("#email").value,
 "firstName": $w("#firstName").value,
 "lastName": $w("#lastName").value,
 "companyName": $w("#companyName").text,
 "comments": $w("#comments").value
                }
                })
            .then(() => {
             wixLocation.to("/dashboard");
            })
             .catch((err) => {
 let errorMsg = err;
            });
        });
    });
});

Could you tell me what needs to change with the code I updated; and provide me with the code I need to send an email to myself?

Are you actually using before or after insert datahook in your data.js file in your Backend?
https://www.wix.com/corvid/reference/wix-data.Hooks.html

Or are you just using Wix Data and it’s insert function?
https://www.wix.com/corvid/reference/wix-data.html#insert

If you are simply saving the user inputs from a form into a dataset, then you can either just use a submit button connected to the dataset itself.
https://support.wix.com/en/article/adding-a-submit-button-to-your-form

Or you can simply use Wix Data and it’s save function.
https://www.wix.com/corvid/reference/wix-data.html#save

Then you can just use a triggered email to members with Wix Dataset onAfterSave function.
https://support.wix.com/en/article/corvid-tutorial-sending-a-triggered-email-to-members
https://www.wix.com/corvid/reference/wix-dataset.Dataset.html#onAfterSave

I have used similar for contacts on a site where the procedure was to have the inputs submitted into the form, then to create the contact itself within the WIx CRM and then to use a triggered email to send to the contact with all required variables etc.

Or you can do what Yisrael has suggested on this other forum post and use a code tutorial that sends two emails out through the use of a .jsw and a .js file in your Backend, or through a NodeJS in Wix Package Manager or by using SendGrid’s own REST API.
https://www.wix.com/corvid/forum/community-discussion/invite-mail-in-wix