AX 4.0 - email purch order as PDF attachment on order post

Hi.

I’m trying to solve how best to do this thing. On posting a purch order, an email should be sent as the purch orders as an PDF attachment using X++. There should be no interaction on the emailing process.

SysInetMail class should be sufficient, but the attachment itself is troubling me.

Is it possible to to create the report and attach it without first saving it to disk? If so, how?

I have done this in the past using the Sysmailer class.

I dont think you can add files as attatchments without first generating and saving them to the disk, but you can use the WinAPI::GetTempPath()(Or use som other more specific path) method when saving and then WinApi::DeleteFile() delete the files after they have been sent.

Sysmailer class: http://msdn.microsoft.com/en-us/library/aa851352(v=ax.10).aspx

WinAPI::GetTempPath(): http://msdn.microsoft.com/en-us/library/aa849545(v=ax.50).aspx

WinApi::DeleteFile(): http://msdn.microsoft.com/en-US/library/aa876456(v=ax.50).aspx

To create PDF-file:

void printReport()

{

;

Args = new Args(ReportStr(PurchPurchaseOrder));

Args.record(purchTable);

reportRun = new ReportRun(Args);

reportRun.report().interactive(false);

reportRun.printJobSettings(printJobSettings.packPrintJobSettings());

reportRun.init();

reportRun.run();

}

But befor the above method is called you need to update the printJobSettings:

void setPrintJobSettings()

{

FileName fileNameExport;

FileName fileNameDestination;

;

printJobSettings = new PrintJobSettings();

printJobSettings.setTarget(PrintMedium::File);

printJobSettings.fileName(“FilePath\Filname.pdf”);

printJobSettings.format(PrintFormat::PDF);

printJobSettings.suppressScalingMessage(true);

printJobSettings.warnIfFileExists(false);

}

I don’t believe so. “Printing” the report as a PDF is what generates the PDF and it needs to be saved somewhere, then attached. You should use temporary directories and/or delete the file afterwords. The SysInetMail class is designed to attach files from locations too…

I found this post http://dynamicsuser.net/forums/p/16922/79208.aspx#79208

But if I do this:

args = new Args(ReportStr(PurchPurchaseOrder));
args.record(vendpurchorderjour);
rr = new ReportRun(args);
rr.printJobSettings().setTarget(PrintMedium::Mail);
rr.printJobSettings().Format(PrintFormat::PDF);
rr.printJobSettings().mailTo(“foo@bar.com”);
rr.printJobSettings().mailSubject(“AX report”);
rr.run();

Only thing that happens is that the report open on screen as per usual. Most annoying. Guess it’s temp folders then.

Well, I guess the following would work if I had Outlook open on the server? Anyone ever combine this with SysInetMail? :slight_smile:

args = new Args(ReportStr(WMDFIPurchPurchaseOrder));

args.record(vendpurchorderjour);
rr = new ReportRun(args);
rr.init();
printJobSettings = rr.printJobSettings();
printJobSettings.setTarget(PrintMedium::Mail);
printJobSettings.preferredTarget(PrintMedium::Mail);
printJobSettings.format(printFormat);
printJobSettings.preferredFileFormat(printFormat);
printJobSettings.fileName(fileName);
printJobSettings.mailTo(“foo@bar.com”);
printJobSettings.mailSubject(“AX raportti”);

// — Lock the print job settings so can’t be changed in X++ to the screen
printJobSettings.lockDestinationProperties(true);
rr.printJobSettings(printJobSettings.packPrintJobSettings());
rr.run();

Well, temp folders it must be. Dang.

Look here for how to use temp folders. You just save to the default temp directory, send the email, then delete the temporary file. I know the feeling you have where you want to just go direct to something but you’re going to have to use temp folders.

http://alexondax.blogspot.com/2012/01/how-to-send-emails-from-ax-without.html

Yeah, that’s easy-peasy, done that a hundred times. Problem is, customers tend to change folders and not the parameters and then complain [:(]

Well, we work with the tools we got etc. etc.

Customers can be a pain, but I wouldn’t use parameters. Use winApi::getTempPath() or winApiServer::getTempPath() to get the default temp directory, save the file, then immediately delete it. I don’t see how it could go wrong.

Yes indeed. If you actually had a say in the definition [:)]