Triggered email doesn't work

I try to set triggered email when sending a form.
I put the code at the end of my onClick() button code but I don’t know if it can work here.

I put this code :

$w(“#dataset1”).onAfterSave( () => {
if (wixUsers.currentUser.loggedIn) {
const userId =;
wixUsers.emailUser(“newChantierMail”, userId, {
variables: {
title: $w(“#input1”).value,
dateDebutChantier: dateDebutFR,
dateFinChantier: dateFinFR,
email: $w(“#input5”).value
} );

Can I put it here or I must put it inevitably in the onReady function?
Thank you

In this case I think that placing it in onReady function is best practice.