web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Answered

You cannot request a change, because the document is in state Draft. when auto submit the workflow in ax 2012 r3

(0) ShareShare
ReportReport
Posted on by 56

hi Team,

I have wrote code for PO auto Submit

PurchLine   purchOrderLine;

    select purchOrderLine
        where purchOrderLine.PurchId == _purchTable.PurchId
        &&    purchOrderLine.PurchPrice == 0;

    if(!purchOrderLine.RecId && _purchTable.ChangeRequestRequired == NoYes::Yes)
    {
        VersioningPurchaseOrder::newPurchaseOrder(_purchTable).createChangeRequest();
        //_purchTable.ChangeRequestRequired = NoYes::No;
        //_purchTable.doUpdate();

        _purchTable.submitToWorkflow(workFlowTypeStr(PurchTableTemplate),
                                    strFmt("Auto submit by %1",curUserId()),
                                    false);

VersioningPurchaseOrder::newPurchaseOrder(_purchTable).createChangeRequest();

When I used the above code then got below error

0654.E1.jpg

2

PurchLine   purchOrderLine;

    select purchOrderLine
        where purchOrderLine.PurchId == _purchTable.PurchId
        &&    purchOrderLine.PurchPrice == 0;

    if(!purchOrderLine.RecId && _purchTable.ChangeRequestRequired == NoYes::Yes)
    {
        //VersioningPurchaseOrder::newPurchaseOrder(_purchTable).createChangeRequest();
        _purchTable.ChangeRequestRequired = NoYes::No;
        _purchTable.doUpdate();

        _purchTable.submitToWorkflow(workFlowTypeStr(PurchTableTemplate),
                                    strFmt("Auto submit by %1",curUserId()),
                                    false);
    }

If i used,

_purchTable.ChangeRequestRequired = NoYes::No;
_purchTable.doUpdate(); 

*** instead of VersioningPurchaseOrder::newPurchaseOrder(_purchTable).createChangeRequest();

then it is successfully submitted.

Can you please let me know is there any difference between both code  ??

VersioningPurchaseOrder::newPurchaseOrder(_purchTable).createChangeRequest();  

_purchTable.ChangeRequestRequired = NoYes::No;

Please give me more shewd on this.

thanks!

I have the same question (0)
  • Suggested answer
    greengrimms Profile Picture
    1,400 on at

    Hi,

    That's happening because of this piece of code:

    pastedimage1627048112447v1.png

    So when you set the ChangeRequestRequired field to No, you're skipping that validation:

    pastedimage1627048158871v2.png

    Also, from the functional side, the PO must be confirmed first in order to be sent to workflow (at least that's how it works in our business scenario)

  • @rp@n Profile Picture
    56 on at

    hi Danilo,

    thanks for prompt response

    Can you please let me know how i will achieve this with the code

    VersioningPurchaseOrder::newPurchaseOrder(_purchTable).createChangeRequest();

    please give me more shed on this

    thanks!

  • Suggested answer
    greengrimms Profile Picture
    1,400 on at

    You could replace the createChangeRequest() code for this:

    if (purchTable.DocumentState == VersioningDocumentState::Draft)
    {
        // Versioning: approve change request
        versioningPurchaseOrder = VersioningPurchaseOrder::newPurchaseOrder(purchTable);
        versioningPurchaseOrder.submitChangeRequest();
        versioningPurchaseOrder.approveChangeRequest();
    }

  • @rp@n Profile Picture
    56 on at

    Thanks a lot

    PurchLine               purchOrderLine;
        VersioningPurchaseOrder versioningPurchaseOrder;
    
        select purchOrderLine
            where purchOrderLine.PurchId == _purchTable.PurchId
            &&    purchOrderLine.PurchPrice == 0;
    
        if(!purchOrderLine.RecId && _purchTable.ChangeRequestRequired == NoYes::Yes
        && _purchTable.DocumentState == VersioningDocumentState::Draft)
        {
                //VersioningPurchaseOrder::newPurchaseOrder(_purchTable).createChangeRequest();
    
                // Versioning: approve change request
                versioningPurchaseOrder = VersioningPurchaseOrder::newPurchaseOrder(_purchTable);
                versioningPurchaseOrder.submitChangeRequest();
                versioningPurchaseOrder.approveChangeRequest();
    
            _purchTable.submitToWorkflow(workFlowTypeStr(PurchTableTemplate),
                                        strFmt("Auto submit by %1",curUserId()),
                                        false);
        }

    Is it correct now?

    Please give me more shed on this

    thanks!

  • @rp@n Profile Picture
    56 on at

    Hi Danilo,

    Now I am getting this error

    6646.e2.jpg

    Please give me more shed on this.

    thanks!

  • @rp@n Profile Picture
    56 on at

    Hi Danilo,

    The PO status is now DRAFT then why the below code executed and getting  error 

    public void submitChangeRequest()
    {
        ttsbegin;
    
        if (this.getDocumentState() != VersioningDocumentState::Draft)
        {
            // Only a document in state Draft can be submitted for approval.
            throw error("@SYS326422");
        }
        this.setDocumentState(VersioningDocumentState::InReview);
    
        ttscommit;
    }

    Even i observed, the code is not go inside if loop. it directly come to this.setDocumentState(VersioningDocumentState::InReview);

    but still getting the error...

    please give me more shed on this

    thanks!

  • @rp@n Profile Picture
    56 on at

    Hi Danilo,

    I found the cause , why this error is coming

    In method - SubmitToWorkflow()

    public void  submitToWorkflow(WorkflowTypeName	     _workflowTemplateName,
                                  WorkflowComment       _workflowComment,
                                  boolean               _activatingFromWeb = false)
    {
        ttsbegin;
    
        Workflow::activateFromWorkflowType(_workflowTemplateName,
                                               this.RecId,
                                               _workflowComment,
                                               _activatingFromWeb,
                                               curUserId());
    
        VersioningPurchaseOrder::newPurchaseOrder(this).submitChangeRequest();
    
        ttscommit;
    }

    Again it is called the class

    VersioningPurchaseOrder::newPurchaseOrder(this).submitChangeRequest();

    Now it is getting DocumentState = INREVIEW

    Due to this I am getting the error

    Please let me know how i will resolve this.?

  • Verified answer
    greengrimms Profile Picture
    1,400 on at

    So since you're requesting the document state change on the workflow submition method, it should work if you just keep this:

    PurchLine               purchOrderLine;
        VersioningPurchaseOrder versioningPurchaseOrder;
    
        select purchOrderLine
            where purchOrderLine.PurchId == _purchTable.PurchId
            &&    purchOrderLine.PurchPrice == 0;
    
        if(!purchOrderLine.RecId && _purchTable.ChangeRequestRequired == NoYes::Yes
        && _purchTable.DocumentState == VersioningDocumentState::Draft)
        {
            _purchTable.submitToWorkflow(workFlowTypeStr(PurchTableTemplate),
                                        strFmt("Auto submit by %1",curUserId()),
                                        false);
        }

    Have you already try this?

    I'm away from my machine right now so I'm unable to test this, but I think it should work given what we know already.

  • @rp@n Profile Picture
    56 on at

    Hi Danilo,

    Now it is working fine. Thanks a lot.

    Can you please let me know one more thing. 

    How I will identified, which user submitting the workflow?

    I passed as a parameter curUserId (). As workflow is running in batch. Shall I identified the user ,who submitting the workflow ?

    Please give me more shed on this 

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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Meet the Microsoft Dynamics 365 Contact Center Champions

We are thrilled to have these Champions in our Community!

Congratulations to the March Top 10 Community Leaders

These are the community rock stars!

Leaderboard > Finance | Project Operations, Human Resources, AX, GP, SL

#1
Giorgio Bonacorsi Profile Picture

Giorgio Bonacorsi 689

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 606 Super User 2026 Season 1

#3
CP04-islander Profile Picture

CP04-islander 356

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans