Please enable JavaScript to view this site.

Navigation: Advanced topics > Events > Sample events > Email

Email selected records

Scroll Prev Next More

How to send an email to a predefined address

To send an email with several selected records on the List page, you need to create a custom button.

 

1. Proceed to the Page Designer screen.

 

2. Create an Update selected custom button and add the following code snippets into it:

Server tab:

 

 

StringBuilder body = new StringBuilder();
XVar rmail = XVar.Array();
 
XVar data;
while(data = button.getNextSelectedRecord())
{
  body.AppendLine(String.Format(
  "OrderID: {0} \n" +
  "Customer: {1} \n" +
  "Employee: {2} \n" +
  "------------------",
  data["OrderID"], data["CustomerID"], data["EmployeeID"]));
}  
 
rmail["to"] = "[email protected]";
rmail["subject"] = "Sample subject";
rmail["body"] = body.ToString();
 
var arr = MVCFunctions.runner_mail(rmail);  
 
result["txt"] = "Emails were sent.";
 
if(!arr["mailed"])
{
  result["txt"] = "Error happened: " + arr["message"].ToString();
}

Client After tab:

var message = result["txt"];
ctrl.setMessage(message);

 

Note: The Client Before tab should be blank (delete sample code there if any).

 

Sample email message:

 

OrderID: 10249

Customer: TRADH

Employee: 6

-------------------

OrderID: 10251

Customer: VICTE

Employee: 3

-------------------

OrderID: 10253

Customer: HANAR

Employee: 3

-------------------

How to send an email to the currently authorized user

Instead of a hardcoded email address, you can send email to the current user.

1. The email address is used as the username

var email = XSession.Session["UserID"];

2. The email address is stored in an individual field

In this case, you need to save the email address to the session variable in the AfterSuccessfulLogin event:

 

XSession.Session["email"] = data["email"];

 

BeforeProcess event code:

 

var email = XSession.Session["email"];

See also:

Inserting custom button

Update selected

Tri-part events

Send an email to all users

Send an email to selected users

Send simple email

How to email selected records as separate PDF files

Grid Row Javascript API: row.getKeys()

JavaScript API:getSelectedRecordKeys()

Button object: getNextSelectedRecord()

runner_mail function

AJAX helper object: setMessage()