You need to sign in to do that
Don't have an account?
Thangamani Nachimuthu 6
Static method cannot be referenced from a non static context: String
Hi I am trying to write a Apex class to create an account and return the record name and i am getting the below error:
Static method cannot be referenced from a non static context: String
Here is my Code:
public class AccountHandler {
public static String insertNewAccount(String strNames)
{
try
{
Account acct = new Account();
acct.Name = strNames;
acct.Phone = '(415)555-1212';
acct.NumberOfEmployees = 100;
insert acct;
ID acctID = acct.Id;
// Display this ID in the debug log
System.debug('ID = ' + acctID);
return strNames;
}
catch(Exception ex)
{
return 'NULL';
}
}
}
Static method cannot be referenced from a non static context: String
Here is my Code:
public class AccountHandler {
public static String insertNewAccount(String strNames)
{
try
{
Account acct = new Account();
acct.Name = strNames;
acct.Phone = '(415)555-1212';
acct.NumberOfEmployees = 100;
insert acct;
ID acctID = acct.Id;
// Display this ID in the debug log
System.debug('ID = ' + acctID);
return strNames;
}
catch(Exception ex)
{
return 'NULL';
}
}
}
As this is a static method so we don't need to create an object for this class.
Instead we can execute like below,
string accountName = AccountHandler.insertNewAccount('test account');
Please let us know if this helps.
Thanks,
Govindaraj.S
All Answers
Your code looks fine. Hope your executing correctly. You will see this error if you try to execute by (Instantiate the class) like below:
As your method is already declared as static you don't need to instantiate the class. Try calling it like below: in your Execute anyonymus window: ClassName.MethodName()
Hope this helps! Please mark as best if it does.
Thanks
As this is a static method so we don't need to create an object for this class.
Instead we can execute like below,
string accountName = AccountHandler.insertNewAccount('test account');
Please let us know if this helps.
Thanks,
Govindaraj.S
Thanks for your response.I am able to execute the way you had given..