Trigger email from dashboard to member

Hello! I have a real estate website and I created a dashboard from where I verify member houses before they become public. I want to send an email from there to the publication’s owner/member when I click the button for ‘‘approve publication’’ so they know their house form has been accepted and published. How can I modify this triggered-email-code so it sends to the email of the person who submitted the form I verify, and not the logged in user since I do this from my dashboard? Thank you for your help.

import wixUsers from ‘wix-users’ ;

$w.onReady( function () {
$w( “#dataset1” ).onAfterSave( () => {
if (wixUsers.currentUser.loggedIn) {
const userId = wixUsers.currentUser.id;

  wixUsers.emailUser( 'S1dpbcZ' , userId, { 
      variables: { 
        nombredelsuscriptor: $w( '#input1' ).value, 
        nombreinmueble: $w( '#input2' ).value 
      } 
    } ) 
    .then( () => { 

// do something after the email was sent successfully
} )
. catch ( (err) => {
// handle error that prevented the email from being sent
} );
}
} );
} );

If you want to send an email to a specific member, then use Wix Users backend API, otherwise you can only send an email to the user who is currently logged in.

https://www.wix.com/corvid/reference/wix-users.html#emailUser
Sends a Triggered Email to the currently logged-in site member.

https://www.wix.com/corvid/reference/wix-users-backend.html#emailUser
Sends a Triggered Email to the specified site member.

Thank you @GOS, does the second option allows me to get automatically the member email of the house I am verifying eventhough I am doing it from my admin manager dashboard? how would the code be if mixed with the above triggered email code?

The dashboard pages let you do the same as normal pages.
https://support.wix.com/en/article/corvid-working-with-dashboard-pages#:~:text=To%20make%20your%20page%20available,to%20elements%20on%20your%20page.

How are you receiving the members info for the house listing?

If that is through a user input form to yourselves, then you can make sure that the form saved into the dataset gets saved with the users Id and then you can just take the user Id from the dataset field and use it for the user Id for the triggered email.

Or use the members email from the form and make a query to your users data, whether that be the Wix Members app Members/PrivateMembersData, or your own collection, for a matching email and get the user id form the results and use it send the email.
https://support.wix.com/en/article/corvid-wix-members-privatemembersdata-collection-fields#emails-emails-1

Thank you for your reply GOS! Yes I gather my info through an input form. My doubt regarding the dashboard was because the triggered email code I have is coded for logged in users, so if i trigger it from my dashboard nothing will happen and so thats why I need the same code but modified to get the email of the user who filled out the form. I understand and agree with everything you have explained. My issue is I dont know how to do the code since I am a beginner. I want to take the user Id of the current form I am verifying from the dataset field and use it for the user Id for the triggered email , or take the email from the current input form I am verifying directly as you said. So far I have this>>> Which modifications should I do to make it work according to that need?

import wixUsers from ‘wix-users’ ;

$w.onReady( function () {
$w( “#dataset1” ).onAfterSave( () => {
if (wixUsers.currentUser.loggedIn) {
const userId = wixUsers.currentUser.id;

  wixUsers.emailUser( 'S1dpbcZ' , userId, { 
      variables: { 
        nombredelsuscriptor: $w( '#input1' ).value, 
        nombreinmueble: $w( '#input2' ).value 
      } 
    } ) 
    .then( () => { 

// do something after the email was sent successfully
} )
. catch ( (err) => {
// handle error that prevented the email from being sent
} );
}
} );
} );

Anyone available?

Don’t use the Wix Users API and the emailUser() function as that will only apply to the currently logged in user, hence why the code uses onAfterSave so that it sends the triggered email to the currently logged in user who submitted the form.

You need to be using the Wix Users Backend API and the emailUser() function from that instead.
https://www.wix.com/corvid/reference/wix-users-backend.html#emailUser

Examples
Send a Triggered Email to a site member

import wixUsersBackend from 'wix-users-backend';

export function myBackendFunction() {
  let userId = // get user ID

  wixUsersBackend.emailUser("emailID", userId)
    .then( () => {
      // email has been sent
    } )
    .catch( (err) => {
      // there was an error sending the email
  } );
}

Send a Triggered Email to a site member with variable values

import wixUsersBackend from 'wix-users-backend';

export function myBackendFunction() {
  let userId = // get user ID
  let value1 = // value for variable1

  wixUsersBackend.emailUser("emailID", userId, {
      "variables": {
        "variable1": value1,
        "variable2": "value for variable2"
      }
    } )
    .then( () => {
      // email has been sent
    } )
    .catch( (err) => {
      // there was an error sending the email
    } );
}

Also, don’t forget that each dataset will have the id field hidden by default, so if you use the Members app collection or your own members dataset, then you can simply just get the id from there and copy and paste it into a user input on your page, which your code can use to get the userId from.

:disappointed: I know that code but I dont know how to use it. Which part goes backend and which one goes front end? How do I get the user id of the form I am currently correcting from the dashboard?

import wixUsersBackend from ‘wix-users-backend’ ;

export function myBackendFunction() {
let userId = what should I write here to get the ID. Copy paste the especific user id doesent work for me. I need it to be found automaticaly depending on the form I am verifying

wixUsersBackend.emailUser( “S1dpbcZ” , userId, {
“variables” : {
“nombredelsuscriptor” : $w( ‘#input1’ ).value,
“nombreinmueble” : $w( ‘#input2’ ).value
}
} )
.then( () => {
// email has been sent
} )
. catch ( (err) => {
// there was an error sending the email
} );
}

Okay I see now, I will get back to asap with that. :+1:

Save the userId in page code while saving the publication into dataset

Page Code

This code should be on the user page while creating house publication for approval

let CurrentUserId = wixUsers.currentUser.id
$w('#dataset1').setFieldValue("userId", CurrentUserId)
$w('#dataset1').save()

BackendCode

Create a Backend web module file SendEmail.jsw

import wixUsersBackend from'wix-users-backend';

exportfunction myBackendFunction(userId, Value1, Value2) {
wixUsersBackend.emailUser("S1dpbcZ", userId, {
"variables": {
"nombredelsuscriptor": Value1,
"nombreinmueble": Value2}    
} )    
.then( () => {// email has been sent    } )
.catch( (err) => {// there was an error sending the email    } );}

Dashboard Page Code

On Dashboard retrieve it from database
I think you use the repeater to display the Publication items for approval. So I am using $item.

import { myBackendFunction } from 'backend/SendEmail';

$item("#Button").onClick( (event) => {
let $item = $w.at(event.context);
let UserID = $item('#dataset2').getCurrentItem().userId
let Value1 = $item("#input1").value;
let Value2 = $item("#input2").value;
return myBackendFunction(userId, Value1, Value2)
.then( () => { $item("#text1").text = "Email has been sent successfully" })
}

Thank you @Vijay. I have done everything so far, however I am not using a repeater. I see all properties to be approved in a table, then I select the one I want to verify and it opens all the input fields with the info of that propertie. When I am done modifying it I just click a button that says ‘approve’ and done. So the dashboard code is not working for me, how should it be instead? I guess just an onclick event for that button? and replace the $item with $w???

Okay, I am back quickly again. I see that you are using a table here, so that is even better.

As your input is coming from members only, any data that they submit through your form will be linked to them with their userId in a hidden field in the dataset that you are using to save the inputs into.

Therefore, you can simply setup your results table to show this hidden id field and then use onRowSelect to get the rows data and use that to populate your own user inputs on the page.

So you can have a user input on your page that is populated with the userId of that member and you can use that for the userId for the emailUser function itself for when you click on the submit button.
https://www.wix.com/corvid/reference/$w.Table.html#onRowSelect

These two old posts should get you moving on a little bit.
https://www.wix.com/corvid/forum/community-discussion/open-local-page-on-table-row-select
https://www.wix.com/corvid/forum/community-discussion/link-search-results-in-table-to-product-page-based-on-row-solved

I will see if i can make time to get back on here very soon to see if you are still struggling or if you have sorted it.

Okay, to add further here, if you go back to your original form, then you can simply get a users Id and add it in a read only box with this simple code.

You can use this to simply get the users Id in the first place if you find that you can’t manage to use the hidden by default id field already and then again use setFieldValue() function to save it to the correct field in your form submission dataset.

Then you can use that in your table and get the userId for the emailUser function from that.

import wixUsers from 'wix-users';

$w.onReady(function () {
let user = wixUsers.currentUser.id; 
$w('#input1').value = user; 
}); 

@GOS Awesome, thank you for your continuous guidance and patience, I will work on this tomorrow and let you know how it goes! I am learning and adapting to this code universe so it might take me a bit to put it together. I’ll be back to you asap​:dizzy::dizzy::dizzy:

@GOS so this is my update. I have done everything you described so far. I have a table, when I click a row a user input form is opened all populated by the info of the selected table row. I added a text field and connected it to the ID field from the dataset, however this shows the property ID, not the _owner ID. Is that right?

I added this code in my dashboard page (which is where I am completing the approval process):

import wixUsersBackend from ‘wix-users-backend’ ;

export function myBackendFunction() {
let userId = $w( ‘#text289’ ).text,
let value1 = $w( ‘#input1’ ).value, (here I get Error: keyword ’ let’ is reserved)
let value2 = $w( ‘#input2’ ).value

wixUsersBackend.emailUser( “S1dpbcZ” , userId, {
“variables” : {
“variable1” : value1,
“variable2” : valie2,
}
} )
.then( () => {
// email has been sent
} )
. catch ( (err) => {
// there was an error sending the email
} );
}

Then in my backend code I added a module called SendEmail.jsw with the code:

import wixUsersBackend from ‘wix-users-backend’ ;

export function myBackendFunction(userId, Value1, Value2) {
wixUsersBackend.emailUser( “S1dpbcZ” , userId, {
“variables” : {
“nombredelsuscriptor” : Value1,
“nombreinmueble” : Value2 }
} )
.then( () => { ‘email sent’ } )
. catch ( (err) => { ‘there was an error sending the email’ } );}

Is this correct? Because of the ‘let’ error I mentioned above, the page is not working properly so I havent been able to check if the email code is working.

@GOS have you been able to check this?

Thank you Raj! I didnt know you could do that, I will keep it in mind! However what I am trying to do is send the email automatically when I verify their properties with the verify button from a dashboard I designed for this purpose. This way I do all at once :smiling_face: