Variable Declaration:
You must declare every variable with data type in Apex like primitive, sObject, collection, Object. Fore example, the code bellow is declared with data type as collection of Account.
List
SOQL:
It will be used to fetch the data from Salesforce object in database. Fore example, The code below is fetching data from Account object and assigning records to variable lstAcc.
lstAcc=[SELECT id, name, description FROM Account];
Loop Statement:
This statement is used for iterating over a collection or iterating over a piece of code for specified number of times. In code bellow shown how to iteration over list of Account (lstAcc).
for(Account a: lstAcc){
a.description='Test loop';
}
Conditional (If-Else) Statements:
you can cause Apex code to execute based on a certain condition, it is decided whether to go for execution or to stop the execution of the particular piece of code. For example, in the code shown below, it is checking whether the list contains records or it is empty.
if(lstAcc.size()>0){
System.debug('>Record size:'+lstAcc.size());
}
else{
System.debug('>list is empty');
}
DML Statement:
Executes the records insert, update, upsert, delete operation on the records in database. For example, the below code is updating list of Account with new field value.
for(Account a: lstAcc){
a.description='Test loop';
}
if(lstAcc.size()>0){
update lstAcc;
}
No comments :
Post a Comment