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

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics AX (Archived)

Ledger Journal workflow approval via X++

(0) ShareShare
ReportReport
Posted on by 127

Hi,

I was trying to approve  Ledger Journal workflow item using this X++ code.

static void ApproveWorkflow(Args _args)
{
    LedgerJournalId ledgerJournalId = 'GL-0000024';

    LedgerJournalTable ledgerEntries = LedgerJournalTable::find (ledgerJournalId);

    WorkflowWorkItemTable workflowWorkItemTable;

   while select workflowWorkItemTable where
        (workflowWorkItemTable.Type == WorkflowWorkItemType::WorkItem) &&
        (workflowWorkItemTable.Status == WorkflowWorkItemStatus::Pending) &&
        workflowWorkItemTable.RefRecId == ledgerEntries.RecId &&
        workflowWorkItemTable.RefTableId == ledgerEntries.TableId
    {
     
      WorkflowWorkItemActionManager::dispatchWorkItemAction(
                            workflowWorkItemTable,
                            "Approved via X++",
                            curUserId(),
                            WorkflowWorkItemActionType::Complete,
                            "LedgerJournalTable5",
                            false);
    }
}


I always encountered this error when running this.

"Element outcome  is not defined on the workflow element."

Can someone help the right way of approving workflow using X++. Specially approving multiple workflow items.

Thanks,

*This post is locked for comments

I have the same question (0)
  • André Arnaud de Calavon Profile Picture
    300,911 Super User 2025 Season 2 on at

    Hi,

    Why do you want to have a workflow approved by x++? what would be the purpose? Would an automatic approval setup be a solution for you? What do you mean with multiple workflow items? Is it within one workflow instance or multiple?

    If it is multiple, why do you have a workflow setup? The workflow is designed to have an approval per document. If you automate things, no one will check the contents and details anymore and the approval is worthless.

  • JazZ0003 Profile Picture
    127 on at

    Hi,

    We are still in the process of AX Implementation in our company.

    All those workflow items that i need to approve are old transactions done with our old ERP system. There are atleast 1000 old RFP that need to be posted,  thus it must be approve immediately.  

    We are encoding the latest and old transactions in AX at the same time. So I cannot disable or automate the approving process  as it could also affect our latest transactions.

    The only way I think to accomplish this is through x++.  I just have to identify what are those old RFP and let X++ approve them.

    Tanks,

  • Verified answer
    André Arnaud de Calavon Profile Picture
    300,911 Super User 2025 Season 2 on at

    Hi Jazz,

    I understand the situation now. I have not done this before, so I can't judge on the code you are using.

    There might be another way to achieve the requirement. You can delete the workflow instances running and set the journals to be approved by x++.

    For deleting the workflow instances you can get some help with this blog: colinmitchell.net/.../tutorial-deleting-a-workflow-instance-in-microsoft-dynamics-ax-2012

    The field WorkflowApprovalStatus on the LedgerJournalTable can be set to approved using a job.

    Please test this before applying it in your life environment.

  • Suggested answer
    Jonathan  Halland Profile Picture
    11,310 on at

    Hi Jazz.

    you may want to try using the workflowworkitem.UserId instead of curUserId() in you code. Otherwise the system may try and "approve" items as the incorrect user causing it to fail.

  • JazZ0003 Profile Picture
    127 on at

    Hi Jonathan,

    I tried using the workflowworkitem.UserId but still the same error.

    I think the problem is on the WorkflowWorkItemActionType::Complete. Because i tried to change it to WorkflowWorkItemActionType::Delegate and I was able to delegate the workflow.  I think it cannot recognize the "Complete" action type and there was no "Approve" action type also.

    @Andre,

    I will give feedback once i implemented the link that you have given in our Test environment.

    Thanks,

  • Suggested answer
    Jonathan  Halland Profile Picture
    11,310 on at

    Hi Jazz.

    I Think the issues is that the "approval" menuitem you are specifying is not the Approval Menuitem specified in the AOT for this approval. 

    The standard menuitem for Daily Journal approval is "LedgerWFApprApprove" where as you are using "LedgerJournalTable5"

  • JazZ0003 Profile Picture
    127 on at

    Hi Jonathan,

    Thank you for helping, it did not work though.

    WorkflowWorkItemActionManager::dispatchWorkItemAction(
                                workflowWorkItemTable,
                                "Approved via X++",
                                workflowWorkItemTable.UserId,
                                WorkflowWorkItemActionType::Complete,
                                "LedgerWFApprApprove",
                                false);

    I got the Menu Item after checking the workflowworkitemtable and found it in the column MENUITEMNAME.

    Thanks,

  • Jonathan  Halland Profile Picture
    11,310 on at

    Hi Jazz.

    Everything you are doing now seems mostly correct. You don't really need to send the userId through if you are not delegating or returning though.

    You mentioned these workitems are from an old ERP system. Its quite possible that your datamigration has data inconsistencies and is not pointing to valid workflow configurations.

    I think in your circumstances Andre is correct that you should rather try fix these items manually and approve them via code.

  • JazZ0003 Profile Picture
    127 on at

    Hi Jonathan Halland

    Thank you for the help.

    @ Andre Arnaud de Calavon 

    Thank you for the link you provided. To make it safer, I run the class in Jobs and modify it to add all the RFP that needs approval. 

    Just approved  1000+ old RFP in minutes. This really made my job easier.

    Thank you very much. 

  • Muhammad Anas Ayub Profile Picture
    105 on at

    Dear All,

    Mention link is not working now , i am facing same issue 

    colinmitchell.net/.../tutorial-deleting-a-workflow-instance-in-microsoft-dynamics-ax-2012

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics AX (Archived)

#1
Martin Dráb Profile Picture

Martin Dráb 4 Most Valuable Professional

#1
Priya_K Profile Picture

Priya_K 4

#3
MyDynamicsNAV Profile Picture

MyDynamicsNAV 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans