0

I am looking to run an Autolauched Flow by simply clicking a Button on my Case Detail page.

The flow in questions performs a series of actions (updates and deletes) based on the Case Id and running User - so intervention required. I realize that you can run Screenflows from a Quick Action, but there are no screens required/desired so I would prefer to stick with an Autolaunched flow. I have used a Lightning Web Component for other actions, but I think this is too complex.

Unfortunately you cannot run flows direct from Lightning Web Components however you can run Apex.

I have created an Apex Class (currently just hardcoded with the Case ID) to start an Autolaunched Flow and an LWC to launch it. In my LWC I am running the Apex Method it via a Wire Property. Because my flow is updating records I am hitting DML errors - Error Occurred: Too many DML statements: 1.

Could someone assist in adjusting the LWC to run imperatively?

Apex Class

public class FlowController {
     
   public FlowController(){
        //Intialization
    }
    @AuraEnabled(cacheable=true)
    public static void callFlow(){
    
        Id caseId = '5004V00001AOwDAQA1';
            
        Map<String, Object> recordMap = new Map<String, Object>();
        recordMap.put('recordId', caseId);
        
        Flow.Interview.CaseFollow_Unfollow myFlow = new  Flow.Interview.CaseFollow_Unfollow(recordMap);
        myFlow.start();
    }
}

LWC Js

import { LightningElement, wire } from 'lwc';
import callApex from '@salesforce/apex/FlowController.callFlow';

export default class LaunchFlowDemo extends LightningElement{
    @wire (callApex) apexMethod;
}
3
  • What is the dml error you getting?
    – metasync
    Mar 9, 2022 at 19:30
  • Error Occurred: Too many DML statements: 1 Mar 9, 2022 at 19:45
  • that helped me figure it out! thanks! Mar 10, 2022 at 4:02

2 Answers 2

0

You're getting the error because the method is set as cacheable, and you can't make database changes (DML) in cacheable methods.

But first, I'd suggest you revisit the idea of running this as a screen flow with a Flow Quick Action. There may not be any user intervention required, but it might be desirable to have a Screen. Perhaps a screen before the action to ask them to confirm before taking the operations, or after the operations to tell them the operations were successful (or let them know if an error happened!).

This would have a twofold benefit: simpler, clearer configuration that is easier for someone else to look at and know what it does, and potentially a better user experience. If you click the button and nothing happens, was it successful? Error? How would they know?

If you really want to avoid any screens, an alternative to this apex-launched flow approach would be to use a screen flow, and use a custom navigation component to navigate the user to the record they are already on after the operation. The user would see their data refresh after the operation without a screen.

1
  • Jeff - thanks for the feedback and tips. your solution of using a screen flow and redirect would have been a lot simpler. However I was able to get this working and added some Toast messages for feedback - the final code is here: salesforce.stackexchange.com/questions/371226/… Mar 10, 2022 at 20:35
0

I used this article and updated to code accordingly LWC Too Many DML Statements 1
I updated the code below

import { LightningElement, api } from 'lwc';
import callFlow from '@salesforce/apex/FlowController.callFlow';

export default class LaunchFlowDemo extends LightningElement{ 
  flowname = 'CaseFollow_Unfollow';
  @api recordId;
  @api async invoke(){
    let result = await callFlow();
  }
}

Not the answer you're looking for? Browse other questions tagged .