Primitive Data Types and Variables:: Example
Primitive Data Types and Variables:: Example
String
Boolean
Time, Date and Datetime
Integer, Long, Double and Decimal
Null Variables
Enum
String:
Example:
String myVariable = 'I am a string.';
We can also create String from value of other data type. It can be done using static
method valueOf()
Example:
1. Date myDate = Date.today();
String myString = String.valueOf(myDate);
System.debug(myString);
Methods in String:
1. endsWith(suffix):
Example:
String s = 'Hello Jason';
System.debug(s.endsWith('Jason'));
2. length():
Example:
String myString = 'abcd';
Integer result = myString.length();
System.assertEquals(result, 4);
3. substring(startIndex, endIndex):
Returns a new String that begins with the character at the specified zero-
based startIndex and extends to the character at end Index - 1.
Example:
'hamburger'.substring(4, 8);
// Returns "urge"
'smiles'.substring(1, 5);
// Returns "mile"
1960-02-17 00:00:00
18:30:02.020Z
Example2:
//You can also create dates and times from the current clock:
Datetime has the addHours, addMinutes, dayOfYear, timeGMT methods and many others.
Date myToday = Date.today();
Date myNext30 = myToday.addDays(30);
System.debug('myToday = ' + myToday);
System.debug('myNext30= ' + myNext30);
Null Variables:
Variable value will be set to null if we don’t initialise it.
Enums:
Enums are typically used to define a set of possible values that don’t otherwise have a
numerical order. Typical examples include the suit of a card, or a particular season of the
year.
To define an enum, use the enum keyword in your declaration and use curly braces to
demarcate the list of possible values.
public enum Season {WINTER, SPRING, SUMMER, FALL}
Case Sensitivity:
Apex is case insensitive language
Lists:
Ordered group of items of same type.
Conditional Statements:
If-else statements:
if(condition is true) {
//do this
} else {
//do this
}
Switch Statements:
switch on expression {
when value1 {
//code block 1
}
when value2 {
//code block 2
}
when else { //if none of the previous values apply
//code block 3
}
}
Loops:
While loops:
Syntax:
While(condition) {
//run this block of code
}
Do While Loops:
Syntax:
Do {
//run this block of code
} while(condition);
For loops:
//normal loop
for (Integer i = 1; i <= 10; i++){
System.debug(i);
}
//loop iterating over a list:
Integer[] myInts = new Integer[]{10,20,30,40,50,60,70,80,90,100};
for (Integer i: myInts)
{
System.debug(i);
}
Sets:
Unordered collection of objects and doesn’t contain duplicate values
Set s = new Set{'a','b','c'};
s.add('c');
s.add('d');
if (s.contains('b'))
Maps:
Example:
Map <integer, String> employeeAddresses = new Map<integer, String();
System.debug(myStrings.get('a'));
Methods
A method can be declared as
Example:
wilt(4);
public static void wilt(Integer numberOfPetals){
system.debug(numberOfPetals);
}
Example:
public class Flower {
public static Integer wilt(Integer numberOfPetals){
if(numberOfPetals >= 1){
numberOfPetals--;
}
return numberOfPetals;
}
public static void grow(Integer height, Integer maxHeight){
height = height + 2;
if(height >= maxHeight){
pollinate();
}
}
public static void pollinate(){
System.debug('Pollinating...');
}
}
Static variables:
We can access static variables without instantiating the calss.
Final variable:
We cannot change the value of final variable once declared.
We can pass object as argument as follows
public static void toDebug(Fridge aFridge) {
Interfaces:
Interfaces is used for abstraction
Interfaces don’t have any implementations
//Creating an interface
public interface InterfaceName{
String method();
}
Implements is the method used to overload the method form an interface.
Example:
public class NewAccounts {
public static void sObjectsInsert(Integer value){
Integer counter = 1;
//create a list to add our accounts
List<Account> teaFactoryAccounts = new List<Account>();
while(counter <= value){
//display the current counter value
System.debug('Counter Value before Incrementing ' + counter);
//create a new account
Account store = new Account();
store.Name = 'The Tea Factory ' + counter;
store.AccountNumber = '35629' + counter;
teaFactoryAccounts.add(store);
System.debug(teaFactoryAccounts);
//increment the counter
counter = counter + 1;
System.debug('Counter Value after incrementing ' + counter);
}
System.debug('Size of Account List: ' + teaFactoryAccounts.size() );
System.debug('Elements in Account List: ' + teaFactoryAccounts);
//insert all of the accounts in the list
insert teaFactoryAccounts;
}
}
SOQL:
SOQL clauses:
Example:
public class AccountUtility {
public static void viewAnnualRevenue(){
List<Account> accountsList = [SELECT Name, AnnualRevenue FROM Account];
for (Account con : accountsList){
String acctRev = con.Name + ':' + con.AnnualRevenue;
system.debug(acctRev);
}
}
}
Child object, and include fields from a related parent object, use a child-to-
parent query.
Parent object, and include fields from a related child object, use a parent-to-
child query.