Exporting the data from data entities through X++ code

As we all are aware of importing/exporting data from standard data entities using Data management Framework(DMF). Similarly we have an option of doing the same through code also.

Below is the code snippet for exporting the data from standard entity and placing the file in Azure blob storage.

public static void main(Args _args)
    {
        #DMF
        Query						  query;
        DMFEntityName				  entityName = "Inventory movement journal headers and lines";
        SharedServiceUnitFileID		  fileId;
        List						  xsltFileList = new List(Types::String);
        boolean						  isGenerated = false;
        VLSAzureSMBFileStorageHandler azureFileStorageProcessor;
        CustParameters				  custParameters = CustParameters::find();
        
        // Update query
        query = new query(dmfutil::getdefaultqueryforentity(entityname));
        querybuilddatasource qbds = query.datasourcetable(tableNum(InventInventoryMovementJournalEntryEntity));

        // Export file
		// Definition group will be created if it is not existed
        DMFDefinitionGroupName definitionGroupName = 'Invent journal entity';

        try
        {
            DMFEntityExporter exporter = new DMFEntityExporter();

            //There are optional parameters also added
            fileId = exporter.exportToFile(
            entityName,//Entity label
            definitionGroupName,//Definition group to reuse
            '',//ExecutionId group to reuse,
            'CSV',//Source format to export in
            'VLSInventJournal',//Specify the field group fields to include in export.
            query.pack(),//Query criteria to export records
            curExt(),//Default 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;
                //Get file extension
                if (uri != null)
                {
                    fileExt = System.IO.Path::GetExtension(uri.LocalPath);
                }

                Filename filename = strFmt('InventJournal%1',fileExt);
                System.IO.Stream stream = File::UseFileFromURL(downloadUrl);
                //Send the file to user
                //File::SendFileToUser(stream, filename);

                DMFDefinitionGroup::find(definitionGroupName, true).delete();

                isGenerated = true;

                azureFileStorageProcessor   = new VLSAzureSMBFileStorageHandler();
                azureFileStorageProcessor.parmAccountName(custParameters.VLSOutboundAccountName);
                azureFileStorageProcessor.parmAccountKey(custParameters.VLSOutboundAccessKey);
                azureFileStorageProcessor.parmFileShareName(custParameters.VLSOutboundFileShareName);
                azureFileStorageProcessor.parmDestinationFileShareName(custParameters.VLSOutboundFileShareName);
                azureFileStorageProcessor.parmErrorFileShareName(custParameters.VLSOutboundFileShareName);
                azureFileStorageProcessor.openConnection();
                azureFileStorageProcessor.resetToRootDirectory();
                azureFileStorageProcessor.setDirectory(custParameters.VLSOutboundInProcess);

                azureFileStorageProcessor.uploadFromStream(stream, filename);
                info(strFmt("File %1 has been moved to Azure storage folder", filename));
			 }
            else
            {
                throw error("The file was not generated succefully.");
            }
        }
        catch
        {
            
            throw error("Data export execution failed.");
        }
    }

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s