Creating APEX Class in Salesforce
Syntax of class:
Private | public | global“Virtual | abstract | with sharing | without sharing | (none)”
Class implements | (none)” “extends | (none)”
{
// the body of the class
}
To create an Apex class, go to the following path Your Name ---> SetupàApp setup--->Develop ---> click on Apex classes ---> click on "New" button.
*In the editor pane, enter the following code Public class Helloworld{
}
Click on Quicksave ---> Saves the Apex Code, making it available to be executed ---> it makes us add and modify code. Save --> savers the Apex Code, but that classes the class editor and returns to the Apex classes list.
*Add the static method to the above class Now Apex class become. Publish class Helloworld {public static void sayyou( ) { System.debug ( 'you' );
}
*Now add an instance method. Now the Apex class became public class Helloworld
{// class creationPublic static void sayyou () { //adding static method to the class
}
Public void sayMe ( ) { //adding instance method to system debug (‘Me '); the class
Calling a class Method:
We have created the "Helloworld" class follow these steps to call its methods.
- Execute the following code in the Developer console to call the Helloworld class's static method.
*** To call a static method, we don't have to create an instance of the class. Helloworld say you (); Open Raw log, then "You" appears in the USER-DEBUG log.
- Now execute the following code in Developer's Console to call the Helloworld classes instance method.
Note:- To call an instance method, we first have to create an instance of the Helloworld class.
Helloworld h= new Helloworld ( );h.say Me ( );
Open Raw log, then "Me" appears in the USER-DEBUG log.
Inclined to build a profession as SalesForce Developer? Then here is the blog post on, explore SalesForce Training
Alternation APEX Class Creation:
We can also create new Apex classes directly in the Developer Console.
- Open the Developer Console.
- Click the repository tab.
- The setup Entity type panel lists the different items. We can view and edit in the Developer Console.
- Click on classes, and then click the "New" button.
- Enter "Message" for the name of the
- new class and click "ok" bottom.
- Add the following static method to the new class
public static string hellowMessage () {return ( 'welcome to capital info solutions '):
}
- Click on "Save" button.
Examples 1:
Public class AccountCreation {Public Account createAccount(String name){
Account a= new Account();
a.Name = name; insert a;
return a;
}
}
Go to the Developer console, and execute the following code
Account Creation ac = new Account Creation();
creating an instance for the above class. ac.CreateAccount('Osmania University');
calling a method
system.debug(ac);
check the account is created or not in the USER-DEBUG Log.
2.The following program is used fetch the Account Name from the account records by passing phone number.
Public class Fetch_Account_ Name_ From_Phone{ Public set<string> Fetch_ ACC_ Name(String P) { set <string> s1= new Set<string>();List<Account> acc= “select id, Name from Account where phone”; for (Account a:acc)
{
String s=a.name; s1.add(s); }
System.debug('xxxxxxxxx' +s1); return s1;
}
}
Go to the Developer Console, and execute the following code. FetchAccountName_ From_Phone ft= new FetchAccountName_from_Phone (); ft.FetchAccName ('9052'); In the User-Debug Log all the accounts will come that has the phone number is 9052.
For in-depth understanding on Salesforce click on
- Introduction To Apex
- Salesforce Interview Questions & Answers
- Annotations in SalesForce
- Loops in SalesForce
- Apex Data Manipulation
- Salesforce Lightning Training