top of page

ER in memory!

In the following link you can consult the official Microsoft documentation on how to develop a report using the Electronic Reports functionality:



But what if we want to send this report to some external service? Can we get the content of the report in a variable?

The answer is YES!!

I leave you a code fragment that launches a GER report obtaining the content in a ByteArray (byteER) variable:



using Microsoft.Dynamics365.LocalizationFramework;
/// <summary>
/// The electronic reporting service class for a custom ER report
/// </summary>
class CustomErReportService extends SysOperationServiceBase
{
    public const str ERModelDataSourceName = 'model';
    public const str DefaultExportedFileName = 'Custom report';
    public const str ParametersDataSourceName = 'RunTimeParameters';

    /// <summary>
    /// Generates report by using Electronic reporting framework
    /// </summary>
    /// <param name = "_contract">The Custom report contract</param>
    public void generateReportByGER(CustomErReportContract _contract)
    {
        ERFormatMappingId formatMappingId;
        CustomErReportDP  dataProvider;
        dataProvider = CustomErReportDP::construct();
        formatMappingId = _contract.parmFormatMapping();
        if (formatMappingId)
        {
            try
            {
                ERIModelDefinitionParamsAction parameters = new             ERModelDefinitionParamsUIActionComposite()
                    .add(new ERModelDefinitionObjectParameterAction(ERModelDataSourceName, ParametersDataSourceName, _contract, true));

                // Call ER to generate the report.
                ERIFormatMappingRun formatMappingRun = ERObjectsFactory::createFormatMappingRunByFormatMappingId(formatMappingId, DefaultExportedFileName);
                runner.parmShowPromptDialog(false);
                runner.withShowDestinationTab(false);

                ERFileDestinationMemory fileDestinationMemory = new ERFileDestinationMemory();
                runner.withFileDestination(fileDestinationMemory);

                str stream = runner.withParameter(parameters).run();
                System.Byte[] byteER = fileDestinationMemory.GetByteArray();
                }
            }
            catch
            {
                // An error occurred while exporting data.
                error("@SYP4861341");
            }
        }
        else
        {
            // There is no data available.
            info("@SYS300117");
        }
    }
}


Comments


bottom of page