Tuesday, June 27, 2023

Code to Upoload CSV file of Vendor payment journal to Azure blob storage when posted in D365 X++

 using Microsoft.WindowsAzure.Storage;

Using Microsoft.WindowsAzure.Storage.Blob;

[ExtensionOf(classStr(LedgerJournalPost))]

internal final class DaxLedgerJournalPost_Extension

{

    public static void post(Common  _record,TableId _tableId,

    NoYes _transferErrors,

            boolean _splitLargeJournal,

            boolean _suppressClientMessages,

            str _callingFormName,LedgerJournalEngine _ledgerJournalEngine,

                            boolean _skipIL_RU,LedgerJournalCheckPostResults _results_RU,boolean _progressBarHide)

    {

        LedgerJournalTable  ledgerJounralTable;

        LedgerJournalTrans  ledgerjournalTrans;

        LedgerJournalPost  post = new LedgerJournalPost();

        next post(_record, _tableId, _transferErrors, _splitLargeJournal, _suppressClientMessages, _callingFormName, _ledgerJournalEngine, _skipIL_RU, _results_RU, _progressBarHide);

        if(_record.TableId == tableNum(LedgerJournalTrans))

        {

            ledgerjournalTrans = _record;

            ledgerJounralTable = LedgerJournalTable::find(ledgerjournalTrans.JournalNum);

        }

        if(_record.TableId == tableNum(LedgerJournalTable))

        {

            ledgerJounralTable = _record;

        }

        post.csvfile(ledgerJounralTable);


    }


//Method to create CSv file and upload it to AzureBlob storage.

public void csvfile(LedgerJournalTable  ledgerJournalTable)

{

        CloudStorageAccount                                 storageAccount;

        PersonnelIntegrationStorageAccountConnectionString  azureStorageKey;

        storageAccount = CloudStorageAccount::Parse("DefaultEndpointsProtocol=https;AccountName=lohithstorageaccount123;AccountKey=C1XNEuXtv0GIB2FQ18iFEuZgafJEcMVGE/m1G3sjoTnbni9tCK1htweacZwuLZxrc/9nlksRVvLg+AStVpXnqA==;EndpointSuffix=core.windows.net"); // AccessKey connection string of storage Account.

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

        System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();

        CloudBlobClient     blobClient;

        CloudBlobContainer  blobContainer;

        CloudBlockBlob      blockblob;

        CloudBlobDirectory      cbDir;

        str   ledgerDim;

        str                 containerName = "lohithcontainer", dirPath;

        dirPath = "newfolder";

        XmlDocument doc;

        commaStreamIo       iO = commaStreamIo::constructForWrite();

        Filename            filename = "MyFile.csv";

          //  Field List;

        container header = ["JournalNumber","VoucherNumber","Account number","Date","Debit","Credit"];

        iO.writeExp(header);

        header = conNull();

        LedgerJournalTrans  ledgerJournalTrans;

          while select ledgerJournalTrans

            where ledgerJournalTrans.JournalNum == ledgerJournalTable.JournalNum

        {

            ledgerDim = LedgerDimensionFacade::getDisplayValueForLedgerDimension(ledgerJournalTrans.LedgerDimension);

            container line =  [ledgerJournalTrans.JournalNum,ledgerJournalTrans.Voucher,ledgerDim,ledgerJournalTrans.TransDate,ledgerJournalTrans.AmountCurDebit,ledgerJournalTrans.AmountCurCredit];

              iO.writeExp(line);

        }

        System.IO.Stream stream = iO.getStream();

        stream.Position = 0;

        System.IO.StreamReader reader = new System.IO.StreamReader(stream);

        str  csvFileContent = reader.ReadToEnd();

        File::SendStringAsFileToUser(csvFileContent,  filename);

        info(strFmt("CSV file %1 is created", filename));

        if(storageAccount && containerName)

        {

            blobClient      = storageAccount.CreateCloudBlobClient();

            blobContainer   = blobClient.GetContainerReference(containerName);

            cbDir           = blobContainer.GetDirectoryReference(dirPath);

            blockblob       = cbDir.GetBlockBlobReference(filename);

            if (blockBlob)

            {

                if (stream)

                {

                    stream.Position = 0;

                    blockBlob.UploadFromStream(stream,null,null,null);

                    blockBlob.FetchAttributes(null, null, null);

                  }

            }

        }

    }

  }

Accounts Payable>Payments>Vendor Payment Journal.


Storage Account > Bob >Container > New Folder> file.







No comments:

Post a Comment