Tuesday, February 28, 2023

Data entity Export using X++ in D365 FO

 Export data entity through X++ in D365 FO.




public final class ExportEntity
{
    public static void main(Args _args)
    {
        #dmf
        Query 				query;
        DMFEntityName 			entityName = "Batch groups";
        SharedServiceUnitFileID 	fileId;       

        // Update query
	// query = new Query(DMFUtil::getDefaultQueryForEntity(entityName));
	query = new query(dmfutil::getDefaultQueryForEntityV3(entityname));
		
        querybuilddatasource qbds = query.datasourcetable(tablenum(BatchGroupEntity));
        sysquery::findorcreaterange(qbds, fieldnum(BatchGroupEntity, ServerId)).value("Batch:DEMO");

        // Export file
        DMFDefinitionGroupName definitionGroupName = 'BatchGroupEntityExport';
		
        try
        {
            DMFEntityExporter exporter = new DMFEntityExporter();

            //There are optional parameters also added
            fileId = exporter.exportToFile(
            entityName,            //Entity label
            definitionGroupName,    //Definition group
            '',                    //ExecutionId group
            'CSV',                 // or 'XML-Element',//Source format to export in
            #FieldGroupName_AllFields,//Specify the field group fields to include in export.
            query.pack(),            //Query criteria to export records
            curExt(),//
            null,                //List of XSLT files
            true,                //showErrorMessages
            false);                //showSuccessMessages

            if (fileId != '')
            {
                //Get Azure blob url from guid
                str downloadUrl = DMFDataPopulation::getAzureBlobReadUrl(str2Guid(fileId));

                System.Uri uri = new System.Uri(downloadUrl);
				
                str fileExt;

                if (uri != null)
                {
                    fileExt = System.IO.Path::GetExtension(uri.LocalPath);
                }

                Filename filename = strFmt('CustomerPaymentData%1',fileExt);
                System.IO.Stream stream = File::UseFileFromURL(downloadUrl);
				
                //Send the file to user
                File::SendFileToUser(stream, filename);
                
                // Below code will delete the export group.
                //DMFDefinitionGroup::find(definitionGroupName, true).delete();
            }
            else
            {
                throw error("The file was not generated succefully. See execution log");
            }
        }
        catch
        {
            throw error("DMF execution failed");
        }

    }
}

Ref: Link


Keep Daxing!!


No comments:

Post a Comment