Skip to main content

Notifications

Announcements

No record found.

Finance | Project Operations, Human Resources, ...
Unanswered

Purchase request Workflow

(0) ShareShare
ReportReport
Posted on by 51

Hi Andre i don't know why it delete it my text.
So i had a class after creating a Purchase request and it being approved it sends an email to the user with a report with an email with the purchase ordre this is the code 
[ExtensionOf(ClassStr(PurchAutoCreate_PurchReq))]final class PurchAutoCreate_PurchReq_Class_Extension{    public Email xxgetRequisitionerEmail()    {        Purchreqline    purchReqLine;        HcmWorker       hcmWorker;        Email           email;        DirPersonUser   dirPersonUser;        UserInfo        userInfo;                select purchReqLine            where purchReqLine.purchId == purchTable.purchId;                 dirPersonUser = HcmWorker::findPersonUser(HcmWorker::find(purchReqLine.Requisitioner).RecId);        select firstonly userInfo where userInfo.Id == dirPersonUser.User;        return  userInfo.networkAlias;            }    public void  create()    {        next create();        this.xxSendMail(purchTable.purchId, this.xxgetRequisitionerEmail());    }    void xxSendMail(PurchId    _purchId,                     Email      _toEmail)    {        PurchTable      purchTable = PurchTable::find(_purchId);        Filename fileName = strFmt(/BC%1.pdf/, _purchId);        xxPurchaseOrderReportController controller = new xxPurchaseOrderReportController();        xxPurchaseOrderReportContract contract = new xxPurchaseOrderReportContract();        SRSPrintDestinationSettings settings;        Array arrayFiles;        System.Byte[] reportBytes = new System.Byte[0]();        SRSProxy srsProxy;        SRSReportRunService srsReportRunService = new SrsReportRunService();        Microsoft.Dynamics.AX.Framework.Reporting.Shared.ReportingService.ParameterValue[] parameterValueArray;        Map reportParametersMap;        SRSReportExecutionInfo executionInfo = new SRSReportExecutionInfo();        //set the report contract parameters        contract.parmPurchId(purchTable.PurchId);        //set the report controller paramters        //controller.parmArgs(_args);        //set report name and desing name        controller.parmReportName(ssrsReportStr(xxPurchaseOrderReport, report));        controller.parmShowDialog(false);        controller.parmLoadFromSysLastValue(false);        controller.parmReportContract().parmRdpContract(contract);         // Provide printer settings        settings = controller.parmReportContract().parmPrintSettings();        settings.printMediumType(SRSPrintMediumType::File);        settings.fileName(fileName);        settings.fileFormat(SRSReportFileFormat::PDF);                // Below is a part of code responsible for rendering the report        controller.parmReportContract().parmReportServerConfig(SRSConfiguration::getDefaultServerConfiguration());        controller.parmReportContract().parmReportExecutionInfo(executionInfo);        srsReportRunService.getReportDataContract(controller.parmreportcontract().parmReportName());        srsReportRunService.preRunReport(controller.parmreportcontract());        reportParametersMap = srsReportRunService.createParamMapFromContract(controller.parmReportContract());        parameterValueArray = SrsReportRunUtil::getParameterValueArray(reportParametersMap);        srsProxy = SRSProxy::constructWithConfiguration(controller.parmReportContract().parmReportServerConfig());        // Actual rendering to byte array        reportBytes = srsproxy.renderReportToByteArray(controller.parmreportcontract().parmreportpath(),parameterValueArray,settings.fileFormat(),settings.deviceinfo());        // You can also convert the report Bytes into an xpp BinData object if needed        container binData;        Binary binaryData;        System.IO.MemoryStream mstream = new System.IO.MemoryStream(reportBytes);        binaryData = Binary::constructFromMemoryStream(mstream);        if(binaryData)        {            binData = binaryData.getContainer();        }        System.Byte[] binData1;        System.IO.Stream stream1;        // Turn the Bytes into a stream        for(int i = 0; i < conLen(binData); i++)        {            binData1 = conPeek(binData,i+1);            stream1 = new System.IO.MemoryStream(binData1);        }        //email sending settings        var mail = SysMailerFactory::getNonInteractiveMailer();        var messageBuilder = new SysMailerMessageBuilder();        str body    = strFmt(/%1 </b> %2 <br> / +                                        /%3 </b> %4 <br> / +                                        /%5 </b> <br> / +                                        /%6 </b> <br> /,                                        /@xxLabels:xxSendMail/, /,/,                                        /@xxLabels:xxObject/, _purchId,                                        /@xxLabels:xxAttach/,                                        /@xxLabels:crdt/) ;        messageBuilder.reset().setFrom('Dynamics365@xx.onmicrosoft.com') // From email address        .addTo(_toEmail) // To Email address        .setSubject(strFmt(/@xxLabels:xxNumber/, _purchId, purchTable.vendorName())) // Email Subject        .setBody(body);        //Email Body        if (stream1 != null)        {            //add attachment to the email            messageBuilder.addAttachment(stream1,filename);        }        //send email        mail.sendNonInteractive(messageBuilder.getMessage());    }}
 
  it worked fine but after some changes made on the workflow it started to send emails but with empty reports (with no data).
i debuged it and find out that purchId is not empty but purchTable is empty. i tried with PurchReqWFTypeEventHandler inside the completed methode but it's same thing.
any help please
[ExtensionOf(classStr(PurchReqWFTypeEventHandler))]final class PurchReqWFTypeEventHandler_xx_Extension{    public void completed(WorkflowEventArgs _workflowEventArgs)    {                        PurchReqTable   purchReqTable;        PurchLine       purchLine;        PurchTable      purchTable;        Purchreqline    purchReqLine;        HcmWorker       hcmWorker;        Email           email;        DirPersonUser   dirPersonUser;        UserInfo        userInfo;                next completed(_workflowEventArgs);                        WorkflowContext workflowContext = _workflowEventArgs.parmWorkflowContext();        purchReqTable = PurchReqTable::find(workflowContext.parmRecId());        select purchLine             where purchReqTable.PurchReqId == purchLine.PurchReqId;        purchTable = purchLine.purchTable();                 select purchReqLine            where purchReqLine.purchId == purchTable.purchId;                dirPersonUser = HcmWorker::findPersonUser(HcmWorker::find(purchReqLine.Requisitioner).RecId);        select firstonly userInfo where userInfo.Id == dirPersonUser.User;        //this.xxSendMail(purchTable, userInfo.networkAlias);
 
  • Suggested answer
    André Arnaud de Calavon Profile Picture
    André Arnaud de Cal... 292,187 Super User 2025 Season 1 on at
    Purchase request Workflow
    Hi Wassim,
     
    The purchase requisition tables are global tables and the purchase orders are saved per company. Can you check (debug) if the issue is related to creating purchase orders in another company than where the code is executed? In that case, you would need to use a command to switch the company first and then find the purchase order record.
  • GirishS Profile Picture
    GirishS 27,821 Super User 2024 Season 1 on at
    Purchase request Workflow
    I am confused. You said you are getting PurchId but purchTable is empty.
    How are you getting PurchId?
    Please use different buffer for PurchTable because there is already a buffer with same name in the standard class.
     
    Thanks,
    Girish S.
  • wassim.77 Profile Picture
    wassim.77 51 on at
    Purchase request Workflow
    Hey Girish thank you for your response. i already have tried it, when i get purchTable buffer from  PurchAutoCreate it is also empty.
    void hpsSendMail(PurchTable    _purchTable,
                         Email          _toEmail)
        {
            PurchId         _purchId = _purchTable.PurchId;
            PurchTable      purchTable = _purchTable;//PurchTable::find(_purchId);
    
     
  • GirishS Profile Picture
    GirishS 27,821 Super User 2024 Season 1 on at
    Purchase request Workflow
    Hi wassim,
     
    I think there is already PurchTable buffer declared in the standard "PurchAutoCreate" class.
    You are again initializing PurchTable with same buffer name in the method "xxSendMail".
    You already have purchTable buffer - In that case you can directly pass that instead of passing purchId and adding find method to get purhTable buffer.
     
    Thanks,
    Girish S.
  • wassim.77 Profile Picture
    wassim.77 51 on at
    Purchase request Workflow
    Hi Andre i don't know why it delete it my text.
    So i had a class after creating a Purchase request and it being approved it sends an email to the user with a report with an email with the purchase ordre this is the code 
    
    [ExtensionOf(ClassStr(PurchAutoCreate_PurchReq))]
    final class PurchAutoCreate_PurchReq_Class_Extension
    {
        public Email xxgetRequisitionerEmail()
        {
            Purchreqline    purchReqLine;
            HcmWorker       hcmWorker;
            Email           email;
            DirPersonUser   dirPersonUser;
            UserInfo        userInfo;
            
            select purchReqLine
                where purchReqLine.purchId == purchTable.purchId;
             
            dirPersonUser = HcmWorker::findPersonUser(HcmWorker::find(purchReqLine.Requisitioner).RecId);
            select firstonly userInfo where userInfo.Id == dirPersonUser.User;
    
            return  userInfo.networkAlias;
            
        }
    
        public void  create()
        {
            next create();
            this.xxSendMail(purchTable.purchId, this.xxgetRequisitionerEmail());
        }
    
        void xxSendMail(PurchId    _purchId,
                         Email      _toEmail)
        {
            PurchTable      purchTable = PurchTable::find(_purchId);
    
            Filename fileName = strFmt("BC%1.pdf", _purchId);
    
            xxPurchaseOrderReportController controller = new xxPurchaseOrderReportController();
    
            xxPurchaseOrderReportContract contract = new xxPurchaseOrderReportContract();
    
            SRSPrintDestinationSettings settings;
    
            Array arrayFiles;
    
            System.Byte[] reportBytes = new System.Byte[0]();
    
            SRSProxy srsProxy;
    
            SRSReportRunService srsReportRunService = new SrsReportRunService();
    
            Microsoft.Dynamics.AX.Framework.Reporting.Shared.ReportingService.ParameterValue[] parameterValueArray;
    
            Map reportParametersMap;
    
            SRSReportExecutionInfo executionInfo = new SRSReportExecutionInfo();
    
            //set the report contract parameters
    
            contract.parmPurchId(purchTable.PurchId);
    
            //set the report controller paramters
    
            //controller.parmArgs(_args);
    
            //set report name and desing name
    
            controller.parmReportName(ssrsReportStr(xxPurchaseOrderReport, report));
    
            controller.parmShowDialog(false);
    
            controller.parmLoadFromSysLastValue(false);
    
            controller.parmReportContract().parmRdpContract(contract);
    
     
    
            // Provide printer settings
    
            settings = controller.parmReportContract().parmPrintSettings();
    
            settings.printMediumType(SRSPrintMediumType::File);
    
            settings.fileName(fileName);
    
            settings.fileFormat(SRSReportFileFormat::PDF);
            
            // Below is a part of code responsible for rendering the report
    
            controller.parmReportContract().parmReportServerConfig(SRSConfiguration::getDefaultServerConfiguration());
    
            controller.parmReportContract().parmReportExecutionInfo(executionInfo);
    
            srsReportRunService.getReportDataContract(controller.parmreportcontract().parmReportName());
    
            srsReportRunService.preRunReport(controller.parmreportcontract());
    
            reportParametersMap = srsReportRunService.createParamMapFromContract(controller.parmReportContract());
    
            parameterValueArray = SrsReportRunUtil::getParameterValueArray(reportParametersMap);
    
            srsProxy = SRSProxy::constructWithConfiguration(controller.parmReportContract().parmReportServerConfig());
    
            // Actual rendering to byte array
    
            reportBytes = srsproxy.renderReportToByteArray(controller.parmreportcontract().parmreportpath(),parameterValueArray,settings.fileFormat(),settings.deviceinfo());
    
            // You can also convert the report Bytes into an xpp BinData object if needed
    
            container binData;
    
            Binary binaryData;
    
            System.IO.MemoryStream mstream = new System.IO.MemoryStream(reportBytes);
    
            binaryData = Binary::constructFromMemoryStream(mstream);
    
            if(binaryData)
    
            {
    
                binData = binaryData.getContainer();
    
            }
    
            System.Byte[] binData1;
    
            System.IO.Stream stream1;
    
            // Turn the Bytes into a stream
    
            for(int i = 0; i < conLen(binData); i++)
    
            {
    
                binData1 = conPeek(binData,i+1);
    
                stream1 = new System.IO.MemoryStream(binData1);
    
            }
    
            //email sending settings
    
            var mail = SysMailerFactory::getNonInteractiveMailer();
    
            var messageBuilder = new SysMailerMessageBuilder();
    
            str body		    = strFmt(	"%1 </b> %2 <br> " +
                                            "%3 </b> %4 <br> " +
                                            "%5 </b> <br> " +
                                            "%6 </b> <br> ",
                                            "@xxLabels:xxSendMail", ",",
                                            "@xxLabels:xxObject", _purchId,
                                            "@xxLabels:xxAttach",
                                            "@xxLabels:crdt") ;
    
    
            messageBuilder.reset().setFrom('Dynamics365@xx.onmicrosoft.com') // From email address
    
            .addTo(_toEmail) // To Email address
    
            .setSubject(strFmt("@xxLabels:xxNumber", _purchId, purchTable.vendorName())) // Email Subject
    
            .setBody(body);        //Email Body
    
            if (stream1 != null)
    
            {
                //add attachment to the email
    
                messageBuilder.addAttachment(stream1,filename);
    
            }
    
            //send email
    
            mail.sendNonInteractive(messageBuilder.getMessage());
    
        }
    
    }
      it worked fine but after some changes made on the workflow it started to send emails but with empty reports (with no data).
    i debuged it and find out that purchId is not empty but purchTable is empty. i tried with PurchReqWFTypeEventHandler inside the completed methode but it's same thing.
    any help please
    [ExtensionOf(classStr(PurchReqWFTypeEventHandler))]
    final class PurchReqWFTypeEventHandler_xx_Extension
    {
    
        public void completed(WorkflowEventArgs _workflowEventArgs)
        {
            
            
            PurchReqTable   purchReqTable;
            PurchLine       purchLine;
            PurchTable      purchTable;
            Purchreqline    purchReqLine;
            HcmWorker       hcmWorker;
            Email           email;
            DirPersonUser   dirPersonUser;
            UserInfo        userInfo;
            
    
            next completed(_workflowEventArgs);
            
            
            WorkflowContext workflowContext = _workflowEventArgs.parmWorkflowContext();
            purchReqTable = PurchReqTable::find(workflowContext.parmRecId());
    
            select purchLine 
                where purchReqTable.PurchReqId == purchLine.PurchReqId;
    
            purchTable = purchLine.purchTable();
             
            select purchReqLine
                where purchReqLine.purchId == purchTable.purchId;
            
            dirPersonUser = HcmWorker::findPersonUser(HcmWorker::find(purchReqLine.Requisitioner).RecId);
            select firstonly userInfo where userInfo.Id == dirPersonUser.User;
    
            //this.xxSendMail(purchTable, userInfo.networkAlias);
     
  • André Arnaud de Calavon Profile Picture
    André Arnaud de Cal... 292,187 Super User 2025 Season 1 on at
    Purchase request Workflow
    Hi Wassim,
     
    I assume you forgot to enter some text to ask your question. Can you update your post, so we would know with what question to help you? Or was it posted by accident and should the question be deleted?

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Vahid Ghafarpour – Community Spotlight

We are excited to recognize Vahid Ghafarpour as our February 2025 Community…

Congratulations to the January Top 10 leaders!

Check out the January community rock stars...

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,187 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 230,966 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans