In this I will share the steps of how we can send the report in email.
First of all for sending the email we all need to setup the email templates based on our requirements.
Below is the path for the email templates:
Basic -> Setup -> Email Templates
Each template is uniquely identified by the Email ID.
we can also create the setup form to allow the user to select the template based on his requirement in his feature.
For example for our daily tax invoice feature we have created a setup form for the user to select the email template.
Now after creating and setting up the template below is the code to create the report and attach and send it in an email.
public boolean send(PrintCopyOriginal _printCopyOriginal = PrintCopyOriginal::Copy)
{
SysEmailId emailId = crtParameters::find().DailyTaxInvoiceEmailId;
//above code is use to get the email template from the setup table that is configured by the end user //before
str tempFile;
;
if (this.DailyTaxInvoiceEnabled())
{
tempFile = this.generatePDF(custInvoiceJour, _printCopyOriginal);
above method generates the report in pdf in temporary location and send the address of that file to be attached in the email
SysEmailTable::sendMail(emailId,
'en-nz',
custTable.CRT_DailyTaxInvoiceEmail,
this.initMap(),
tempFile,
'',
true,
'admin',
true,
custInvoiceJour.RecId);
//above method is use to send the email to the
//First parameter is the email template id (The template which we will use to send //the email)
//second parameter is the language.
//third parameter is the email address to whom we are sending the email
//in fourth parameter we are just initializing the map based on the fields required in //the email template for the subject and other things (will show this method below)
//fifth parameter address of the report where report is saved after generation
//eighth parameter is the user id from which we are sending the email (credentials are setup on //the smtp server)
new FileIOPermission(tempFile, 'rw').assert();
WinAPIServer::deleteFile(tempFile);
CodeAccessPermission::revertAssert();
//above code is just deleting the report from the temporary location
return true;
}
return false;
}
Below is the method for generating the pdf:
private str generatePDF(CustInvoiceJour _custInvoiceJour, PrintCopyOriginal _printCopyOriginal = PrintCopyOriginal::Copy)
{
Args args;
ReportRun rr;
Report rb;
PrintJobSettings pjs;
str tempFile;
str tempFolder;
;
tempFolder = Global::strReplace(SysEmailParameters::find().AttachmentsPath,'//','/');
//Above attachment path is define in the following navigation path of the setup of form //Administration -> setup -> E-mail Parameters
tempFile = strfmt('%1\\TaxInvoice%2.pdf',tempFolder,custInvoiceJour.InvoiceId);
args = new Args(reportstr(CRTFreeTextInvoice));
args.record(_custInvoiceJour);
args.parmEnum(_printCopyOriginal);
// Set report run properties
rr = new ReportRun(args,'');
rr.suppressReportIsEmptyMessage(true);
rr.query().interactive(false);
// set report properties
rb = rr.report();
rb.interactive(false);
// set print job settings
pjs = rr.printJobSettings();
pjs.fileName(tempFile);
pjs.fitToPage(true);
// break the report info pages using the height of the current printer's paper
pjs.virtualPageHeight(-1);
// force PDF printing
pjs.format(PrintFormat::PDF);
pjs.setTarget(PrintMedium::File);
pjs.viewerType(ReportOutputUserType::PDF);
// lock the print job settings so can't be changed
// X++ code int the report may try to change the destination
// to the screen for example but this does not make
// sense when running a report here
pjs.lockDestinationProperties(true);
// Initialize the report
rr.init();
rr.run();
return tempFile;
}
Below is the method for initiating the map for the email to insert the subject and other things that I have discussed above.
private Map initMap()
{
Map map = new Map(Types::String, Types::String);
;
map.insert('subject', strfmt('Daily tax invoice %1- %2', custInvoiceJour.InvoiceId,custInvoiceJour.DocumentDate));
map.insert('phone', companyInfo::find().CRTFreePhone);
map.insert('Customeraccount', strfmt('%1', custInvoiceJour.InvoiceAccount));
map.insert('CustomerName', strfmt('%1', CustTable::find(custInvoiceJour.InvoiceAccount).Name));
map.insert('InvoiceNumber', strfmt('%1', custInvoiceJour.InvoiceId));
return map;
}
This was all related to the generating the report and sending it in the email.
If you have any questions than feel free to post here.
Thanks
Muhammad Zahid.