Tiggered Email to Contact NOT Working

This is an extension of this thread, https://www.wix.com/corvid/forum/community-discussion/multi-page-form-why-do-we-think-it-is-not-working? , but this a new issue. I am trying to create a contact so that the triggered email sends to Admin. I am not using a third party because I don’t want plain text emails with the form info. I want a stylized template.
Let me know if this is possible.
URL: https://elizabethjhay.wixsite.com/mysite-9/thankyou

//-------------Imports-------------//
import wixData from'wix-data';
import {session} from'wix-storage';
import wixCrm from'wix-crm';
//-------------Form ID Code-------------//
$w.onReady(function () {$
w('#formIdInput').value = session.getItem("formId");
$w('#formtype').value = session.getItem("title");
$w('#business').value = session.getItem("companyName");
$w('#adminEmail').value === "ehey@metest.com"});
//-------------Notification-------------//
exportfunction Home_click(event) { 
wixCrm.createContact({
"firstName": $w('#adminEmail').value,
"emails": [$w("#adminEmail").value]  })  
.then((contactId) => {   
wixCrm.emailContact("RwiGjN4", contactId, 
{"variables": 
{"formId": $w('#formIdInput').value,
"Form_Type": $w("#formtype").value,
"Business_Name": $w("#business").value,     }    })    
.then(() => {// do something after the email was sent    })   
.catch((err) => {// handle the error if the email wasn't sent    });  });}

I also tried this code:

//-------------Imports-------------//
import wixData from'wix-data';
import {session} from'wix-storage';
import wixCrm from'wix-crm';
//-------------Form ID Code-------------//
$w.onReady(function () {
$w('#formIdInput').value = session.getItem("formId");
$w('#formtype').value = session.getItem("title");
$w('#business').value = session.getItem("companyName");
$w('#adminEmail').value = "ehey@metest.com"});
//-------------Notification-------------//
exportfunction Home_click(event) { 
wixCrm.createContact({
"firstName": $w('#adminEmail').value,
"emails": [$w("#adminEmail").value]  })  
.then((contactId) => {   
wixCrm.emailContact("RwiGjN4", contactId, 
{"variables": 
{"formId": $w('#formIdInput').value,
"Form_Type": $w("#formtype").value,
"Business_Name": $w("#business").value,     }    })    
.then(() => {// do something after the email was sent    })   
.catch((err) => {// handle the error if the email wasn't sent    });  });}
  1. What is your expected outcome ? Sending an email to the site admin? a constant email address?

  2. What is the current outcome ?

  1. The expected outcome is that the triggered email sends to this email by creating a contact as a way to get around the logged in user.
    $w ( ’ #adminEmail ’ ). value = “ehey@metest.com” }

  2. The current outcome: No email is sending. I have a 0 under sent in the tiggers panel.
    Please note, fake email provided in code above. I have a real email in my code.

The following code works for me. Is the email already on your contact list?

export function button1_click(event) {
    wixCrm.createContact( {
 "firstName": $w("#name").value,
 "emails": [$w("#email").value]
    } )
    .then( (contactId) => {
        wixCrm.emailContact("Rm2GxCe", contactId, {
 "variables": {
 "one": "Dark",
 "two": "Stranger Things",
 "three": "The Witcher"
            }
        })
        .then( () => {
            console.log("Triggered email sent");
        })
        .catch( (err) => {
            console.log(err);
        });
    });
}

Sorry, I didn’t see your response. Typically, I get an email, but this time I didn’t.
Yes, the email is already a site contact, but as I understand it the create contact just updates the contact if it exists. But I will let you know how this goes.

This is not working for me.
This is how I wrote it:

//-------------Imports-------------//
import wixData from 'wix-data';
import {session} from 'wix-storage';
import wixCrm from 'wix-crm';

//-------------Form ID Code-------------//
$w.onReady(function () {
$w('#formIdInput').value = session.getItem("formId");
$w('#formtype').value = session.getItem("title");
$w('#business').value = session.getItem("companyName");
$w('#adminEmail').value === "ehey_hey@metest.com"
});

//-------------Notification-------------//
export function Home_click(event) {
    wixCrm.createContact( {
 "emails": [$w("#adminEmail").value],
    } )
    .then( (contactId) => {
        wixCrm.emailContact("RwiGjN4", contactId, {
 "variables": {
 "formId": $w('#formIdInput').value,
 "Form_Type": $w('#formtype').value,
 "Business_Name": $w('#business').value
            }
        })
        .then( () => {
            console.log("Triggered email sent");
        })
        .catch( (err) => {
            console.log(err);
        });
    });
}

@givemeawhisky I am trying this based on your recommendation in this thread:
https://www.wix.com/corvid/forum/community-discussion/send-triggered-email-to-a-user-not-logged-in

$w.onReady(function () {
  $w("#next").onClick( () => {
 let userId = "7f3f0458-6726-4c38-8824-6287fe9c6052"
 let value1 = $w('#formIdInput').value

wixCrm.emailContact("RwiGjN4", userId, {
"variables": {
"formId": value1,
"Form_Type": $w('#conDropdown').value,
"Business_Name": $w('#businessName').value
      }
    } )
    .then( () => {
 // email has been sent
    } )
    .catch( (err) => {
 // there was an error sending the email
  } );
})
 }
    ); 

Still not working.

The answer Shan gave above should work fine for what you are trying to do already if you want it to run after a button click etc.

I have similar setup on a site like this below which uses onAfterSave to create a new contact and email them with a triggered email after they have submitting a user form.

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) => { 
        // Need to use the triggered email name
        return wixCRM.emailContact('joiningusform', contactId, { 
          "variables": { 
            // Need to use the triggered email variable names
            "firstName": firstName,
            //"lastName": $w('#lastName').value,
            // not defined in the triggered email
            //"emails": [$w("#email").value],
            // cannot have array here
             "email": email,
            // correct variable is email not emails
            "choirRole": choirRole,
            "readMusic": readMusic,
            "choirBefore": choirBefore,
            "startNow": startNow,
            "startDate": startDate.toLocaleDateString('en-GB', { weekday: 'short', day: 'numeric', month: 'short', year: 'numeric'})
          } 
        });  
      }) 
      .catch((err) => { 
        // handle the error if the email wasn't sent
        console.log(`Error: ${err}`);
      }); 
  }); 
}); 

Don’t forget that you are using Wix Storage to pass the values from the previous page to the last page.

Remember that you are using code to pass those values across and so the user inputs are most likely not registering the value being entered into the input itself.

Hence the reason why you had to use setFieldValue / setFieldValues previously with the save function to save the user inputs.

You won’t be able to send anything to the user if you set the values through code and you have not saved them beforehand.

setFieldValue( )
Updates the value of a field in the current item.

Description
The setFieldValue function sets the value of a field in the current item. Setting a field value fires an onItemValuesChanged event when the page elements connected to the field have been updated with the new value.

Setting the value of a field in a dataset item does not immediately set that value in the collection that the dataset is connected to. You still need to call the dataset save() function or any other function that performs a save to have the new value reflecting in the collection.

I was trying to avoid having a button on my last page. I tried it with my first page and the userId
Here is what I have for my first page code, so I avoid the session items:

//-------------Imports-------------//
import wixData from 'wix-data';
import wixLocation from 'wix-location';
import {session} from 'wix-storage';
import wixCrm from 'wix-crm'; 'wix-crm-backend';

//-------------Form ID------------//
$w.onReady( function() {
function randomNumber (len) {
var x;
 var n = '';
 for(var count = 0; count < len; count++) {
        randomNumber = Math.floor(Math.random() * 10);
        n += randomNumber.toString();
    }
 return n;
}
$w('#formIdInput').value = randomNumber(10);
});

//-------------Status------------//
$w.onReady( function() {
$w('#status').value = "PENDING";
});

//-------------Form Submission-------------//

export function next_click(event) {
session.setItem("formId", $w('#formIdInput').value);
session.setItem("title", $w('#conDropdown').value);
session.setItem("status", $w('#status').value);
session.setItem("companyName", $w('#businessName').value);
session.setItem("firstName", $w('#firstName').value);
session.setItem("lastName", $w('#lastName').value);
session.setItem("email", $w('#email').value);
session.setItem("phone", $w('#phone').value);

//-------------Dropdown Code-------------//
 if ($w('#conDropdown').value === 'Membership Application') {
      $w('#next').link = wixLocation.to("https://elizabethjhay.wixsite.com/mysite-9/member-applications-business")
      } else if ($w('#conDropdown').value === 'Flower Basket Plaques') { 
      $w('#next').link = wixLocation.to("https://elizabethjhay.wixsite.com/mysite-9/flower-basket-plaques")
      } else if ($w('#conDropdown').value === 'Mailing a Payment') { 
      $w('#next').link = wixLocation.to("https://elizabethjhay.wixsite.com/mysite-9/payment-by-mail")
      } else if ($w('#conDropdown').value === 'Membership Deal') { 
      $w('#next').link = wixLocation.to("https://elizabethjhay.wixsite.com/mysite-9/membership-deals")
      } else if ($w('#conDropdown').value === 'Sleepy Hollow Street Fair') { 
      $w('#next').link = wixLocation.to("https://elizabethjhay.wixsite.com/mysite-9/streetfair-application")
      } else if ($w('#conDropdown').value === 'Tarrytown Street Fair') { 
      $w('#next').link = wixLocation.to("https://elizabethjhay.wixsite.com/mysite-9/streetfair-application")
 
    } 
}
 
//-------------Create Contact-------------//
$w.onReady(function () {
  $w("#next").onClick( () => {
    wixCrm.createContact( {
 "firstName": $w("#firstName").value,
 "lastName": $w("#lastName").value,
 "emails": [$w("#email").value],
 "phones": [$w("#phone").value],
 "Business Name": $w('#businessName').value
   }
    );

  });
});

//-------------Send Email-------------//
$w.onReady(function () {
  $w("#next").onClick( () => {
 let userId = "7f3f0458-6726-4c38-8824-6287fe9c6052"
 let value1 = $w('#formIdInput').value

wixCrm.emailContact("RwiGjN4", userId, {
"variables": {
"formId": value1,
"Form_Type": $w('#conDropdown').value,
"Business_Name": $w('#businessName').value
      }
    } )
    .then( () => {
 // email has been sent
    } )
    .catch( (err) => {
 // there was an error sending the email
  } );
  })
  })

Wix CRM uses emails by the way. I got most of this coding form the API
https://www.wix.com/corvid/reference/wix-crm.html
https://www.wix.com/corvid/reference/wix-crm-backend.html

I also know that I must use the email variables and not the form variables.

I would prefer to have the tigger on my first page without a button click, but if I must have it on my last page I tried this. I am setting the session items by the way. I am aware of the flow. Both are not working.

//-------------Imports-------------//
import wixData from 'wix-data';
import {session} from 'wix-storage';
import wixCrm from 'wix-crm';

//-------------Form ID Code-------------//
$w.onReady(function () {
$w('#formIdInput').value = session.getItem("formId");
$w('#formtype').value = session.getItem("title");
$w('#business').value = session.getItem("companyName");
$w('#adminEmail').value = "ehey_hey@me.com"
});

//-------------Notification-------------//
export function Home_click(event) {
    wixCrm.createContact( {
 "email": [$w("#adminEmail").value],
    } )
    .then( (contactId) => {
        wixCrm.emailContact("RwiGjN4", contactId, {
 "variables": {
 "formId": $w('#formIdInput').value,
 "Form_Type": $w('#formtype').value,
 "Business_Name": $w('#business').value
            }
        })
        .then( () => {
            console.log("Triggered email sent");
        })
        .catch( (err) => {
            console.log(err);
        });
    });
}

Note the use of the email variables

@elizabethjhay try checking you console log for errors and replace the 2nd last line with another catch.

})
. catch ( ( err ) => {
console . log ( err );
});
} //end of function

Also, in this particular code you have a typo for wixCrm . createContact

It should be “emails” and not “email”

@shantanukumar847 Are you referring the one with the onlick? Because I would prefer to get a solution for:

//-------------Send Email-------------//
$w.onReady(function () {
  $w("#next").onClick( () => {
 let userId = "7f3f0458-6726-4c38-8824-6287fe9c6052"
 let value1 = $w('#formIdInput').value

wixCrm.emailContact("RwiGjN4", userId, {
"variables": {
"formId": value1,
"Form_Type": $w('#conDropdown').value,
"Business_Name": $w('#businessName').value
      }
    } )
    .then( () => {
 // email has been sent
    } )
    .catch( (err) => {
 // there was an error sending the email
  } );
  })
  })

@shan or @givemeawhisky Any thoughts on why this is still not triggering? I would prefer it not to have to be an onclick. Thanks

Anyone that can help me with why the tiggers are not working will be a great help!