Wednesday, February 21, 2024

Code to Import the Journals from excel file in D365F&O X++


using System.IO;

using OfficeOpenXml;

using OfficeOpenXml.ExcelPackage;

using OfficeOpenXml.ExcelRange;

internal final class Daximportjournal

{

   public static void main(Args _args)

   {

       System.IO.Stream         stream;

       FileUploadBuild          fileUpload;

       DialogGroup              dlgUploadGroup;

       FileUploadBuild          fileUploadBuild;

       FormBuildControl         formBuildControl;

       str                      journalnum,businessunit,costcenter,department,itemgroup,projectid,Journalname;

       LedgerJournalTable       ledgerJournalTable,ledgerJournalTableloc,ledgerJournalTableloc1;

       LedgerJournalTrans       ledgerJournalTrans;

       ledgerJournalName        ledgerJournalName;

       Dialog                   dialog = new Dialog("Import of Journals");

       LedgerJournalTable       legderjournalTable;

       DimensionDefault         result;

       DimensionAttribute       dimensionAttribute;

       NumberSeq                numberseq;

       DimensionAttributeValue  dimensionAttributeValue;

 

       DimensionAttributeValueSetStorage    valueSetStorage = new DimensionAttributeValueSetStorage();

       container                           conAttr = ["BusinessUnit", "CostCenter", "Department","ItemGroup","Project"];

 

       dlgUploadGroup = dialog.addGroup("Select excel file");

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

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

       fileUploadBuild.style(FileUploadStyle::MinimalWithFilename);

       fileUploadBuild.fileTypesAccepted('.xlsx');

 

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

       {

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

           FileUploadTemporaryStorageResult fileUploadResult = fileUploadControl.getFileUploadResult();

 

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

           {

               stream = fileUploadResult.openResult();

               using (OfficeOpenXml.ExcelPackage package = new OfficeOpenXml.ExcelPackage(stream))

               {

                   int rowCount, i,y;

                   Package.Load(stream);

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

                   OfficeOpenXml.ExcelRange range = worksheet.Cells;

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

                   try

                   {

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

                       {

                           journalnum      = range.get_Item(i, 1).value;

                           businessunit    = range.get_Item(i, 2).value;

                           costcenter      = range.get_Item(i, 3).value;

                           department      = range.get_Item(i, 4).value;

                           itemgroup       = range.get_Item(i, 5).value;

                           projectid       = range.get_Item(i, 6).value;

                           Journalname     = range.get_Item(i, 7).value;

                           container               conValue = [businessunit, costcenter, department,itemgroup,projectid];

                           str                     dimValue;

                           for (y = 1; y <= conLen(conAttr); y++)

                           {

                               dimensionAttribute = dimensionAttribute::findByName(conPeek(conAttr,y));

                               if (dimensionAttribute.RecId == 0)

                               {

                                   continue;

                               }

                               dimValue = conPeek(conValue,y);

                               if (dimValue != "")

                               {

 

                                   dimensionAttributeValue = dimensionAttributeValue::findByDimensionAttributeAndValue(dimensionAttribute,dimValue,false,true);

                                   valueSetStorage.addItem(dimensionAttributeValue);

                               }

                           }

                           conValue = conNull();

                           result = valueSetStorage.save();

                           ledgerJournalTableloc = LedgerJournalTable::find(journalnum,false);

                           if(ledgerJournalTableloc)

                           {

                               select forupdate ledgerJournalTable

                               where ledgerJournalTable.JournalNum == journalnum;

                       

                               ttsbegin;

                               ledgerJournalTable.DefaultDimension = result;

                               ledgerJournalTable.update();

                               ttscommit;

                           }

                           else

                           {

                               ttsbegin;

                               ledgerJournalTableloc.clear();

                               ledgerJournalTableloc.initValue();

                               ledgerJournalTableloc.JournalNum = journalnum;

                               ledgerJournalTableloc.JournalName = Journalname;

                               ledgerJournalTableloc.JournalType = ledgerjournaltype::Daily;

                               ledgerJournalTableloc.DefaultDimension = result;

                               ledgerJournalTableloc.insert();

                               ttscommit;

                               select ledgerJournalTableloc1

                                   where ledgerJournalTableloc1.JournalName == Journalname;

 

                               numberSeq = NumberSeq::newGetVoucherFromId(ledgerJournalTableloc1.NumberSequenceTable,false);

 

                               ledgerJournalTrans.clear();

                               ledgerJournalTrans.initValue();

                               ledgerJournalTrans.defaultRow();

                               ledgerJournalTrans.JournalNum = ledgerJournalTableloc.JournalNum;

                               ledgerJournalTrans.Voucher = numberseq.voucher();

                               ledgerJournalTrans.insert();

                           }

                       }

                   }

                   catch (Exception::Error)

                   {

                       ttsabort;

                       throw error("error here");

                   }

                   

               }

           }

       }

   }

}

............................................

Output :









No comments:

Post a Comment