SOLVED - Help sending an email after submitting a form using tables directly, not with a defined dataset.

SEE COMMENTS FOR SOLUTION

I have code where I insert into table 1, then update table 2 on submit. I am doing this directly in the code, not tied to a dataset (see below).

Does anyone know how I can use something similar to the .onAfterSave(sendFormData) directly from a table and not a dataset to send an email (using sendGrid)?

Thank you in advance!

export function submit_click(submitEvent) { 

 let quantity = $w("#nbrVolQuantity").value
 
 if (!$w('#txtFirstName').valid ||
        !$w('#txtLastName').valid ||
        !$w('#txtEmail').valid ||
        !$w('#nbrVolQuantity').valid ||
        !$w('#txtPhone').valid) {
         $w('#txtError').text = "One or more fields are not valid";
         $w('#txtError').show();
 return;
} //close function submit_click   

//Make sure not entering a larger quantity than is available
if (parseFloat(quantity) > eventDetail.available){
         $w('#txtError').text = "Please correct the Quantity below";
         $w('#txtError').show();
 return;
}

 var data = {
 "first_name": $w('#txtFirstName').value,
 'last_name': $w('#txtLastName').value,
 'title': $w('#txtEmail').value,
 'phone': $w('#txtPhone').value,
 'quantity':$w('#nbrVolQuantity').value,
 'comments':$w('#txtComments').value,
 'ped_title':eventDetailId, // This is "title" field which is referenced in VOLUNTEERS collection and the row I want to later update
  }; //close var data

  wixData.insert('PFA_VOLUNTEERS', data).
    then((results) => {
            results.fulfilled = results.fulfilled + parseInt($w('#nbrVolQuantity').value,10)
            console.log("insert results ",results);

 if (!eventDetail.fulfilled) { eventDetail.fulfilled = parseFloat(quantity)} else { /// in case you do not already have 0
               eventDetail.fulfilled = parseFloat(eventDetail.fulfilled) + parseFloat(quantity) // in case it is a string not a number     
            }
            wixData.update('PFA_EVENT_DETAIL', eventDetail).
                 then(update => console.log("Updated!  : " , eventDetail))
                  .catch( err => console.log("Error while updating : " , err)) 
                  $w('#dsEventDetail').refresh();
                  console.log("update results ",results);
                  });

        $w('#txtError').hide();
        $w('#txtSuccess').show();
        $w("#txtFirstName").value = null; 
        $w("#txtLastName").value = null;
        $w('#txtEmail').value = null;
        $w('#txtPhone').value = null;
        $w('#nbrVolQuantity').value = null;
        $w('#txtComments').value = null;
        $w("#txtSelectedPosition").value = null;
}

1 Like
I am not certain it's "best practices" but it's functioning. I will ask a peer to review.

 ...
wixData.insert('PFA_VOLUNTEERS', data).
    then((results) => {
            results.fulfilled = results.fulfilled + parseInt($w('#nbrVolQuantity').value,10)
            console.log("insert results ",results);

  if (!eventDetail.fulfilled) { eventDetail.fulfilled = parseFloat(quantity)} else { /// in case you do not already have 0
               eventDetail.fulfilled = parseFloat(eventDetail.fulfilled) + parseFloat(quantity) // in case it is a string not a number     
            }

            wixData.update('PFA_EVENT_DETAIL', eventDetail).
                 then(update => console.log("Updated!  : " , eventDetail))
                  .catch( err => console.log("Error while updating : " , err)) 
                  $w('#dsEventDetail').refresh();
                  console.log("update results ",results);

           //call the function
            sendFormData();

                      }
        );                   

 function sendFormData() {
 const subject = 'Thank you for registering to volunteer';
 const body = 'Hello  ' + firstName + 
 ',  \r Put some text here ' + eventDetailTitle + 
 '. \r More text.';
 const recipient = email; //email is stored in the title column of volunteers
          console.log("in sendFormData");
 
            sendEmailWithRecipient(subject, body, recipient)
              .then(response => console.log(response)
            ); 
          } //close function sendFormData
...