Monday, October 30, 2023

How to fetch values from records to Include filter using x++ in D365FO

  Hi guys, I have a requirement to fetch the record to include filtered values from the report dialog.



I need to show those values in the report footer.

 Code :

Users can apply filters from the filter screen, and if any new tables are joined or new fields are added. To get those values I have used a query filter.



        str                         value;
	Query                       query;
	QueryFilter                 qf;

	query = qr.query();// getting query from query run.

	int  cnt = query.queryFilterCount();

	// Fetching data from query if any new tables are joined or new Fileds are added
	for (int ds =1; ds<= cnt; ds++)
	{
		qf = query.queryFilter(ds);

		if(qf.value())
		{
			value += strFmt('%1 : %2',
				            qf.field(),
					    qf.value());
		}
	}


The below code is to fetch data from the standard query.

        str                         value;
	Query                       query;
	QueryBuildDataSource        qbds;
	QueryBuildRange             qbr;
	int                         totalDS, totalRanges;

	query             = qr.query();// getting query from query run.
	totalDS           = query.dataSourceCount();

	// Used to fectch data from the standard view query 
	for (int ds =1; ds<= totalDS; ds++)
	{
	    qbds            = query.dataSourceNo(ds);
	    totalRanges     = qbds.rangeCount();

	    for (int r =1; r<= totalRanges; r++)
	    {
		qbr =  qbds.range(r);
			
		if (qbr.value())
		{
                    // Way 1 : 
		    value += strFmt('%1 : %2',
			                        qbr.prompt(),
						qbr.value());
									

		    // Way2 :
		    TableId     tableId 	=  qbds.table();//tableName2Id(qbds.AOTname());
		    DictTable   dictTable   = new SysDictTable(tableId);

		    str fieldname = qbr.AOTname();

		    if(qbr.value())
		    {
			MiscParameters += strFmt('%1 : %2  ',
					dictTable.fieldObject(fieldName2Id(tableId, qbr.fieldName())).label(),
					qbr.value());				   
		    }


		    // Way3 :
		    // Direct Field
		    FieldId  fieldId = qbr.field();
		    if(qbr.value())
		    {
			MiscParameters += strFmt('%1 : %2  ',
						        dictTable.fieldObject(fieldId).label(),
							qbr.value());						  
		    }


		    // Way4 :
		    str     value 	      = strFmt('%1', qbr.value());
		    Map     miscParametersMap = new Map(Types::String, Types::String);// Write this line at out side of loop

		    if (value)
		    {
			if (miscParametersMap.exists(qbr.prompt()))
			{
			    container   con = str2con(miscParametersMap.lookup(qbr.prompt()), ',' , false);

			    if (!conFind(con, value))
			    {
				con += value;
			    }

			    miscParametersMap.insert(qbr.prompt(), con2Str(con));
			}
			else
			{
			    miscParametersMap.insert(qbr.prompt(), value);
			}
		    }

		    if (miscParametersMap.elements())
		    {
			MapEnumerator   mapEnumerator = miscParametersMap.getEnumerator();

			while (mapEnumerator.moveNext())
			{
			    value += (strfmt("%1 : %2  ",
						mapEnumerator.currentKey(),
						mapEnumerator.currentValue()));
			}
		    }
		}
	    }
	}




Keep Daxing!!



Thursday, October 26, 2023

Merge multiple pdf’s into a single pdf using x++ in D365FO

 Got the requirement to Merge multiple pdf’s into a single pdf in D365FO.



To achieve this need to download the (pdfsharp)3rd party DLL from the below link 

https://www.dllme.com/dll/files/pdfsharp#dl.

once DLL is downloaded you have to add at the project reference node.


Write the below code.

using PdfSharp;
internal final class PFDRun
{
    public static void main(Args _args)
    { 
	PdfSharp.Pdf.PdfDocument 	outPutPDFDocument = new PdfSharp.Pdf.PdfDocument();
	PdfSharp.Pdf.PdfDocument 	inputPDFDocument  = new PdfSharp.Pdf.PdfDocument(); 
        PdfSharp.Pdf.PdfPages 		pdfPages;

        container con = ['D:\\Test1.pdf','D:\\Test2.pdf', 'D:\\Test3.pdf'];

        int i, j, pageCount;

	FileName 		pdfFile;
        InteropPermission 	permission;
        str 			errorMessage;

        try
        {
            permission = new InteropPermission(InteropKind::ClrInterop);

            permission.assert();

            for (i = 1; i <= 2; i++)
            {
		// I have written custom class to get the file stream from blob.
                System.IO.MemoryStream pdfStream = MyClass::GetStream(i);

                // pdfStream.Seek(0); // Ensure the stream is at the beginning

		// intialize With stream
                inputPDFDocument = PdfSharp.Pdf.IO.PdfReader::Open(pdfStream, PdfSharp.Pdf.IO.PdfDocumentOpenMode::Import);
				
		// intialize With file name(File to be stored in local folder)
		// pdfFile = conpeek(con, i);
                //inputPDFDocument = PdfSharp.Pdf.IO.PdfReader::Open(pdfFile, PdfSharp.Pdf.IO.PdfDocumentOpenMode::Import);

                outputPDFDocument.set_Version(inputPDFDocument.get_Version());

                pageCount = inputPDFDocument.get_PageCount();
                pdfPages  = inputPDFDocument.get_Pages();

                if (pageCount >0)
                {
                    if (pageCount == 1)
                    {
                        outputPDFDocument.AddPage(pdfPages.get_Item(0));
                    }
                    else
                    {
                        for (j = 1 ; j <= pageCount; j++)
                        {
                            outputPDFDocument.AddPage(pdfPages.get_Item(j-1));
                        }
                    }
                }
            }

            System.IO.MemoryStream outPutStream= new System.IO.MemoryStream();

            outputPDFDocument.Close();
            outputPDFDocument.Save('D:\\Output\\mergedFile.pdf'); // To save in local folder.
            outputPDFDocument.Save(outPutStream, false);

            file::SendFileToUser(outPutStream, 'mergedFile.pdf'); // To download

            CodeAccessPermission::revertAssert();
        }
        catch(Exception::CLRError)
        {
            errorMessage = AifUtil::getClrErrorMessage();

            CodeAccessPermission::revertAssert();

            throw error(errorMessage);
        }
    
    }

}