Handling the variable declared as private in D 365 through reflection



Error Faced: The BankPositivePayTable is private and cannot be handled in the method send file to destination.
The Requrement was to handle BankPositivePayTable variable declared  as private in the method of same class. So we used Reflection to handle this variable declared as Private. Below is the attached code on how we tackle the scenario.

public class BankPositivePayExport extends RunBase
{
    private BankAccountID bankAccountId;
    private BankAccountTable bankAccountTable;
    private AllSelected companySelection;
    private AllSelected bankSelection;
    private DataAreaId dataAreaId;
    private TransDate cutOffDate;
    private BankPositivePayFormatName payFormat;
    private DialogField dialogPayFormat;
    private DialogField dialogBank;
    private DialogField dialogCompany;
    private DialogField dialogcutOffDate;
    private DialogField dialogSelectedBank;
    private DialogField dialogSelectedCompany;
    protected SysQueryRun queryRun;
    private DialogRunbase dialog;
    private MenuItemNameAction callerMenuItemName;
    private BankPositivePayTable bankPositivePayTable;
    protected SharedServiceUnitFileID fileId;

final class Test_BankPositivePayExport_Extension
{
   
    private AmountCur totalAmt = 0;
    private AmountCur voidAmt = 0;
    protected void populateBankPositivePayTable(BankPositivePayTable _bankPositivePayTable, int   _voidQty, int _totalQty)
    {
        next populateBankPositivePayTable(_bankPositivePayTable,_voidQty,_totalQty);
           
  _bankPositivePayTable.Test_TotalAmt   = totalAmt;
        _bankPositivePayTable.Test_VoidAmt    = voidAmt;
        totalAmt    = 0;
        voidAmt     = 0;
    }
 
    protected final void setChequeProcessedByPositivePay(RecId _bankChequeTableRecId)
    {
        next setChequeProcessedByPositivePay(_bankChequeTableRecId);
        BankChequeTable bankChequeTable = BankChequeTable::findByRecId(_bankChequeTableRecId, false);
        if (bankChequeTable.ChequeStatus == ChequeStatus::Void
                  || bankChequeTable.ChequeStatus == ChequeStatus::Cancelled
                  || bankChequeTable.ChequeStatus == ChequeStatus::Rejected)
        {
            voidAmt += bankChequeTable.AmountCur;
        }
        totalAmt += bankChequeTable.AmountCur;
   }

   [Replaceable]
    protected void sendFileToDestination()
    {
        var bindFlags  = BindingFlags::Instance | BindingFlags::NonPublic;
        var tableCustom = this.GetType().GetField(identifierStr(bankPositivePayTable), bindFlags);
        BankPositivePayTable bankPositivePayTableLoc;
       
        if (tableCustom)
        {
            bankPositivePayTableLoc = tableCustom.GetValue(this); // getting value
        }
       
        BankPositivePayFormat positivePayFormat = BankPositivePayFormat::findByFormatName(bankPositivePayTableLoc.PayFormat);
        Test_BankPositivePayOutputLocation bankPositivePayOutputLocation = Test_BankPositivePayOutputLocation::find(positivePayFormat.FormatName);
        if(!bankPositivePayOutputLocation)
        {
            throw error(strFmt("PositivePayLocationUndefined", positivePayFormat.FormatName));
        }
       
        this.sendFileToAzureFileStorage(bankPositivePayOutputLocation);
    }

    private void sendFileToAzureFileStorage(Test_BankPositivePayOutputLocation bankPositivePayOutputLocation)
    {
       
        var bindFlags  = BindingFlags::Instance | BindingFlags::NonPublic;
        var tableCustom = this.GetType().GetField(identifierStr(bankPositivePayTable), bindFlags);
        BankPositivePayTable bankPositivePayTableLoc;
       
        if (tableCustom)
        {
            bankPositivePayTableLoc = tableCustom.GetValue(this); // getting value
        }
               str downloadUrl = DMFDataPopulation::getAzureBlobReadUrl(str2Guid(fileId));
        Filename    filename = strFmt('%1-%2%3', bankPositivePayTableLoc.PayFormat, bankPositivePayTableLoc.PositivePayNum, this.getFileExtensionFromURL(downloadUrl));
        System.IO.Stream stream = File::UseFileFromURL(downloadUrl);
        str connectionString;
        str keyStr;
       
       
        keyStr = bankPositivePayOutputLocation.keyEdit(false, null);
        connectionString = strFmt("@Test:AzureConnectionString", bankPositivePayOutputLocation.Test_AzureStorageAccountName, keyStr );
        try
        {
            ECL_AzureStorageUtility.AzureStorageUtility::uploadStreamToFileStorage(connectionString, bankPositivePayOutputLocation.Test_AzureShareName, bankPositivePayOutputLocation.Test_AzureFileLocation, filename, stream);
            info(strFmt("AzurePositivePaySuccess", filename, bankPositivePayOutputLocation.Test_AzureShareName, bankPositivePayOutputLocation.Test_AzureFileLocation));
        }
        catch (Exception::CLRError)
        {
            throw error(AifUtil::getClrErrorMessage());
        }      
    }
}

Thank you
D 365 Learner

Comments

Popular posts from this blog

Identifying the Database Synchronization Issue in D 365 FO

Steps to create new cheque layout in D365 FO

SSRS Reports using the QueryInsertRecordSet in D 365 FO