• Batch Running reports and send email for attachment


    if the customer have not buy [Business analysis]  License , then the standard reports will can't  using to running reports with Batch jobs function in Dynamics AX 2009.

     so, I have a good Idear. just that Using some of x++ code achieve to it.

     

    Wrote by Jimmy xie on Feb 24th 2011

    Main code below as:

    1)Batch Running reports

    void runReport(QVS_BatchManageReportLine  ReportLine)
    {
        #WinAPI
        Args                args;
        SysReportRun        reportRun;
        Str 500             AttachmentPath;
        System.Exception    ex;
        reportname          reportname = ReportLine.ObjectName;
    ;
        if(ReportLine.ActivedCondition && ReportLine.Condition01 && (ReportLine.Condition01 != "1" || ReportLine.Condition01 != "0"))
            AttachmentPath = WinAPI::getFolderPath(#CSIDL_Personal) + "\\" +  reportName2Pname(reportname) +
                             ReportLine.Condition01 +  ReportLine.FileFormat;
        else
            AttachmentPath = WinAPI::getFolderPath(#CSIDL_Personal) + '\\' + reportName2Pname(reportname)  + ReportLine.FileFormat;//ReportLine.FileFormat - > ".PDF";
    
        if(WinAPI::fileExists(AttachmentPath))
            WinAPI::deleteFile(AttachmentPath);
        try
        {
            setprefix("Run report ... " + reportName2Pname(reportname));
            args = new Args();
            args.name(reportname);
            args.record(ReportLine);
            //reportRun = new ReportRun(args,'');
    
            reportRun = classFactory.reportRunClass(args);
    
            reportRun.setTarget(PrintMedium::File);
            reportRun.printJobSettings().preferredTarget(PrintMedium::File);
            //ReportLine.PrintFormat - > PrintFormat::PDF
            reportRun.printJobSettings().format(ReportLine.PrintFormat);
            reportRun.printJobSettings().preferredFileFormat(ReportLine.PrintFormat);
            reportRun.printJobSettings().fileName(AttachmentPath);
            reportRun.printJobSettings().warnIfFileExists(False);
            reportRun.printJobSettings().suppressScalingMessage(True);
            reportRun.query().interactive(false);
            reportRun.report().interactive(false);
            reportRun.run();
        }
        catch (Exception::CLRError)
        {
            ex = CLRInterop::getLastException();
            throw Error(strfmt('Running the report %1 generic %2!',reportname,ex.ToString()));
        }
        catch (Exception::CodeAccessSecurity)
        {
            info("Code Access Security Error");
            throw Error(strfmt('Running the report %1 generic CodeAccessSecurity!',reportname));
        }
        catch (Exception::Internal)  // This exception handler was the Magic Sauce!!
        {
            ex = CLRInterop::getLastException();
            if (ex)
                error(ex.ToString());
            else
                error("Internal Error");
        }
        catch (Exception::Error)
        {
            error("None of it worked (generic Exception)");
            throw Error(strfmt('Running the report %1 generic error!',reportname));
        }
    }
    

    2)Send email for attachment

    void sendEmail(QVS_BatchManageReportHeader  Header)
    {
        #WinAPI
        QVS_ProcessLongStr              _ProcessLongStr;
        SysMailer   mailer              = new SysMailer();
        SysEmailParameters parameters   = SysEmailParameters::find();
        QVS_BatchManageReportLine       ReportLine;
    
        List                            listTo,listCC;
        ListEnumerator                  leTo,leCC;
        Str 60                          CurMail;
        Str 150                         CurReceivedName;
        Name                            curUserName;
        str 500                         AttachmentPath;
        InteropPermission               interopPermission;
        System.Exception                ex;
    
    ;
        curUserName = (select firstonly UserInfo where UserInfo.id == curUserId()).Name;
    
        try
        {
            interopPermission = new InteropPermission(InteropKind::ComInterop);
            interopPermission.assert();
    
            if (parameters.SMTPRelayServerName)
                mailer.SMTPRelayServer(parameters.SMTPRelayServerName);
            else
                mailer.SMTPRelayServer(parameters.SMTPServerIPAddress);
    
            CurMail = parameters.SMTPUserName;
            if(!CurMail)
                CurMail = sysUserInfo::find(curUserId()).Email;
            if (!CurMail)
                CurMail = "dynax@qvsglobal.com.cn";
            if(CurMail)
                mailer.fromAddress(CurMail);
    
            //Attachment
            while select ReportLine where ReportLine.RelationId == Header.RelationId
                && ReportLine.ObjectName
            {
                if(strlrtrim(ReportLine.ObjectName))
                {
                    if(ReportLine.ActivedCondition && ReportLine.Condition01 && (ReportLine.Condition01 != "1" || ReportLine.Condition01 != "0"))
                        AttachmentPath = WinAPI::getFolderPath(#CSIDL_Personal) + "\\" +  reportName2Pname(ReportLine.ObjectName) +
                                         ReportLine.Condition01 +  ReportLine.FileFormat;
                    else
                        AttachmentPath = WinAPI::getFolderPath(#CSIDL_Personal) + '\\' + reportName2Pname(ReportLine.ObjectName)  + ReportLine.FileFormat;
                    if(WinAPI::fileExists(AttachmentPath))
                        mailer.attachments().add(AttachmentPath);
                }
            }
    
            //Send to
            _ProcessLongStr = new QVS_ProcessLongStr();
            _ProcessLongStr.ProcessMulitList(Header.Sendto,";\n");
            listTo  = _ProcessLongStr.parmList();
            leTo    = listTo.getEnumerator();
            while(leTo.moveNext())
            {
                if(strlrtrim(leTo.current()))
                {
                    mailer.tos().appendAddress(leTo.current());
                    if(!CurReceivedName)
                        CurReceivedName =  subStr(leTo.current(),0,strScan(leTo.current(),'@',1,StrLen(leTo.current()))-2);
                    else
                        CurReceivedName += ',' + subStr(leTo.current(),0,strScan(leTo.current(),'@',1,StrLen(leTo.current()))-2);
                }
    
            }
    
            //CC
            _ProcessLongStr = new QVS_ProcessLongStr();
            _ProcessLongStr.ProcessMulitList(Header.CCTo,";\n");
            listCC = _ProcessLongStr.parmList();
            leCC   = listCC.getEnumerator();
    
            while(leCC.moveNext())
            {
                if(strlrtrim(leCC.current()))
                {
                    mailer.ccs().appendAddress(leCC.current());
                    /*
                    if(!CurReceivedName)
                        CurReceivedName =  subStr(leCC.current(),0,strScan(leCC.current(),'@',1,StrLen(leCC.current()))-1);
                    else
                        CurReceivedName += ',' + subStr(leCC.current(),0,strScan(leCC.current(),'@',1,StrLen(leCC.current()))-1);
                    */
                }
            }
    
            if(Header.SubJect)
                mailer.subject(Header.SubJect);
            else
                mailer.subject("Batch Job Run To Report");
            if(Header.Body)
                mailer.htmlBody(strfmt("<font size=2 face=Arial>%1</font>",Header.Body)
                               );
    
            else
                mailer.htmlBody('<B><font color= "#36648B">'+
                                'Hi ' + CurReceivedName + ',' +
                                '<br><p>    ' +
                                'This email and Attachment is an automatically  sent to you,</br>' +
                                '<br>    ' +
                                'It was send by the ERP system batch function generates report.</br>' +
                               // '<br>    ' +
                               // 'Please be informed that!'+
                               // '</br>
                                '</p></font></B>'+
                                '<font color = "blue">' +
                                'Best Regards,' +
                                '<br>' + curUserName +
                                '</br></font></br>');
    
            mailer.sendMail();
        }
        catch (Exception::CLRError)
        {
            ex = CLRInterop::getLastException();
            info(ex.ToString());
        }
        catch (Exception::CodeAccessSecurity)
        {
            info("Code Access Security Error");
        }
        catch (Exception::Internal)  // This exception handler was the Magic Sauce!!
        {
            ex = CLRInterop::getLastException();
            if (ex)
                info(ex.ToString());
            else
                info("Internal Error");
        }
        catch (Exception::Error)
        {
            info("None of it worked (generic Exception)");
            throw Error('Send the report Attachment Error!');
        }
    }
    

    3)store reports attachment.

    void UpdateLogFile(QVS_BatchManageReportHeader  Header)
    {
        BinData                     binData = new BinData();
        QVS_BatchJobRunToReportLog  Log;
        QVS_BatchManageReportLine   Line;
        filePath                    AttachmentPath;
        #WinAPI
    ;
        while select Line where Line.RelationId == Header.RelationId
            && Line.ObjectName != ""
        {
            Log.clear();
            if(Line.ActivedCondition && Line.Condition01 && (Line.Condition01 != "1" || Line.Condition01 != "0"))
                AttachmentPath = WinAPI::getFolderPath(#CSIDL_Personal) + "\\" +  reportName2Pname(Line.ObjectName) +
                                 Line.Condition01 +  Line.FileFormat;
            else
                AttachmentPath = WinAPI::getFolderPath(#CSIDL_Personal) + '\\' + reportName2Pname(Line.ObjectName)  + Line.FileFormat;
            ttsbegin;
            Log = QVS_BatchJobRunToReportLog::find(Header.PrjId,Line.LineNum,true);
            if(Log && WinAPI::fileExists(AttachmentPath))
            {
                binData.loadFile(AttachmentPath);
                Log.file            = binData.getData();
                Log.PrintFormat     = Line.PrintFormat;
                Log.Fileformat      = Line.Fileformat;
                Log.FilePath        = AttachmentPath;
                Log.update();
            }
            ttscommit;
            sleep(5000);
            if(WinAPI::fileExists(AttachmentPath))
                WinAPI::deleteFile(AttachmentPath);
        }
    
    }
    
  • 相关阅读:
    AWS上的游戏服务:Lumberyard + Amazon GameLift + Twitch
    Android 屏幕旋转 处理 AsyncTask 和 ProgressDialog 的最佳方案
    iOS多线程与网络开发之小文件上传
    VMware-Fusion-7.0.0-2103067 Pro SN:序列号+ 百度云下载地址
    PCA的数学原理Matlab演示
    typedef,结构体,共用体,联合体
    XMPP系列(三)---获取好友列表、加入好友
    王立平--eclipse中改动android项目的版本
    linux入门
    机器学习(3)——多变量线性回归
  • 原文地址:https://www.cnblogs.com/Fandyx/p/1963961.html
Copyright © 2020-2023  润新知