18 September 2023

Export data entity or composite data entity using X++ code in D365 F&O

using System;

using System.Globalization;


using System.Net.Http.Headers;

using System.Threading.Tasks;

using Microsoft.WindowsAzure.Storage;

using Microsoft.WindowsAzure.Storage.Blob;

using System.Net.Http;

//using Windows.Web.Http;


internal final class TRGExportEntitySO

{

    public static void main(Args _args)

    {

        TRGExportEntitySO entityExport = new TRGExportEntitySO();

        entityExport.exportEntityData();

    }


    public void exportEntityData()

    {

        DMFDefinitionGroupEntity        definitionGroupEntity,definitionGroupEnt;

        DMFEntity                       dmfEntity,dmfEntityLoc;

        CBBlobHelper                        helper         = new CBBlobHelper();

        #DMF

        Query query;

        DMFEntityName entityName = "Sales Orders Composite v3";

        DMFDefinitionGroupName definitionGroupName = 'SO Export';//'Customer payment journal Exp';

        SharedServiceUnitFileID fileId;

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

        boolean isGenerated = false;


        select firstonly DefinitionGroup,Entity,Source from definitionGroupEnt

            where definitionGroupEnt.DefinitionGroup == definitionGroupName

            join TargetEntity from dmfEntityLoc

            where dmfEntityLoc.EntityName == definitionGroupEnt.Entity

            && dmfEntityLoc.EntityType == DMFEntityTypes::CompositEntity;


        //DMFEntity   dmfEntityLoc = DMFEntity::find(definitionGroupEnt.Entity);


        //entityName = DMFEntity::findFirstByTableId(tableNum(SalesOrderV3Entity)).EntityName;

        // Update query

        query = new query(dmfutil::getDefaultQueryForEntityV3(definitionGroupEnt.Entity,definitionGroupName));

        //query = new query(this.getDefaultQueryForEntityV3(entityName));


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

        sysquery::findorcreaterange(qbds, fieldnum(SalesOrderHeaderV2Entity, SalesOrderNumber)).value("SOCN01-0000056"); // selected record value to be pass here

        

        select firstonly RecId,Entity,DefinitionGroup from definitionGroupEntity

        exists join dmfEntity

            where definitionGroupEntity.DefinitionGroup == definitionGroupName

        && dmfEntity.entityName == definitionGroupEntity.entity

        && dmfEntity.TargetEntity == dmfEntityLoc.TargetEntity;//"SalesOrderV3Entity";


        try

        {

            ttsbegin;

            definitionGroupEntity.reread();

            definitionGroupEntity.selectForUpdate(true);

            definitionGroupEntity.QueryData = query.pack();

            definitionGroupEntity.update();

            ttscommit;


            DMFEntityExporter exporter = new DMFEntityExporter();


            fileId = exporter.exportToFile( definitionGroupEnt.Entity,

                                        definitionGroupName,

                                        "",

                                        definitionGroupEnt.Source,//'CSV-Unicode',

                                        #FieldGroupName_AllFields,

            query.pack(),

                                        curExt(),

                                        null,

                                        true,

                                        false);


            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('%1,%2',definitionGroupEnt.Entity,fileExt);

                System.IO.Stream stream = File::UseFileFromURL(downloadUrl);

                //Send the file to user

                //File::SendFileToUser(stream, filename);

                //filePath 

                //str connectStr =  CBKeyVaultCertificateHelper::getManualSecretValue('https://smartcoredev-a9b3cnb8fph3hae2.z01.azurefd.net/d365?sv=2022-11-02&ss=b&srt=sco&sp=rwdlacyx&se=2024-04-01T18:49:49Z&st=2023-10-12T10:49:49Z&spr=https&sig=xvLiFCCg6JmHjJ1x6i2Eu%2BhEw1DWSetEKD6jZtRWgu4%3D');

                str connenctionString = "https://smartcoredev-a9b3cnb8fph3hae2.z01.azurefd.net/d365/" + filename + "?sv=2022-11-02&ss=b&srt=sco&sp=rwdlacyx&se=2024-04-01T18:49:49Z&st=2023-10-12T10:49:49Z&spr=https&sig=xvLiFCCg6JmHjJ1x6i2Eu%2BhEw1DWSetEKD6jZtRWgu4%3D";


                //str connectStr = "https://smartcoredev-a9b3cnb8fph3hae2.z01.azurefd.net/d365?sv=2022-11-02&ss=b&srt=sco&sp=rwdlacyx&se=2024-04-01T18:49:49Z&st=2023-10-12T10:49:49Z&spr=https&sig=xvLiFCCg6JmHjJ1x6i2Eu%2BhEw1DWSetEKD6jZtRWgu4%3D";


                Uri blobUri = new Uri(connenctionString);


                //try

                //{



                //using(System.Net.Http.HttpClient cl = new System.Net.Http.HttpClient())

                //    {

                //        System.Net.Http.HttpClient clien = new System.Net.Http.HttpClient();

                //        System.Net.Http.StreamContent content = new System.Net.Http.StreamContent(stream);

                //        //content.Headers.Add("x-ms-blob-type", "BlockBlob");

                //        //string mimeType = "text/plain";

                //        //content.Headers.Add("Content-Type", mimeType);


                //        var response = cl.PutAsync(blobUri, content).Result;


                //        if (!response.IsSuccessStatusCode)

                //        {

                //            throw new InvalidPluginExecutionException("Put request wasn't a success.");

                //        }

                //    }

                //}

                //catch (Exception e)

                //{

                //    throw new InvalidPluginExecutionException("Your file upload wasn't successful.");

                //}


                helper.createBlobContainer(connenctionString,'');


                helper.uploadBlobFileByStream("filename", stream);

                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");

        }

    }


}

No comments:

Post a Comment

Give me the commetns and solutions

Ledger Voucher creation Framework and x++ code to create ledger voucher

 Please click her for MS reference file Below is the out of the box example reference and code. SalesInvoiceJournalPostSubBill_Extension->...