Wednesday, April 29, 2015

Send email by batch with attachment - AX 2009


You must have a shared folder so that AX service account has full permission on it.

You also need to turn on the "Allow to connect printers.. " on AX server configuration and AX client configuration

Sample code:
    InteropPermission permission = new InteropPermission(InteropKind::ComInterop);
    SysMailer mailer;
    SysEmailParameters parameters;
    Set                                   permissionSet;
    System.Exception                      e;
    BatchJob _BatchJobDelete, bj;
    int j;
    FileIOPermission    _perm;
    ;

    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);
    }
    else
    {
        mailer.SMTPRelayServer(parameters.SMTPServerIPAddress,
                     parameters.SMTPPortNumber,
                     parameters.SMTPUserName,
                     SysEmailParameters::password(),
                     parameters.NTLM);
    }
    mailer.fromAddress(this.getmailFrom());
    mailer.tos().appendAddress(this.getMailTo());
    if (mailCC2 != connull())
    {
        for(j=1;j<=conlen(mailCC2);j++)
            mailer.ccs().appendAddress(strfmt('%1',conpeek(mailCC2,j)));
            //mailCCCollection.Add(strfmt('%1',conpeek(mailCC2,j)));
    }
    mailer.htmlBody(this.getMailBody());
    mailer.subject(this.getmailSubject());
    info(pdfFileName);
    mailer.sendMail();
    info("sent mail");
    CodeAccessPermission::revertAssert();

    _perm = new FileIOPermission(pdfFileName,'rw');
    _perm.assert();
    if(WinAPIServer::fileExists(pdfFileName))
        WinAPIServer::deleteFile(pdfFileName);

How to change Status of Withhold Batch job to Waiting - AX 2009

On class BatchChangeStatus, comment the Return to allow changing status if AX doesn't allow to change .

static void main(Args args)
{
    BatchChangeStatus   changeStatus;

    changeStatus = new BatchChangeStatus();
    BatchChangeStatus::initFromForm(changeStatus,args);

    if (! changeStatus.prompt())
//        return; //20150429 allow to change status

    changeStatus.run();

    if (args.caller())
    {
        args.caller().doReselect();
    }
}