Monday, December 12, 2022

D365FO IDisposable context class

Scenario: pass additional parameter to a method that doesnt have a parametr signature 
After the execution of using code block , Dispose method will be called.

IDisposible is a singleton instance of a class per session, for example if you open two different tabs and run the code you will get two different instance of the class
class DocTypeFile_CUSTOM_Context implements System.IDisposable
{
    static DocTypeFile_CUSTOM_Context  docTypeFileContext;

    public  boolean overrideDefaultDocType = false;
    public boolean printSalesInvoiceReport;
    public boolean printSalesConfReport;
   // public PrintMgmtDocumentType printMgmtDocumentType;
   public DocuTypeId doucTypeId = strMin();
  
    public void new()
    {
        if(docTypeFileContext)
        {
            throw error("NestingIsNotSupported");
        }
        docTypeFileContext = this;
    }

    public void dispose()
    {
        docTypeFileContext = null;
    }

    static public DocTypeFile_CUSTOM_Context current()
    {
        return  docTypeFileContext;
    }

}
/// <summary>
///     To handle the report design to print based on caller
/// </summary>
[ExtensionOf(classStr(SalesInvoiceController))]
final class SalesInvoiceController_CUSTOM_Extension
{
    /// <summary>
    ///     choose report design
    /// </summary>
    protected void outputReport()
    {
		//initalize disposible context class 
        using (var docTypeFileContext = new DocTypeFile_CUSTOM_Context())
        {
            SRSPrintDestinationSettings srsPrintDestinationSettings = formLetterReport.getCurrentPrintSetting().parmPrintJobSettings();
           
            if (srsPrintDestinationSettings.printMediumType() == SRSPrintMediumType::File)
            {
			   docTypeFileContext.overrideDefaultDocType = true;
               docTypeFileContext.doucTypeId = "File";
            }

            next outputReport();
        }
    }

}
[ExtensionOf(tableStr(DocuType))]
final class DocuType_CUSTOM_Extension
{
    static DocuTypeId typeFile()
    {
        DocuTypeId docuTypeId;
        docuTypeId = next typeFile();

        DocTypeFile_CUSTOM_Context  docTypeFileContext = DocTypeFile_CUSTOM_Context::current();

        if(docTypeFileContext && docTypeFileContext.overrideDefaultDocType)
        {
            docuTypeId = docTypeFileContext.doucTypeId;
        }

        

        return docuTypeId;
    }

}

No comments:

Post a Comment

Table browser URL in D365FO

Critical Thinking icon icon by Icons8