Thursday, February 16, 2023

Trigger business event on workflow approval process in D365FO

 Trigger business event for the workflow approval process in D365FO.

Got a requirement to do the approval process without clicking the Approve button. upon user submission, the record transitions to a 'Submitted' state and automatically triggers a business event without the need for manual approval. The approver will receive the email notification to Approve.


Way 1: Using WorkflowWorkItemTable

[ExtensionOf(tableStr(WorkflowWorkItemTable))]
final class WorkflowWorkItemTable_Extension
{  
    public void insert()
    {
        PurchTable              purchTable;
        WorkflowWorkItemTable   workflowStatusLoc;
        WorkflowElementTable    workflowElementTable;

        next insert();

        select firstonly workflowElementTable
            where workflowElementTable.ElementId == this.ElementId;

        if (workflowElementTable && !strContains(strUpr(workflowElementTable.Name),"REVIEW"))
        {
            if (this.CompanyId)
            {     changecompany(this.CompanyId)     {     select firstonly PurchId from purchTable where purchTable.RecId == this.RefRecId && purchTable.TableId == this.RefTableId;     if(purchTable.PurchId)     {     //BusinessEvent class code     }                 } }             else             {                 PurchReqTable purchReqTable;// Global table
                select firstonly PurchReqId from purchReqTable
                 where purchReqTable.RecId == this.RefRecId
                  && purchReqTable.TableId == this.RefTableId;
    if(purchReqTable.PurchReqId)
    {                         //BusinessEvent class code     }             } } }     // While Reassign this method will trigger public void update() { PurchTable purchTable; WorkflowWorkItemTable workflowStatusLoc; WorkflowElementTable workflowElementTable; next update(); if(this.Status == WorkflowWorkItemStatus::Pending) { select firstonly workflowElementTable where workflowElementTable.ElementId == this.ElementId; if(workflowElementTable && !strContains(strUpr(workflowElementTable.Name),"REVIEW")) { select firstonly PurchId from purchTable where purchTable.RecId == this.RefRecId && purchTable.TableId == this.RefTableId; if(purchTable.PurchId) { //BusinessEvent class code } } } }


Way 2: Using WorkflowTrackingTable

    // It will trigger while Once Approve process starts. 
    [DataEventHandler(tableStr(WorkflowTrackingTable), DataEventType::Inserted)]
    public static void WorkflowTrackingTable_onInserted(Common sender, DataEventArgs e)
    {
        WorkflowTrackingTable   workflowTrackingTable = sender;

        if (workflowTrackingTable.TrackingContext == WorkflowTrackingContext::Approval 
                && workflowTrackingTable.TrackingType == WorkflowTrackingType::Creation)
        {
            WorkflowTrackingStatusTable        workflowTrackingStatusTable;

            select workflowTrackingStatusTable 
                where workflowTrackingStatusTable.RecId == workflowTrackingTable.WorkflowTrackingStatusTable;

            PurchTable              purchTable;

            select firstonly PurchId from purchTable
                where   purchTable.RecId    == workflowTrackingStatusTable.ContextRecId
                &&      purchTable.TableId  == workflowTrackingStatusTable.ContextTableId;

            if(purchTable.PurchId)
            {
                info ('Triggered');
            }
        }
    }



Keep Daxing!!






4 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. Hi Kishore, day by day I read your blog it's really a massive. I have question your declared purchReqTable table but you never selected, but you checked( if condition only )

    ReplyDelete
    Replies
    1. Hi, Sorry. As my requirement I need to check purchReqTable also. But in this post I didn't add that select statement.

      Delete
    2. Thanks for reading my blog. Just now I have update the code please check.

      Delete