Hello,
I have requirement to send an Email to the customer with attachment of a PDF file of Customer Aging report. For that I have done below customization and it is quite simple process that we can apply to any other report in Ax2012.
1. Create one button in SRSReportViewer form.
2. Enable this button for only required report, in my case its CustAging report So Im enabling it for CustAging report by adding a piece of code in Init method.
ReportName = this.controller().parmReportName();
if (ReportName == 'CustAgingReport.DesignWithNoDetailAndNoTransactionCur')
{
MailReport.visible(true);
}
3. Now add the Code under the Mail button clicked method to create and send report PDF to the customer.
Clicked:
void clicked()
{
Microsoft.Dynamics.AX.Frameworks.Controls.ReportViewer.AxReportViewer
axviewer;
Microsoft.Reporting.WinForms.ReportViewer nviewer;
Microsoft.Reporting.WinForms.LocalReport report;
//ERPReportMailParameter mailParameter;
List salesReplist;
ListIterator listiterator;
str Salesreps;
str reportName;
str reportPath;
int dot;
int slash;
str documentName = 'CustAgingV1';
SRSReportName srsReportName;
SRSReportDesignName srsReportDesignName;
System.Exception ex;
System.Array bytear;
System.Object no;
System.IO.File file;
str tmpPath;
System.IO.FileStream fs;
SysEmailParameters emailParameters = SysEmailParameters::find();
;
if(!emailParameters.AttachmentsPath)
warning('Attachments path not defined on Email parameters');
super();
axviewer = AxReportViewer.control();
nviewer = axviewer.get_ReportViewerControl();
try
{
//render as PDF
report = nviewer.get_ServerReport();
bytear = report.Render("PDF");
no = bytear;
salesReplist = this.GetSalesRep();
listiterator = new listiterator(salesReplist);
while(listiterator.more())
{
Salesreps += listiterator.value();
listiterator.next();
if(listiterator.more())
{
Salesreps += ',' ;
}
}
documentName = strfmt(" CustAgeing for %1 on %2 %3",Salesreps,
date2str(systemDateGet(),123,2,DateSeparator::None,2,DateSeparator::None,2),
time2str(timeNow(),TimeSeparator::Dot,TimeSeparator::Dot));
//path to temp. files
tmpPath = emailParameters.AttachmentsPath + '\\' + documentName + '.pdf'/*System.IO.Path::GetTempPath()*/;
//save to file
fs = System.IO.File::Create(tmpPath);
fs.Write(no,0,bytear.get_Length());
fs.Flush();
fs.Close();
if(tmpPath)
{
this.sendReportAsEmail(tmpPath, Salesreps);
}
}
catch(Exception::CLRError)
{
ex = CLRInterop::getLastException();
info(ex.ToString());
}
}
To Send mail I have created one more method in that :
private void sendReportAsEmail(FilenameOpen _fileName,
str _SalesRep )
{
str SalesRep;
ListIterator listIteratorLoc;
SysEmailTable sysEmailTable;
str emailAddress;
Map mappings = new Map(Types::String, Types::String);
Email custAgingEmail = CustParameters::find().E2C_CustAgingEmailId;
;
// gets the salesrep mail Ids
listIteratorLoc = new ListIterator(this.GetSalesRep());
while(listIteratorLoc.more())
{
SalesRep = listIteratorLoc.value();
if(SalesRep)
{
emailAddress += HcmWorker::findByPersonnelNumber(SalesRep).email();
}
listIteratorLoc.next();
if(listIteratorLoc.more())
{
emailAddress += ';' ;
}
}
if (custAgingEmail)
{
emailAddress += strFmt('; %1',custAgingEmail);
}
// Build your variable/text mappings
mappings.insert('subject', strFmt('CustomerAging report of Sales rep %1 ', _SalesRep));
mappings.insert('Salesrep', _SalesRep);
sysEmailTable = SysEmailTable::find('CustAgeing');
if(sysEmailTable.RecId)
{
SysEmailTable::sendMail(sysEmailTable.EmailId,
CompanyInfo::find().LanguageId ? CompanyInfo::find().LanguageId : 'en-nz', // Chosen language
emailAddress, // Who you're sending the email to
mappings, // Your variable mappings
_fileName, // Location of file attachment (server/client matters) or none
'' , // XML if you're using XSLT
true, // Traceable or not?
curUserId(),//'admin', // Sending user
true); // Use retries?
info(strFmt("Email has been sent to %1", emailAddress));
}
else
{
warning("Email template not found");
}
}
This has worked in Ax 2012 version. If it is D365 we need to change Send Email process.
Note: For sending attachments from Ax 2012 we must need to use the files folder location which is specified under the EMail Parameters
Thanks,
Happy Daxing !!
No comments:
Post a Comment