Send Auto-Email when sales order posting a packing slip

Today i am going to show you code how to send an auto-email through x++ code when the sales order posting a packing slip 

class DAL_SalesFormLetterClass_EventHandler

{

 

    [PostHandlerFor(classStr(SalesFormLetter), methodStr(SalesFormLetter, run))]

    public static void SalesFormLetter_Post_run(XppPrePostArgs args)

    {

     SalesFormLetter  SalesFormLetter = args.getThis() as SalesFormLetter ;

     if(SalesFormLetter.documentStatus() == DocumentStatus::PackingSlip

        ||  SalesFormLetter.documentStatus() == DocumentStatus::ProjectPackingSlip

        && !SalesFormLetter.proforma() )

        {

            custPackingSlipJour     custPackingSlipJour = SalesFormLetter.parmJournalRecord();

              if(custPackingSlipJour)

               this.sendMail_Sales_PackingSlip(custPackingSlipJour);

        }

    }


public void sendMail_Sales_PackingSlip(CustPackingSlipJour _CustPackingSLipJour)

    {

        Set                                   permSet;

        FileName                              filename,localfilename;

        SRSPrintDestinationSettings           printerSettings;

        Array                                 arrayFiles;

        System.Byte[]                         reportBytes = new System.Byte[0]();

        SRSProxy                              srsProxy;

        SRSReportRunService                   srsReportRunService = new SrsReportRunService();

        Microsoft.Dynamics.AX.Framework.Reporting.Shared.ReportingService.ParameterValue[]                                                   parameterValueArray;

        Map                                   reportParametersMap;

        SRSReportExecutionInfo                executionInfo = new SRSReportExecutionInfo();

        int                                   tryagain;

        str                                   email;

        Notes                                 content;

        MenuFunction                          mf;

        FormRun                               formrun;

        Args                                  _args;

        EmailBase                             ToMailId;

        SalesTable                            salesTableObj;

        str                                   subjectPart,bodyMessage;

        HcmWorkerName                         salesResponsible;

        ;

        select salesTableObj where salesTableObj.SalesId ==  _CustPackingSLipJour.SalesId;

        if(salesTableObj.WorkerSalesResponsible)

        {

       if(salesTableObj.ProjId && salesTableObj.SalesType == SalesType::ItemReq)

       {

       bodyMessage = strFmt('The items requested  vide  work order number %3 is issued through packing slip number %1 dated %2 ',_CustPackingSLipJour.PackingSlipId,_CustPackingSLipJour.DeliveryDate,salesTableObj.ProjId);

        subjectPart = "work order number -" + salesTableObj.ProjId;

       }

       else

       {

                if(salesTableObj.SalesId && salesTableObj.SalesType == SalesType::Sales )

                {

                    bodyMessage = strFmt('The items requested  vide sales order number %3 is issued  through packing slip number %1 dated %2 ',_CustPackingSLipJour.PackingSlipId,_CustPackingSLipJour.DeliveryDate,salesTableObj.SalesId);

                    subjectPart = "Sales order number -" + salesTableObj.SalesId;

                }

        }

            ToMailId = DALParameterTable::getUserEmail(salesTableObj.WorkerSalesResponsible);

            salesResponsible = HcmWorker::find(salesTableObj.WorkerSalesResponsible).name();

 

            System.IO.Stream workbookStream = new System.IO.MemoryStream();

            SysMailerSMTP           mailer = new SysMailerSMTP();

            SysMailerMessageBuilder builder = new SysMailerMessageBuilder();

            SysEmailParameters      parameters = SysEmailParameters::find();

            

            if (parameters.SMTPRelayServerName)

            {

                mailer.SMTPRelayServer(parameters.SMTPRelayServerName,

                                       parameters.SMTPPortNumber,

                                       parameters.SMTPUserName,

                                       SysEmailParameters::password(),

                                       parameters.SMTPUseNTLM);

            }

            else

            {

                warning("@ApplicationFoundation:EmailProviderSMTPServerNotFound");

            }

 

            content ="";

           

            content = content + strfmt("\n<p dir=ltr align=left><font size='2' face='arial'>Dear %1, </font></p> ",salesResponsible);

            content =  content + strfmt('\n<p dir=ltr align=left>                   </p>');

            content =  content + strfmt('\n<p dir=ltr align=left>                     </p>');

            content = content + strfmt("\n<p dir=ltr align=left><font size='2' face='arial'> %1 </font></p> ",bodyMessage);

                   

            content =  content + strfmt('\n<p dir=ltr align=left></p>');

            content =  content + strfmt('\n<p dir=ltr align=left></p>');

 

 

 

            content =  content + strfmt('\n<p dir=ltr align=left>             </p>');

            content =  content + strfmt('\n<p dir=ltr align=left>             </p>');

 

            content =  content + strfmt('\n<p dir=ltr align=left>             </p>');

            content = content + strfmt("\n<p dir=ltr align=left><font size='2' face='arial'>Thanks & Regards,</font></p> ");

            content = content + strfmt("\n<p dir=ltr align=left><font size='2' face='arial'> Procurement Team</font></p> ");

            content =  content + strfmt('\n<p dir=ltr align=left></p>');

            content =  content + strfmt('\n<p dir=ltr align=left>              </p>');

            content = content + strfmt("\n<p dir=ltr align=left> <B><font size='2' face='arial'>This email was sent by an automated system. Please do not reply to this message.</font></B></p> ");

                   

            sleep(5000);     //Wait 5 seconds for network file write

            tryagain = 0;

 


            builder.setFrom(parameters.SMTPUserName);

            builder.addTo(ToMailId);

           

            builder.setSubject(strfmt("Items issued for %1",subjectPart));

            builder.setBody(content);

          

            SysMailerFactory::getNonInteractiveMailer().sendNonInteractive(builder.getMessage());

        }

    }


}

}

Output:

    


 


Comments

Popular posts from this blog

SSRS Report using Controller , Contract and RDP classes in D365

Exporting data to Excel through X++ code

COC for Form level method

How to pass the parameter from one form to another in Dynamic365

Multi Select Lookup in SSRS Report in D365