Tuesday, September 12, 2023

Multi select workflow using x++

 I got a requirement to submit multiple records to workflow.

For this, I have created multiple buttons like below.

Submit Code:

#define.WorkFlowTemplateName("Template Name")  
// or -- workFlowTypeStr(Template name)

if (common.WorkflowApprovalStatus == WFApprovalStatus::Draft)
{
    Workflow::activateFromWorkflowType(#WorkFlowTemplateName, common.RecId, 
	                                'Workflow submitted by button', false, Curuserid());
}

Approval Code:
WorkflowWorkItemTable   WorkflowWorkItemTable;

// main code
select firstonly WorkflowWorkItemTable
    where workflowWorkItemTable.Type == WorkflowWorkItemType::WorkItem
	&& workflowWorkItemTable.Status == WorkflowWorkItemStatus::Pending // this should be Pending
	&& WorkflowWorkItemTable.RefTableId == tableNum(common)
	&& WorkflowWorkItemTable.RefRecId  == common.RecId;

if (WorkflowWorkItemTable)
{
    WorkflowWorkItemActionManager::dispatchWorkItemAction(WorkflowWorkItemTable,
							"Mulitple Approve by button", 
							curuserId(), 
                                                        WorkflowWorkItemActionType::Complete,
							menuitemActionStr("Approve menu item name"));
}


Reject Code:
WorkflowWorkItemTable   WorkflowWorkItemTable;

// main code
select WorkflowWorkItemTable
    where workflowWorkItemTable.Type == WorkflowWorkItemType::WorkItem
	&& workflowWorkItemTable.Status == WorkflowWorkItemStatus::Pending
	&& WorkflowWorkItemTable.RefTableId == tableNum(Common)
	&& WorkflowWorkItemTable.RefRecId  == common.RecId;

if (WorkflowWorkItemTable)
{
    ttsbegin;
    WorkflowWorkItemActionManager::dispatchWorkItemAction(WorkflowWorkItemTable,
							    "Mulitple Reject by button", 
							    curuserId(), 
							    WorkflowWorkItemActionType::Return,
							    menuitemActionStr('Reject menu item Name'));
    ttscommit;
}


Recall Code:
#define.WorkFlowTemplateName("Template Name")  
// or -- workFlowTypeStr(Template name)

if (common.WorkflowApprovalStatus == WFApprovalStatus::Submitted 
    || common.WorkflowApprovalStatus == WFApprovalStatus::PendingApproval)
{
    WorkflowCorrelationId	workflowCorrelationId = Workflow::activateFromWorkflowType(#WorkFlowTemplateName, 
											    common.RecId, 
                                                                                            'By button RecallAllToWorkflow', 
											    true, Curuserid());
   
    Workflow::cancelWorkflow(workflowCorrelationId,"Mulitple Recalled by user");
}


Button Clicked method Code:
    For multi-select, we need to loop the record by record so for that I have used the button clicked. Instead of this, we can create a new class and write in the main method.

void clicked()
{   
    FormDataSource      Common_ds = element.Common_ds;
    Common    	       common    = Common_ds.cursor(); 
    System.Exception    ex;

    next clicked();      

    if (Common_ds.Anymarked())
    {
        for (common = getFirstSelection(Common_ds); common; common = Common_ds.getNext())
	{
	    try
	    {
	        // call the Approve or submit or Reject code 
	    }    
	    catch
	    {
		ex = CLRInterop::getLastException().GetBaseException();
		error(ex.get_Message());
	    }
	}
    }
	
    Common_ds.research(true);
}

I have set the image for the button group using the below properties.




Keep daxing!!











No comments:

Post a Comment