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
tarun jain 110tarun jain 110 

I want to access the function of first class in to another class but Error: Compile Error: Method must define a body

I want to access the function of first class in to another class

public with sharing class HelperClass {
public void helperMethod()
{
CustomObject__c acct = new CustomObject__c();
           acct.Name = 'test';
            insert acct;
}
}

public with sharing class MainClass {
private HelperClass helper = new HelperClass();
helper.helperMethod();
}


Please provide answer of above problem i want to do same thing as shown in above classes 
 Error: Compile Error: Method must define a body 

 
Dilip_VDilip_V
Tarun,

You can't call a method in the class.
You have to create a method or constructor  in the class to invoke a method.
Like this:here I am using constructor.
public with sharing class MainClass {
Public MainClass
{
private HelperClass helper = new HelperClass();
helper.helperMethod();
}
}

Let me know if you have any issues.

Mark it as best answer if it helps.

Thanks.
 
tarun jain 110tarun jain 110
Now i facing issue of  Compile Error: Illegal modifier on local variable 
Dilip_VDilip_V
public with sharing class MainClass {
Public MainClass()
{
private HelperClass helper = new HelperClass();
helper.helperMethod();
}
}
Let me know if you have any issues.

Mark it as best answer if it helps.

Thanks.
 
sfdcMonkey.comsfdcMonkey.com
hello Tarun
Please update you class with this code
public with sharing class MainClass {
Public MainClass(){
 HelperClass helper = new HelperClass();
helper.helperMethod();
}
}
Thanks
i hop it helps you
Please Mark it best answer if it helps you so it make proper solution for other:)
 
tarun jain 110tarun jain 110
Hi piyush,
Facing issue of DML currently not allowed 
sfdcMonkey.comsfdcMonkey.com
hi tarun
are you using this class in vf page ?