function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
BarracksBarracks 

Simple beginner questions about Apex classes

Hi All,

I'm new to Apex programming and I ran into some problems and I hoped one of you could advise.

I'm trying to follow this article on how to call a visual cloud flow from Apex:
https://andyinthecloud.com/2014/10/26/calling-flow-from-apex/

I followed the instructions on the Hello World Example, I created a simple flow, opened the developer console and pasted the following:
 
// Call the Flow
Map<String, Object> params = new Map<String, Object>();
Flow.Interview.ReturnHelloWorld helloWorldFlow = new Flow.Interview.ReturnHelloWorld(params);
helloWorldFlow.start();

It worked great.

Then I wanted to create a new Apex class to store it.
I created a new Class and pasted the following:
public class Test {

// Call the Flow
Map<String, Object> params = new Map<String, Object>();
Flow.Interview.ReturnHelloWorld helloWorldFlow = new Flow.Interview.ReturnHelloWorld(params);
helloWorldFlow.start();
}

But got this error message: 
Error: Compile Error: Method must define a body at line 6 column 1

So here's my first question, why did it work on the Developer console without defining the method first?

Anyways, I added the method and here's the complete code:
public class Test {
// Call the Flow
Map<String, Object> params = new Map<String, Object>();
Flow.Interview.ReturnHelloWorld helloWorldFlow = new Flow.Interview.ReturnHelloWorld(params);
helloWorldFlow.start();

   public void start() {
      helloWorldFlow = new Flow.Interview.ReturnHelloWorld(params);
      helloWorldFlow .start();
   }

}

But the error persists!
What am I doing wrong? Clearly I'm missing something very obvious.

Thank you.
 
Best Answer chosen by Barracks
Pruthvi KankunthalaPruthvi Kankunthala

@Barracks : To facilitate the development of robust, error-free code, developer Console supports the creation and execution of unit tests. Unit tests are class methods that verify whether a particular piece of code is working properly.

In Apex method is more of a synonym for function . Try this code 

public class Test {

public void startTheFlow(){
// Call the Flow
Map<String, Object> params = new Map<String, Object>();
Flow.Interview.ReturnHelloWorld helloWorldFlow = new Flow.Interview.ReturnHelloWorld(params);
helloWorldFlow.start();
}
}
start() is a predefined method . If you use that name for your own custom method , then Salesforce Apex engine will be confused , so do not use the standard method names .Let me know if any more issues come .