This example shows how to email selected records as separate PDF files.
Client before:
First, we need to get information about the user-selected records, since the PDF files are created from these records. If no entries are selected, the code execution ends.
Then, we make a dialog appear on the web page. Using this dialog, the user can specify an email recipient's address, email subject and body.
We also set the parameters of the the PDF pageType to ‘View’.
var pdfParams = {},
selectedRecords = pageObj.getSelectedRecords();
if( selectedRecords.length == 0 )
return false;
params.recordCount = selectedRecords.length;
params.filenames = [];
var createOnePdf = function( idx ) {
ajax.addPDF( 'pdf'+idx, pdfParams[idx], function() {
delete pdfParams[idx];
if( Object.keys( pdfParams ).length == 0 ) {
showDialog();
}
});
}
var showDialog = function() {
ctrl.dialog( {
title: 'Email ' + params.recordCount + ' PDF files',
fields: [
{
name: 'email',
value: '[email protected]'
},
{
name: 'subject',
value: 'Check out this data'
},
{
name: 'body',
value: 'This email is generated by Runner-created application',
type: 'textarea'
},
]
});
}
selectedRecords.forEach( function( ajaxRow, idx ) {
pdfParams[ idx ] = {
pageType: 'view',
keys: ajaxRow.getKeys()
};
params.filenames.push( '_viewpage_id'+ajaxRow.getKeys()+'.pdf')
createOnePdf( idx );
});
return false;
Server:
In the Server event, we create temporary PDF files for each record. After that, we send the email. The result is returned to the Client after event:
•result "success" = true - everything is fine, the message is sent.
•result "success" = false - the message is not sent. In this case, an error message defined in the result variable appears.
// save generated files to disk and create an array of attachments
dynamic attachment = XVar.Array(), a_button = null, mail = XVar.Array(), path = null, a_result = XVar.Array(), ret = XVar.Array(), idx, attachments = XVar.Array();
mail = XVar.Clone(XVar.Array());
mail.InitAndSetArrayItem("[email protected]", "to");
mail.InitAndSetArrayItem("Runner PDF and Email demo", "subject");
mail.InitAndSetArrayItem("Check the results", "body");
foreach(var item in parameters["filenames"].GetEnumerator()){
idx = item.Key;
path = XVar.Clone(button.saveTempFile((XVar)(parameters[MVCFunctions.Concat("pdf",idx)])));
attachment = XVar.Array();
attachment.InitAndSetArrayItem(path,"path");
attachment.InitAndSetArrayItem(parameters["filenames"][idx],"name");
attachments.InitAndSetArrayItem(attachment, idx);
}
mail.InitAndSetArrayItem(attachments, "attachments");
ret = XVar.Clone(MVCFunctions.runner_mail((XVar)(mail)));
if(XVar.Pack(ret["mailed"]))
{
result.InitAndSetArrayItem(true, "success");
}
else
{
result.InitAndSetArrayItem(ret["message"], "message");
result.InitAndSetArrayItem(false, "success");
}
return null;
Client after:
The user gets a notification with the result.
if( result.success ) {
ctrl.setMessage("sent ok");
} else {
ctrl.setMessage("error sending " + result.message );
}
See also:
•AJAX helper object: ajax.addPDF