Thursday, May 18, 2023

How to export/import purchase order lines through button in D365 using x++

 Class 1 : Export Class :

internal final class DaxExportClass

{

    public static void main(Args args)

    {

        #DMF

        Query query;

        DMFEntityName entityName = "Purchase order lines V2";

        DMFDefinitionGroupName definitionGroupName = 'purchase order export';

          SharedServiceUnitFileID fileId;

        List xsltFileList = new List(Types::String);

        boolean isGenerated = false;

        PurchLine   purchLine = args.record();

               // Update

 query        query = new query(dmfutil::getDefaultQueryForEntityV3(entityName,definitionGroupName));

        querybuilddatasource qbds = query.datasourcetable(tablenum(PurchPurchaseOrderLineV2Entity));

        sysquery::findorcreaterange(qbds, fieldnum(PurchPurchaseOrderLineV2Entity, PurchaseOrderNumber)).value(purchLine.PurchId);

        // Export file        // Definition group will be created if it is not existed

        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,

            'EXCEL',//Source format to export in

            #FieldGroupName_AllFields,//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('PurchaseOrderlines%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;

            }

            else

            {

                throw error("The file was not generated succefully. See execution log");

            }

        }

        catch

        {

                        throw error("DMF execution failed and details were written to the execution log");

        }

      }

  }

Class 2 : Import Class :

internal final class DaxImportClass

{

    private static DMFDefinitionGroup findDMFDefinitionGroup()

    {

        DMFDefinitionGroup definitionGroup;

        select firstonly definitionGroup

            where definitionGroup.DefinitionGroupName == 'Purchase order lines'; //DMF import data project

        return definitionGroup;

    }

      private static DMFDefinitionGroupEntity findDMFDefinitionGroupEntity(DMFDefinitionGroup _definitionGroup)

    {

        DMFDefinitionGroupEntity definitionGroupEntity;

        DMFEntity dmfEntity;

          select firstonly RecId, Entity from definitionGroupEntity 

exists join dmfEntity

            where definitionGroupEntity.DefinitionGroup == _definitionGroup.DefinitionGroupName

                && dmfEntity.EntityName == definitionGroupEntity.Entity

                && dmfEntity.TargetEntity == dataentityviewstr(PurchPurchaseOrderLineV2Entity);

          if (!definitionGroupEntity)

        {

            throw error(strFmt("@DMF:DMFNoEntityExists", _definitionGroup.DefinitionGroupName));

        }

        return definitionGroupEntity;

    }

      private static DMFLocalFilePath applyTransforms(SharedServiceUnitFileID _uploadedStatement,                                                                                             DMFDefinitionGroup definitionGroup)

    {

        DMFDefinitionGroupEntity    definitionGroupEntity =                             DaxImportClass::findDMFDefinitionGroupEntity(definitionGroup);

        DMFExecutionId              executionId =                                     DMFUtil::setupNewExecution(definitionGroup.DefinitionGroupName);

        DMFDefinitionGroupExecution execution = DMFDefinitionGroupExecution::find(

            definitionGroup.DefinitionGroupName,

            definitionGroupEntity.Entity,

            executionId,

            true);

        execution.IsTransformed = NoYes::No;

        DMFLocalFilePath filePath = execution.applyTransforms(_uploadedStatement);

        DMFExecution e = DMFExecution::find(executionId, true);

        e.delete();

        return filePath;

    }

      public static void main(Args _args)

    {

        SharedServiceUnitFileID fileId;

        //Get the file from user

        FileUploadTemporaryStorageResult result = File::GetFileFromUser() as FileUploadTemporaryStorageResult;

        if (result && result.getUploadStatus())

        {

            fileId = result.getFileId();

            DMFDefinitionGroup          definitionGroup = DaxImportClass::findDMFDefinitionGroup();

            DaxImportClass::applyTransforms(fileId, definitionGroup);

            DMFDefinitionGroupEntity    definitionGroupEntity = DaxImportClass::findDMFDefinitionGroupEntity(definitionGroup);

            DMFExecutionId              executionId = DMFUtil::setupNewExecution(definitionGroup.DefinitionGroupName);

            // Find execution

            DMFDefinitionGroupExecution execution = DMFDefinitionGroupExecution::find(definitionGroup.DefinitionGroupName, definitionGroupEntity.Entity,

                                                                                        executionId, true);

            execution.FilePath = fileId;

            execution.IsTransformed = NoYes::Yes;

            execution.update();

            // Import the file via quick import DMF

            DMFQuickImportExport::doPGImport(definitionGroup.DefinitionGroupName, executionId, true);

            //deletes file

            result.deleteResult();

        }

    }

}

Note : You need to create projects in DMF.






No comments:

Post a Comment