sap-commerce-cloudbackoffice

How can I insert a Workflow dynamically when an Order is placed in SAP Commerce Cloud [Hybris]?


I need to create a Workflow when an Order is placed and when the assigned user changes the Order attribute, it should trigger some business logic.

So far I have defined the Workflow template for Updating Order attribute, but unable to figure out how to dynamically create the Workflow when order is placed from storefront.


Solution

  • You can try overriding afterPlaceOrder method from DefaultCheckoutFacade, get the workflow template and trigger it from there

    WorkflowTemplateService workflowTemplateService;
    WorkflowService workflowService;
    WorkflowTemplateService workflowTemplateService;
    
    protected void afterPlaceOrder(final CartModel cartModel, final OrderModel orderModel)
    {
        ........
        // get the workflow template
        final WorkflowTemplateModel workflowTemplate = workflowTemplateService
                    .getWorkflowTemplateForCode(WFL_TEMPLATE_CODE);
    
        // create new workflow based on template
        final WorkflowModel workflow = workflowService.createWorkflow(workflowTemplate, attachment, user);
    
        // save the workflow
        modelService.save(workflow);
    
        // start the workflow
        workflowProcessingService.startWorkflow(workflow);
        ........
    }