What are Apex Basics? A Guide in 2023
The primary languages required to know in salesforce are: -
- Apex – It is used to write the business logic or we can say it is used to write the controller. It is similar to JAVA.
- Visual force – It is used to build/create a custom UI (User Interface), It is similar to HTML.
- Lightning – It is also used to develop UI and it is a framework that is used to provide a better user interface to end users it is mobile responsive as well.
Apex Basics
Apex is a strongly typed (it means we have to declare every variable with a particular data type) object-oriented programming language which is a proprietary language developed by salesforce.com to allow us to write the code that executes on the force.com platform.
The basic features of Apex are:
Connected to the Database
Unlike java, apex does not have to make a connection with the database and fire a query and then get the result to interact with code written in it. Instead, it is already integrated with the database.
So, all we have to do is just write the query in [ // code ] and it will automatically return the results from the database.
Don't forget to check out: Salesforce Apex Class | Salesforce Development | SFDC Development
Easy to Test
Apex provides its own testing framework which allows us to test the code with the help of test classes and unit test cases.
Multi-tenant Environment
An apex multitenant environment is used which means that a common application for multiple groups or clients is used such as multiple clients using the same server but the data of one client is secure and isolated from other groups.
An apex class is saved with any API version then it will be executed with the same API version even if upgrades are made or new versions arrive.
Case-insensitive Language
Apex works as a case-insensitive language.
When to Use Apex?
- Apex is used when we have to perform some business processes that are not supported by workflows or process builders or flows.
- To perform complex validation over multiple objects.
- To create web service and email services.
- For transactions and rollback.
Apex Datatype
- Primitive – Integer, Boolean, string, Date etc.
- sObject – generic or specific e.g. Account.
- Collection – list, set, map.
Apex Classes
User-defined and system supplied
User-defined example
public class democlass { }
Apex Class Constructor
public class democlass { public democlass(){ system.debug('I am in constructor'); } }
Apex Class Method
public class democlass { Boolean b; public void methoddemo(){ b=true; system.debug('I am in method'); system.debug('b =' + b); } }
Specific sObject
Account a = new Account();
a.Name=’test’;
acc.Phone='123456';
insert acc;
MyObj__c obj = new MyObj__c();
Check out another amazing blog by Bhawana here: Related Records Updation with the Help of Salesforce Flow
Generic sObject
In generic it is made with reference to the specific sObject such as
sObject sobj1 = new Account(Name = ‘Test’);
sObject sobj2 = new MyObj__c (Name = ‘Test’);
Casting generic sobject to specific sobject
- Account acc =(Account) sObj1;
We cannot access the fields of generic sobject without type casting it into specific. So, we can use casting to be able to access fields using dot notation, which is not available on the generic sObjects.
Difference between system.debug and system.assert
- System.debug will let you see the output from the apex.
- System.assert is used in test classes to make sure expected results match with actual outputs.
Responses