Thursday, 14 April 2022

Export file Azure blob using data entity in d365 f&o

 public void exportFileInAzureBlobUsingEntity()

 {

#DMF

SharedServiceUnitFileID fileId;

DMFDefinitionGroupName definitionGroupName = 'DefinationGroupName'

try

{

DMFEntityName               entityName              = DMFEntity::findFirstByTableId(tableNum(‘TargetEntityName’)).EntityName;

DMFDefinitionGroupEntity    definitionGroupEntity   = DMFDefinitionGroupEntity::find(definitionGroupName, entityName, true);

ttsbegin;

Query                query = new Query(DMFUtil::getDefaultQueryForEntityV3(entityName));

QueryBuildDataSource qbds = query.dataSourceTable(tableNum(‘TargetEntityName’));

//If need to pass a range below is required             

definitionGroupEntity.resetQuery();

SysQuery::findOrCreateRange(qbds, fieldNum('TargetEntityName', 'fieldName')).value(queryValue(''));

SysQuery::findOrCreateRange(qbds, fieldNum('TargetEntityName', 'fieldName')).value(queryValue(''));

if (definitionGroupEntity) // Update the query range 

{

QueryRun    queryRun    = new QueryRun(query);

container   queryData   = queryRun.pack();

definitionGroupEntity.QueryData = queryData;

definitionGroupEntity.update();

}

ttscommit;

DMFEntityExporter exporter = new DMFEntityExporter();

fileId = exporter.exportToFile(entityName,

definitionGroupName,

'', //Optional: ExecutionID

‘FileFormat’, //Optional::File Format

#FieldGroupName_AllFields, //Optional field selection

query.pack(),

curExt());

if (fileId != '')

{

str downloadUrl = DMFDataPopulation::getAzureBlobReadUrl(str2Guid(fileId));               

Filename filename = ‘FileName’

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

//Upload in Azure blob 

str azureStorageAccount = ‘Azure storage account name’;

str azureStorageKey = ‘Azure storage account key’;

Microsoft.WindowsAzure.Storage.Auth.StorageCredentials storageCredentials = new Microsoft.WindowsAzure.Storage.Auth.StorageCredentials(azureStorageAccount, azureStorageKey);

Microsoft.WindowsAzure.Storage.CloudStorageAccount storageAccount = new Microsoft.WindowsAzure.Storage.CloudStorageAccount(storageCredentials, true);

var blobcli = storageAccount.CreateCloudBlobClient();

Microsoft.WindowsAzure.Storage.Blob.CloudBlobContainer cont = blobcli.GetContainerReference('Export container folder path Ex: Container/folder);

CloudBlockBlob cloudBlockBlob = cont.GetBlockBlobReference(fileName);

cloudBlockBlob.UploadFromStream(stream, null, null, null);

}

else

{

throw error("");

}

}

catch

{

error("");

}

}

No comments:

Post a Comment

DefaultDimension in d365 FO X++

 Navigate to PurchTable methods you can find with DefaultDimension this.DefaultDimension = this.mergeDimension(this.getDefaultDimension(),th...