Triggered email doesn't work

Hello,
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.currentUser.id;
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

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