1

I'm trying to invoke an autolaunched flow from an apex class like below,

@RestResource(urlMapping='/apex/contact_us')
global class ContactUsAPI {
    Map<String, Object> request = (Map<String, Object>) JSON.deserializeUntyped(RestContext.request.requestBody.ToString());
    Flow.Interview.Contact_Us myFlow = new Flow.Interview.Contact_Us(request);     
    myFlow.start();   
}

I'm getting the error "Invalid constructor name: myFlow.start" when trying to save the apex class. Note: I need this apex class to be an API endpoint, therefore, using the @RestResource and setting the class to be global but I did try to eliminate things so I tried without the annotation and switched to a normal public class instead of global but still got the same error.

I even tried the first example on this page. I've already done the trailhead so I opened up my playground org and verified the autolaunch Discount Calculator flow is there and is active. Then I created a simple apex class and just copied/pasted from the referenced documentation page but still got the same error "Invalid constructor name: myFlow.start"

The reason I'm trying to do this is that the service calling this API uses JSForce library to authenticate and call salesforce endpoints. I still have not been able to find out how to use JSForce to call a salesforce flow similar to how we call apex classes. Here's my other question asking about JSForce and how to call a flow from that library, Can I use jsforce to launch/invoke a lightning flow

1 Answer 1

1

Executable code needs to be in a method. In your case, an @HttpPost method:

@RestResource(urlMapping='/apex/contact_us')
global class ContactUsAPI {
  @HttpPost global static void doPost() {
    Map<String, Object> request = (Map<String, Object>) JSON.deserializeUntyped(RestContext.request.requestBody.ToString());
    Flow.Interview.Contact_Us myFlow = new Flow.Interview.Contact_Us(request);     
    myFlow.start();
  }
}

The first two lines are treated as properties with initializers, which is why they don't throw compile errors.

4
  • Thanks, you rock. Do you have any information to share about whether we can use JSForce library to call an autolaunched flow which will help me avoid creating these additional apex classes? I've posted a question separately. I appreciate it if you can help me there as well. salesforce.stackexchange.com/questions/391243/…
    – Bahman.A
    Dec 5, 2022 at 22:50
  • I just tried this and the only thing missing seems to be a 'static' keyword for the method because of the @HttpPost.
    – Bahman.A
    Dec 5, 2022 at 22:52
  • 1
    @Bahman.A Yeah, that was an oversight on my part.
    – sfdcfox
    Dec 5, 2022 at 23:12
  • 1
    @Bahman.A Added an answer for the other thing as well. It should be straightforward.
    – sfdcfox
    Dec 6, 2022 at 0:21

You must log in to answer this question.

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