Friday, July 1, 2016

Basic Salesforce Apex Syntax

Apex code typically contains many things that we might be familiar with from other programming languages.




1. Naming Variables Apex

 Declaring variables:
datatype​ variable_name;
                           OR:
datatype​ variable_name = value;
EX:
      Account objAcc = new Account(); 
     List<Account> listAcc = new List<Account> (); 

You cannot use any of the Apex reserved keywords when naming variables, methods or classes, such as: list, for,...

 2. SOQL Query Apex 

This will be used to fetch the data from Salesforce database.

EX:
Select Id, name from Account

 3. Loop Statement Apex

Apex supports the following types of loops:
 + Do-while
 + While
 + For
EX:
for (Integer i = 0; i < 10; i++) {
     System.debug("SalesforceXQ");

 4. Conditional (if-else) Apex 

EX:
 Integer i = 1;
 if (i == 0) {
     System.debug('Salesforce');
 } else if (i == 1) {
     System.debug('XQ');
} else {
     System.debug('SalesforceXQ');
}
5. DML Statement Apex 

Performs the records insert, update, upsert, delete operation on the records in database.
Ex:
update listAccount 
6. Using Collections Apex

 Apex has the following types of collections:
 + Lists (arrays): A list is a collection of elements, such as Integers, Strings, objects, or other collections.
List<Account> listAcc = new List<Account> (); 
 + Maps: A map is a collection of key-value pairs. Keys can be any primitive data type. Values can include primitive data types, as well as objects and other collections. 
Map<Id, Account> mapAcc = new Map<Id, Account>(); 
 + Sets: A set is a collection of unique, unordered elements. It can contain primitive data types, such as String, Integer, Date, sObjects,... 
Set<String>setStr = new Set<String> ();

0 nhận xét:

Post a Comment