Monday, January 23, 2023

Export SSRS report to SharePoint using X++

internal final class RunnableClass1
{   
    /// /// Runs the class with the specified arguments.
    /// The specified arguments.
    public static void main(Args _args)
    {
        DocuType docuType;
        select firstonly * from docuType
                    where docuType.PrintMgmtDocumentType == PrintMgmtDocumentType::SalesOrderInvoice;

        str URI = docuType.sharePointUrl();
        System.UriBuilder builder = new System.UriBuilder(URI);
        str extId = xUserInfo::getExternalId();
        Microsoft.Dynamics.AX.Framework.FileManagement.SharePointDocumentStorageProvider provider;
        Microsoft.Dynamics.AX.Framework.FileManagement.DocumentLocation documentLocation = new Microsoft.Dynamics.AX.Framework.FileManagement.DocumentLocation();       
        provider = new Microsoft.Dynamics.AX.Framework.FileManagement.SharePointDocumentStorageProvider(docuType.Host, docuType.Site, docuType.FolderPath, extId);
        System.Byte[] reportBytes = new System.Byte[0]();
        System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
        reportBytes = RunnableClass1::renderReportToBinaryArray("100002");
     //   reportBytes = enc.GetBytes("YOUR STRING/TEXT");
        System.IO.MemoryStream memoryStream = new System.IO.MemoryStream(reportBytes);
        //memoryStream = comma.getStream();
        documentLocation = provider.SaveFile(newGuid(), '100002.PDF', System.Web.MimeMapping::GetMimeMapping('100002.PDF'), memoryStream);
    }

    public static System.Byte[] renderReportToBinaryArray(SalesInvoiceId _salesInvoiceId)
    {
        str                     ret;
        CustInvoiceJour         custInvoiceJour;
        System.Byte[] reportBytes;
        select firstonly custInvoiceJour
            where custInvoiceJour.InvoiceId == _salesInvoiceId;

        if (custInvoiceJour)
        {
            str ext = SRSPrintDestinationSettings::findFileNameType(SRSReportFileFormat::PDF, SRSImageFileFormat::BMP);
            PrintMgmtReportFormatName printMgmtReportFormatName = PrintMgmtDocType::construct(PrintMgmtDocumentType::SalesOrderInvoice).getDefaultReportFormat();
                                                                                                        
            SalesInvoiceContract salesInvoiceContract = new SalesInvoiceContract();
            salesInvoiceContract.parmRecordId(custInvoiceJour.RecId);

            SrsReportRunController  srsReportRunController = new SrsReportRunController();
            srsReportRunController.parmReportName(printMgmtReportFormatName);
            srsReportRunController.parmExecutionMode(SysOperationExecutionMode::Synchronous);
            srsReportRunController.parmShowDialog(false);
            srsReportRunController.parmReportContract().parmRdpContract(salesInvoiceContract);
            srsReportRunController.parmReportContract().parmReportExecutionInfo(new SRSReportExecutionInfo());
            srsReportRunController.parmReportContract().parmReportServerConfig(SRSConfiguration::getDefaultServerConfiguration());

            SRSPrintDestinationSettings printerSettings = srsReportRunController.parmReportContract().parmPrintSettings();
            printerSettings.printMediumType(SRSPrintMediumType::File);
            printerSettings.fileFormat(SRSReportFileFormat::PDF);
            printerSettings.parmFileName(custInvoiceJour.InvoiceId + ext);
            printerSettings.overwriteFile(true);
                                               
            SRSReportRunService srsReportRunService = new SrsReportRunService();
            srsReportRunService.getReportDataContract(srsReportRunController.parmReportContract().parmReportName());
            srsReportRunService.preRunReport(srsReportRunController.parmReportContract());
            Map reportParametersMap = srsReportRunService.createParamMapFromContract(srsReportRunController.parmReportContract());
            Microsoft.Dynamics.AX.Framework.Reporting.Shared.ReportingService.ParameterValue[]  parameterValueArray = SrsReportRunUtil::getParameterValueArray(reportParametersMap);

            SRSProxy srsProxy = SRSProxy::constructWithConfiguration(srsReportRunController.parmReportContract().parmReportServerConfig());
                        
             reportBytes = srsproxy.renderReportToByteArray(srsReportRunController.parmreportcontract().parmreportpath(),
                                            parameterValueArray,
                                            printerSettings.fileFormat(),
                                            printerSettings.deviceinfo());

            //if (reportBytes)
            //{
            //    using (System.IO.MemoryStream memoryStream = new System.IO.MemoryStream(reportBytes))
            //    {
            //        ret = System.Convert::ToBase64String(memoryStream.ToArray());
            //    }
            //}
        }

        return reportBytes;
    }

}

pass marked records to another form in D365FO

[DataSource]
    class ActivityLogDetails
    {
        /// <summary>
        ///
        /// </summary>
        public void init()
        {
            super();

            if (element.args() && element.args().caller() && element.args().record())
            {
                this.query().dataSourceTable(tableNum(ActivityLogDetails)).clearDynalinks();
                helper= MultiSelectionHelper::createFromCaller(element.args().caller());
                helper.createQueryRanges(this.query().dataSourceTable(tablenum(ActivityLogDetails)),fieldstr(ActivityLogDetails, RecId));

                if (element.args().menuItemName() == menuItemDisplayStr(MarkSalesActivityLogReserved))
                {
                    activityLogStatus = ActivityLogStatus::Reserved;
                }
                else if (element.args().menuItemName() == menuItemDisplayStr(MarkSalesActivityLogRejected))
                {
                    activityLogStatus = ActivityLogStatus::Rejected;
                }
                else
                {
                    throw error("@SYS22996");
                }
            }
            else
            {
                throw error(Error::missingRecord(tableId2PName(tableNum(ActivityLogDetails))));
            }

            Ok.enabled(false);
        }

    }

Send Email from D365FO

 public void sendMail()
    {
        SysMailerMessageBuilder messageBuilder = new SysMailerMessageBuilder();
        Email   toEmail;
        Email   fromEmail;
         
        try
        {
            FromEmail = "fromemail@xyz.com";
            toEmail   = "toemail@xyz.com";

            messageBuilder.setBody("Hello from D365", false);
            messageBuilder.setSubject("Email Test from D365");
            messageBuilder.addTo(toEmail);
            // Note: not calling setFrom() defaults to the current user for SMTP client, whereas
            // when sent to Outlook any setFrom() value will be ignored since profiles on the client are used
            messageBuilder.setFrom(fromEmail);

            // Open the generated email in the configured client
            // Sends a message interactively, allowing the user to view and modify the message before
            SysMailerFactory::sendNonInteractive(messageBuilder.getMessage());
            // Try using sendNonInteractive method too
        }
        catch (Exception::Error)
        {
            throw error("@SYS33567");
        }
    }

Simple dialog with with field in D365FO

[Control("Button")]
    class BtnMarkAsReserved
    {
        /// <summary>
        ///
        /// </summary>
        public void clicked()
        {
            //Declare dialog variables
            Dialog                  dialog;
            DialogField             fieldfMemo;
            Notes                   notes;
            ;

            dialog = new Dialog("Comment");

            //define fields to show on the dialog
            fieldfMemo = dialog.addField(extendedTypeStr(Notes));
            dialog.run();
           
            if (dialog.closedOk())
            {
                //get values from the dialog fields
                notes = fieldfMemo.value();

                ActivityLogDetails_DIS logDetails;
                MultiSelectionHelper helper = MultiSelectionHelper::construct();

                helper.parmDatasource(ActivityLogDetails_DS);

                logDetails= helper.getFirst();
                while (logDetails.RecId != 0)
                {
                    info(logDetails.ItemID);

                    logDetails= helper.getNext();
                }
            }

            super();
        }

    }

D365FO: Update inventory marking

if (updateMarking)
{
	InventDim inventDim = InventDim::find(currInventTransferLine.inventDimId);
	InventDimParm inventDimParm;
	inventDimParm.initFromInventDim(inventDim);

	InventTransOriginId receiptInventTransOriginId = InventTransOrigin::findByInventTransId(currInventTransferLine.InventTransId).RecId;

	InventTransOriginId issueInventTransOriginId = InventTransOrigin::findByInventTransId(currPurchLine.InventTransId).RecId;

	qty markedQty = InventTransOrigin::updateMarking(
						receiptInventTransOriginId,
						issueInventTransOriginId,
						-currInventTransferLine.QtyTransfer,
						'',
						SortOrder::Ascending,
						false,
						inventDim,
						inventDimParm);

	if (markedQty)
	{
		markedQty = InventTransOrigin::updateMarking(
						issueInventTransOriginId,
						receiptInventTransOriginId,
						currInventTransferLine.QtyTransfer,
						'',
						SortOrder::Ascending,
						false,
						inventDim,
						inventDimParm);
	}
}

update_recordset using queryrun class in D365FO

//intialize the query
Query                  salesActivityLineQuery = new Query();
QueryBuildDataSource    SalesActivityLineDS = salesActivityLineQuery.addDataSource(tableNum(SalesActivityLine));
SalesActivityLineDS.addRange(fieldNum(SalesActivityLine_DIS, RecId)).value(con2Str(conSalesActivityLineRecId));

//Initialize Map and specify the field and field value to be updated
Map fieldSetMap1 = new Map(Types::String, Types::String);
fieldSetMap1.insert(fieldStr(SalesActivityLine_DIS, Status), any2Str(ActivityLineStatus_DIS::Rejected));

//Update
Query::update_recordset(fieldSetMap1, salesActivityLineQuery);

Table browser URL in D365FO

Critical Thinking icon icon by Icons8