Loading...

Play around with D365FO workflow .. Some code samples to

Play around with D365FO workflow ..  Some code samples to

 1. Current workflow User a  by code 

public WorkflowUser currentWFUser(Common _common)

    {

        WorkflowTrackingStatusTable workflowTrackingStatusTable;

        WorkflowTrackingTable       workflowTrackingTable; 

        

        select firstonly RecId from workflowTrackingStatusTable

            join User from workflowTrackingTable

            order by WorkflowTrackingTable.CreatedDateTime desc

            where workflowTrackingTable.WorkflowTrackingStatusTable == workflowTrackingStatusTable.RecId

            && workflowTrackingStatusTable.ContextRecId             == _common.RecId

            && workflowTrackingStatusTable.ContextTableId           == _common.TableId

            && workflowTrackingTable.TrackingContext                == WorkflowTrackingContext::WorkItem;


        return workflowTrackingTable.User;

    }

2. Approve a workflow by code

public void autoApproveWF(Common _common, str _user)

    {

        WorkflowWorkItemTable WorkflowWorkItemTable;


        select WorkflowWorkItemTable

            where  workflowWorkItemTable.Type   == WorkflowWorkItemType::WorkItem

            &&  workflowWorkItemTable.Status    == WorkflowWorkItemStatus::Pending

            &&  WorkflowWorkItemTable.RefTableId== _common.TableId

            &&  WorkflowWorkItemTable.RefRecId  ==  _common.RecId;


        WorkflowWorkItemActionManager::dispatchWorkItemAction(

                                WorkflowWorkItemTable,

                                "Auto Approve by system",

                                _user,

                                WorkflowWorkItemActionType::Complete,

                                menuitemDisplayStr(PurchReqTable)); 

    }


3, Display approver name

 display WorkflowApprovalName Displayapprovername()

    {

        WorkflowTrackingStatusTable workflowTrackingStatusTable;

        WorkflowTrackingTable WorkflowTrackingTable;

        WorkflowApprovalName approvername;


        select firstonly workflowtrackingstatustable

            join workflowtrackingtable

           where workflowtrackingstatustable.ContextRecId == this.recid

            && workflowtrackingtable.TrackingContext == workflowtrackingcontext::WorkItem

            && workflowtrackingtable.TrackingType == workflowtrackingtype::Approval

            && workflowtrackingtable.WorkflowTrackingStatusTable == workflowtrackingstatustable .recid;

        {

            approvername = workflowtrackingtable.User;

        }

        return approvername;

    }

    4. Get last approver name for PO by PurchID


     select firstFast _PURCHTABLE where _PURCHTABLE.PurchId == purchTable.PurchId ;

            select firstFast _WorkflowTrackingStatusTable

             order by _WorkflowTrackingStatusTable.RecId desc

             where _WorkflowTrackingStatusTable.ContextRecId == _PURCHTABLE.RecId  && _WorkflowTrackingStatusTable.ContextCompanyId == curext()

                          && _WorkflowTrackingStatusTable.TrackingStatus == WorkflowTrackingStatus::Completed ;

            select _WorkflowTrackingTable

                    order by _WorkflowTrackingTable.createdDateTime desc

                   where _WorkflowTrackingTable.WorkflowTrackingStatusTable == _WorkflowTrackingStatusTable.RecId

                && _WorkflowTrackingTable.TrackingType == 4 ;


Published on:

Learn more
Sherif Fayed
Sherif Fayed

Share post:

Related posts

Power Automate: convertToUtc function

Converts a timestamp from a source time zone to UTC using Windows time zone names.

11 hours ago

Batch Operations and Throttling Mitigation in Power Automate

Bundle hundreds of SharePoint writes into a single $batch request to stop hitting throttling limits.

1 day ago

Extending Power Automate Run History Beyond 28 Days Using Cloud Flow Run Metadata

Power Automate has become the backbone for many business processes, integrations, and automation scenarios across the Power Platform ecosystem...

4 days ago

Power Automate – Restore accidentally deleted flows

We are announcing the ability to restore accidentally deleted flows in Power Automate. This feature will reach general availability on July 30...

5 days ago

Power Automate – Review Firewall Configuration for Upcoming Platform Infrastructure Updates

We identified that one or more flows in your environment may be affected by existing firewall or IP allow list configurations. What action do ...

5 days ago

Power Automate – Export work queue items to CSV

We are announcing the ability to export work queue items to CSV in Power Automate. This feature will reach general availability on July 31, 20...

5 days ago

Power Automate – UI automation repair agent

We are announcing the UI automation repair agent in Power Automate. This feature will reach public preview on July 16, 2026. How does this aff...

5 days ago

Power Automate – View machine and flow utilization in dashboards

We are announcing the ability to view machine and flow utilization in dashboards in Power Automate. This feature will reach general availabili...

5 days ago

Power Automate – Build better forms with integrated Power Apps

We are announcing the ability to launch interactive Power Apps directly from desktop flows within Power Automate. This feature will reach gene...

5 days ago

Power Automate: isInt function

Checks if a string is an integer.

7 days ago
Stay up to date with latest Microsoft Dynamics 365 and Power Platform news!
* Yes, I agree to the privacy policy