Skip to main content

Recall/cancel and resubmit/submit a workflow through X++ code

Bharani Preetham Peraka Profile Picture Bharani Preetham Pe... 3,460 Super User

Recently I came across a requirement to cancel and resubmit the workflow through code. Then I went through the form in UI and found the backend code in clicked method of the form. But I did not get the exact idea. Then out of curiosity, thought like I may have some class like Workflow. Fortunately, I found one.

Then I checked all the methods, used the find references and finally found 2 methods which are required for my purpose.

Now, lets check how can we do cancel or submit the workflow through code.

Recall / Cancel a workflow

To recall/cancel a workflow through code, simply call the static method called cancelWorkflow of Workflow class. The parameters, you need to provide here are the correlation id and a comment which we actually give when cancelling the workflow from UI.

The code looks like below.

Workflow::cancelWorkflow(workFlowTrackingTable.CorrelationId, "cancel by code");

Now the problem comes.

Where do I get the correlation Id?

It can be found in WorkflowTrackingStatusTable. To get the exact buffer, you must give a select on this table with where condition on InstanceNumber from this table.

So, the final code would be as below.

WorkflowTrackingStatusTable                 workFlowTrackingTable;

select firstonly CorrelationId from workFlowTrackingTable where workFlowTrackingTable.InstanceNumber == '002647';

Workflow::cancelWorkflow(workFlowTrackingTable.CorrelationId, "cancel by code");

So first we should find the exact record of that workflow to be submitted, then call the cancelWorkflow method.

Resubmit / Submit a workflow

To resubmit or submit a workflow, you need to call again another static method from same class called “activateFromWorkflowType”.

This can be done as below.

Workflow::activateFromWorkflowType(workflowtypestr(HcmDiscussion), hcmDiscussion.RecId, "SubmittedBy by Code", NoYes::No);

But wait.

We also have a return type here called WorkflowCorrelationId in the above static method. So, the code will be,

WorkflowCorrelationId		workflowCorrelationId;
HcmDiscussion               hcmDiscussion;

hcmDiscussion = HcmDiscussion::find(5637146584);

workflowCorrelationId = Workflow::activateFromWorkflowType(workflowtypestr(HcmDiscussion), hcmDiscussion.RecId, "SubmittedBy by Code", NoYes::No);

As my workflow is in HRM module, I am using the table called HcmDiscussion. Based on the place where we will work with the workflow, we should get the table name accordingly and we need to pass the parameters accordingly. The logic to submit workflow will be same across all modules or tables but based on the place, the table should be selected.

Coming to the method activateFromWorkflowType, the parameters to be passed are explained below.

The first one is the workflowtype for that module (if you are not able to get it you can make a find reference and find what is being using in that module), then the RecId of the table involved, comment, if activating from web yes otherwise no. We also have another parameter which has default value like curUserid(). We can also change the last parameter on requirement.

In this way we can simply cancel or submit the workflow through code. This logic can be implemented for multiple records or in batch or with different tables too.

Comments

*This post is locked for comments