25 October 2021

D365FO disposable context class example

Scenario: Payment journal Customer receipt data is not getting.

Below is the route cause: the temp table record is getting cleared.

To fix issue implemented the disposable context concept.

New class :

class CITPaymProposalReportContext implements System.IDisposable
{
    static CITPaymProposalReportContext instance;
    public CustVendPaymProposalTmp vendPaymProposalTemp;

    public void new()
    {
        if (instance)
        {
            throw Error ("Nesting of CITPaymProposalReportContext is not supported.");
        }
        instance = this;
    }

    public void dispose()
    {
        instance = null;
    }

    static public CITPaymProposalReportContext current()
    {
        return instance;
    }

}


Before clearing the record to get it and keep it in global variable using context class so need to create extension for the "CustVendPaymProposalTmp" table insert() method.

[ExtensionOf(TableStr(CustVendPaymProposalTmp))]
final class CITCustVendPaymProposalTmp_TBL_Extension
{
    public void insert()
    {
        using (CITPaymProposalReportContext context = new CITPaymProposalReportContext())
        {
            next insert();
            context.vendPaymProposalTemp = this;
        }

    }

}

Create another extension- Post COC - "PaymentProposalReport" insert payment method and get the record and assign to variable.

[ExtensionOf(classStr(PaymProposalReport))]
final class CITPaymProposalReportClass_Extension
{

  protected void insertPayment(CustVendPaymProposalTmp _custVendPaymProposalTmp)
    {
        
        LedgerJournalTrans ledgerJournalTrans;
        CustVendPDCRegister custVendPDCRegister;
        
        next insertPayment(_custVendPaymProposalTmp);

        CITPaymProposalReportContext context = CITPaymProposalReportContext::current();
        if (context && context.vendPaymProposalTemp)
        {
            _custVendPaymProposalTmp = context.vendPaymProposalTemp;
        }
        
        select ledgerJournalTrans
                        where ledgerJournalTrans.RecId == _custVendPaymProposalTmp.WLKLedgerJournalTransRecId;
        
        if(ledgerJournalTrans)
        {
            _custVendPaymProposalTmp.WLKAmountInTxt = Global::numeralsToTxt(abs(_custVendPaymProposalTmp.PaymentAmountMST));
            _custVendPaymProposalTmp.WLKTxt = ledgerJournalTrans.Txt;
            _custVendPaymProposalTmp.WLKUserId = ledgerJournalTrans.ledgerJournalTable().createdBy;

            select custVendPDCRegister
                        where custVendPDCRegister.LedgerJournalTrans == ledgerJournalTrans.RecId;

            _custVendPaymProposalTmp.PaymReference = ledgerJournalTrans.BankCentralBankPurposeText;
            _custVendPaymProposalTmp.WLKCheckNumber = ledgerJournalTrans.DocumentNum;
            _custVendPaymProposalTmp.BankAccountTableName = ledgerJournalTrans.offsetAccountName();
            _custVendPaymProposalTmp.WLKMaturityDate = ledgerJournalTrans.DocumentDate;
            _custVendPaymProposalTmp.ttsbegin();
            _custVendPaymProposalTmp.selectForUpdate(true);
            _custVendPaymProposalTmp.update();
            _custVendPaymProposalTmp.ttscommit();
            PaymProposalReport::insertCustSettlementTmp(_custVendPaymProposalTmp);
        }
    }
}

Ledger Voucher creation Framework and x++ code to create ledger voucher

 Please click her for MS reference file Below is the out of the box example reference and code. SalesInvoiceJournalPostSubBill_Extension->...