Search This Blog

Tuesday, June 11, 2013

How to send Normal AX Reports to User through Batch Process in Dynamics Ax 2009.


Hi friends,
Recently I got one requirements from my User, I.e. customer want to see ax reports in E-mail everyday once through batch process.
its just like send mail from batch process only, But we need to work on how to send normal report.

1) write the class with below methods mandatory.
         


2)Here am going to post logic for how to convert ax report to PDF and how to send the email to user only.

// Saved the reports into particular location
void SaveReport()
{
    Args args;
    ReportRun rr;
    str reportName = "OpenPurchOrderLine_Vendor";
    str myPath;
    int i;
   ;
   i = 1;
  args = new Args(reportName);
  args.caller(rr);
  rr = new reportRun(args);
    rr.query().interactive(false);
    rr.report().interactive(false);
    rr.setTarget(printMedium::File);
    rr.printJobSettings().setTarget(PrintMedium::File);
    rr.printJobSettings().preferredTarget(PrintMedium::File);
    rr.printJobSettings().format(PrintFormat::PDF);
    rr.printJobSettings().warnIfFileExists(false);
    rr.printJobSettings().suppressScalingMessage(true);
    pdfFileName = @\\AXTESTDEV1\D$\Test\test.pdf; //@ is used for server\\ServerName\drive name$\Folder
    rr.printJobSettings().fileName(pdfFileName);
    rr.init();
    rr.run();
    info("After Run in SaveReport()");
}

//Used to sending emails to particular users

void EmailCheck()
{
         //Set                     permissionSet2 = new Set(Types::Class);
         InteropPermission permission = new InteropPermission(InteropKind::ComInterop);
         ;
        CodeAccessPermission::revertAssert();
        info("After code access in EmailCheck()");
         permission.assert();
         mailer = new SysMailer();
         parameters = SysEmailParameters::find();
        if (parameters.SMTPRelayServerName)
       {
            mailer.SMTPRelayServer(parameters.SMTPRelayServerName,
                         parameters.SMTPPortNumber,
                         parameters.SMTPUserName,
                         SysEmailParameters::password(),
                         parameters.NTLM);
             info("if");
       }
      else
      {
        mailer.SMTPRelayServer(parameters.SMTPServerIPAddress,
                             parameters.SMTPPortNumber,
                             parameters.SMTPUserName,
                             SysEmailParameters::password(),
                             parameters.NTLM);
                  info("else");
        }
        mailer.fromAddress('xyz@abc.com');
       mailer.tos().appendAddress('abc@xyz.com');
       mailer.tos().appendAddress('TEST@company.com');
      mailer.htmlBody('Find the attachment.
                         <Br>\n NOTE:This is a System Generated Email. Please do not Reply.');
      mailer.subject('Report Attached(Testing Mail)');
      info(pdfFileName);
      mailer.attachments().add("D:\\Test\\test.pdf");
       mailer.sendMail();
       CodeAccessPermission::revertAssert();
}
Next Configure this class into batch Job User Form.....
If any clarification or issues please comments to this..
Happy
Daxing...
  

5 comments:

  1. Thank you its very useful post.

    What if I do not want to hard code the user email-id's....?

    How can I select email id's dynamically....?

    ReplyDelete
  2. As per my thoughts if you want to select the email ids manually,U Know we need to create the form with email ids like parameter form,There we have to select the mail ids ,while running the batch process system goes to that table and pick the email id'.....

    ReplyDelete
  3. kindly i need to know how to run this as a scheduled batch
    also this line gives me an error
    CodeAccessPermission::revertAssert();

    ReplyDelete
    Replies
    1. Hi Hamdi,

      Kindly check the below links for batch processing ..and you said you are getting error right ,i think so can you please check if you useing below versions in ax2009.the problem with RollUps...

      http://axhelpdesk.blogspot.com/2013/03/how-to-send-emails-to-users-in-batch.html

      Thanks
      Brahma

      Delete
  4. Suppose if I want to save a Sales Invoice as PDF, how can I do it?

    ReplyDelete