Thursday, October 14, 2010

Copying Infolog -Sending EMail from ax2009

This is a small post to send and configure mail in ax 2009.I have had a requirement to copy the infolog at the end of some function and send it to the user. This is a code snippet (not the actual one though) how I managed.
First you need to set a template for the mail (Basic-> set up->Email Template). I have created a template as shown.

%name% and %matter% are dynamic macro taken from runtime.You can have your code to take up customer name and description from a failed sales order for an instance.
A job is written, which produces three info message logs and sent it to the email.The template is helpful in forming email body parameters. You can send the mail from the email address of customer.

static void SendEmail(Args _args)
    SysInetMail mail;
    Map mappings;
    COM document;
    COM body;
    str ret, subject, matter;
    sysemailid emailid;
    languageId languageId;
    SysEmailMessageTable message;
    Xinfo info;
    int infoLogCounter;
    Log log;
    int i=1;
    container con;

    SysInfologEnumerator enum ;

    info( " hello world");
    error('this is error');
    error('this is  error 2 ');
    //info - if you want to copy these info msg to a email in some functionality
     log = Info::infoCon2Str( infolog.copy(1,infolog.num()));
     con = infolog.copy(1,infolog.num());
     enum = SysInfologEnumerator::newData(con );
      while (enum.moveNext())
        matter +=strfmt('
%1 -> %2 ',int2str(enum.currentException()), enum.currentMessage());

    emailId = 'Log';
    languageId = 'en-us';
    mail = new SysInetMail();
    mail.parmForceSendDialog(true); // this will show u a dialog before sending.
    mappings = new Map(Types::String, Types::String);
    mappings.insert('name', 'parthav');
    mappings.insert('matter',matter );
    message = SysEmailMessageTable::find( emailId,languageId);
    ret = SysEmailMessage::stringExpand(message.Mail, mappings);
    ret = WebLet::weblets2Html4Help(ret, '');
    document = new COM(#HTMLDocumentClassName);
    SysEmailTable::insertHTML2Document(document, ret);
    body = document.body();
    subject = SysEmailMessageTable::find(emailId, languageId).Subject;
    mail.sendMail( ''   ,  'CSV Import Log',    body.OuterText() ,false);
After hitting f5,you will get a dialog and composition of email. You can avoid this by setting mail.parmForceSendDialog(false) in the above code.

You are now ready to send the mail of your report.

No comments:

Post a Comment