Skip to main content

D365 : Upload Via Excell Code

/// <summary>

///            Modification : Added NoYes dialog box and export template feature for Changing PO

/// </summary>

/// <remarks>

///         Class for updating Department in PO.

/// </remarks>

using System.IO;

using OfficeOpenXml;

class LT_Supp_UploadDepPO

{

    public static void main(Args args)

    {

        LT_Supp_UploadDepPO updateCoverRes = new LT_Supp_UploadDepPO();

        DialogButton diagBut;

        ;

        diagBut = Box::yesNoCancel('Do you wish to upload ->"Yes" or Export template ->"No" or Abort->"Cancel"', DialogButton::Yes, 'Click');

        

        if(diagBut == DialogButton::Yes)

        {

            updateCoverRes.run();

            info("Processing completed");

        }

        else if(diagBut == DialogButton::No)

        {

            updateCoverRes.exportTemplate();

        }

    }


    /// <summary>

    /// Export template for this job

    /// </summary>

    public void exportTemplate()

    {

        int i = 1;

        MemoryStream memoryStream = new MemoryStream();


        using (var package = new ExcelPackage(memoryStream))

        {

            var currentRow = 1;

            var worksheets = package.get_Workbook().get_Worksheets();

            var CustTableWorksheet = worksheets.Add("Export");

            var cells = CustTableWorksheet.get_Cells();

            ;

            OfficeOpenXml.ExcelRange cell = cells.get_Item(currentRow, i);

            System.String value = "Purchase order number";

            cell.set_Value(value);

            i++;


            cell = null;

            value = "From Department";

            cell = cells.get_Item(currentRow, i);

            cell.set_Value(value);

            i++;


            cell = null;

            value = "To Department";

            cell = cells.get_Item(currentRow, i);

            cell.set_Value(value);

            i++;


            package.Save();

            file::SendFileToUser(memoryStream, 'Update_Purchase_Order_Department.xlsx');

        }

    }


    /// <summary>

    /// Get the values from file and Upload Purchase Order Department Through Excell

    /// </summary>

    public void run()

    {

        FileUploadTemporaryStorageResult fileUploadResult;

        OfficeOpenXml.ExcelRange range;

        System.IO.Stream stream;

        ExcelWorksheet worksheet;

        ExcelSpreadsheetName sheeet;


        FormBuildControl formBuildControl;

        FileUploadBuild fileUpload, fileUploadBuild;

        DialogGroup dlgUploadGroup;

        FileUpload fileUploadControl;

        Dialog dialog = new Dialog('Upload Purchase Order Department Through Excell');

        int rowCount, row, i = 1;


        PayrollLeaveRequestId_GCC leaveRequestId;

        PurchTable                  purchTable,purchtableloc;

        str legalCompany;

        PurchIdBase                         PurchId;

        OMOperatingUnitNumber               toDept,fromdept;


        ;

        

        dlgUploadGroup = dialog.addGroup('@SYS54759');

        formBuildControl = dialog.formBuildDesign().control(dlgUploadGroup.name());

        fileUploadBuild = formBuildControl.addControlEx(classstr(FileUpload), 'Upload');

        fileUploadBuild.style(FileUploadStyle::MinimalWithFilename);

        fileUploadBuild.fileTypesAccepted('.xlsx');

    

        if (dialog.run() && dialog.closedOk())

        {

            fileUploadControl   = dialog.formRun().control(dialog.formRun().controlId('Upload'));

            fileUploadResult = fileUploadControl.getFileUploadResult();

            if (fileUploadResult != null && fileUploadResult.getUploadStatus())

            {

                stream = fileUploadResult.openResult();

                using (ExcelPackage package = new ExcelPackage(stream))

                {

                    package.Load(stream);

                    worksheet   = package.get_Workbook().get_Worksheets().get_Item(1);

                    range       = worksheet.Cells;

                    rowCount    = worksheet.Dimension.End.Row - worksheet.Dimension.Start.Row + 1;

                    for (row = 2; row <= rowCount ; row++)

                    {

                        PurchId = range.get_Item(row, 1).value;

                        toDept = range.get_Item(row, 2).value;

                        fromdept    = range.get_Item(row, 3).value;


                        purchtableloc = PurchTable::find(PurchId);

                        

                       /* select purchtableloc

                            where(purchtableloc.PurchStatus == PurchStatus::None

                            ||  purchtableloc.PurchStatus == PurchStatus::Received);*/

                                    

      

                        if(purchtableloc)

                        {

                            //i = 3;

                            ttsbegin;

                            purchtableloc.selectForUpdate(true);

                            purchtableloc.LTDepartment = fromdept;

                            purchtableloc.modifiedField(fieldNum(purchTable, LTDepartment));

                            purchtableloc.doupdate();


                            info(strFmt("PO No -- %1, Before Departement -- %2 ,After Department ---%3",purchtableloc.PurchId,toDept,purchtableloc.LTDepartment));

                            ttscommit;

                        }


                    }

                }

            }

            else

            {

                error('Error: Uploading failed');

            }

        }

    }


Comments

Popular posts from this blog

Customization Tips for D365 for Finance and Operations How to configure Excel Add in D365 Finance and Operations August 1, 2021 by alirazazaidi I recorded how to configure Excel Addin for D365 finance and operations. How to configure Power BI out of the box dashboard on local VM Dynamics 365 for Finance and Operations July 17, 2021 by alirazazaidi Hi friends, here is video, which I recording during configuring Dynamics 365 Finance and operations out of the box dashboard on local VM. My session is held on Urdu Hindi User group on 1 August. For this I need to configure it. During development we need to configure these dashboards and some times we need to extend these dashboard. Hope this video will helpful to you. How hide SSRS Parameter RDP Menu Name Dynamics 365 Finance and Operations

  Again documentation class XYZUIBuilder extends SrsReportDataContractUIBuilder { public void build() { RecId hcmWorkerRecId; str name, menuItemName; FormBuildGroupControl grp; //Dialog dialogLocal = this.dialog(); XYZDC rdpContract = this.dataContractObject(); DialogField workerfieldDiff; DialogField _FromDate,_Todate; // Add the pay cycle field. menuItemName = this.controller().parmArgs().menuItemName(); workerfieldDiff = this.addDialogField(methodStr(XYZDC ,parmworkerrecid),rdpContract); // hcmWorkerRecId = HcmWorker::userId2Worker(curUserId()); if (menuItemName =="ESSReport") { workerfieldDiff.visible(false); workerfieldDiff.value(0); } _FromDate= this.addDialogField(methodStr(BEKenyaP9DC,parmFromDate),rdpContract); _Todate = this.addDialogField(methodStr(BEKenyaP9DC,parmToDate),rdpContract); } }

D365 : SEND EMAIL

 class SRSendEmail_D365 {            public static void main(Args _args)     {           SysMailerMessageBuilder messageBuilder = new SysMailerMessageBuilder();         Email   toEmail;         Email   fromEmail;                   try         {             FromEmail = "fromemail@xyzcompany.com";             toEmail   = "oemail@xyzcompany.com";             messageBuilder.setBody("Hello from D365", false);             messageBuilder.setSubject("Email Test from D365");             messageBuilder.addTo(toEmail);             // Note: not calling setFrom() defaults to the current user for SMTP client, whereas ...

CODE TO PDF IN AX 2012

static void Job1(Args _args) {         PurchPackingSlipController      ssrsController = new SrsReportRunController();         TradeDocumentReportContract     purchPackingSlipContract = new TradeDocumentReportContract();         SRSPrintDestinationSettings     printerSettings;         VendPackingSlipJour             VendPackingSlipJour;         Args                            args;         //select the latest record based on create date         while select VendPackingSlipJour             order by VendPackingSlipJour.createdDateTime DESC             where VendPackingSlipJour.PackingSlipId == 'LJ-01'         ...