Data types in Apex
Premitive Datatype:
Integer, String,
Decimal, Long, Date, DateTime, ID etc.
Sobject Datatype:
Salseforce support
two types of objects: Standard and Custom. We can define these datatype in this
way:
Account
objAcc = new Account();//Standard sObject Example
JobInfo__c
objJobInfo = new JobInfo__c();// Custom sObject Example
Data Collection
List:
List is an ordered
collection of elements. Every list have specific data type and we can add same
datatype elements in the list in ordered pattern. List can contain duplicate
elements.
Syntax:
List<String>
lstItem = new List<string>();
Set: Set
is an unordered collection of elements and it cannot have duplicate elements.
Set<string>
setItem = new set<string>();
Map: Map is having two attributes: key and
value. Every specific key is mapped with a specific value.
Syntax:
Map<integer,string>
mapEmpIDwithEmail = new map<integer,string>();
Put elements in map:
mapEmpIDwithEmail.put(100,’abc@gmail.com’);
mapEmpIDwithEmail.put(101,’xyz@gmail.com’)
Retrieve value from map:
mapEmpIDwithEmail.get(100); Output: ‘abc@gmail.com’
Apex Trigger Overview
Trigger
basically have some piece of apex code which execute on a specific event. Its
self executable code and we don’t need to execute that explicitly. It executed
implicitly on a specific event.
Syntax:
Trigger <triggername> on <object
Name>(trigger Events){
//Add
Trigger logic
}
Types of Triggers:
1.
Before Trigger: Before triggers are
used to perform the logic on the same object and specifically we cannot use the
DML operation (Insert, update, delete) on these triggers.
These triggers are fired before the data is saved into the database.
These triggers are fired before the data is saved into the database.
2.
After Trigger: After triggers are
used to perform the logic on the related objects and these triggers are used to
access the fields values that are created by system (Ex: CreatedBy,
LasteModifiedBy , Record Id etc..).
Trigger Context
Variable:
A.
Trigger.isExecuting:
Returns true if the current context for the Apex code is a trigger, not a Visualforce
page, a Web service, or an executeanonymous() API
call.
B.
Trigger.isInsert:
Returns true if this trigger was fired due to an insert operation,
from the Salesforce user interface, Apex,
or the API.
C.
Trigger.isUpdate:
Returns true if this trigger was fired due to an update operation,
from the Salesforce user interface, Apex,
or the API.
D.
Trigger.isDelete:
Returns true if this trigger was fired due to a delete operation,
from the Salesforce user interface, Apex,
or the API.
E.
Trigger.isBefore:
Returns true if this trigger was fired before any record was saved.
F.
Trigger.isAfter:
Returns true if this trigger was fired after all records were saved.
G.
Trigger.isUndelete:
Returns true if this trigger was fired after a record is recovered
from the Recycle Bin (that is, after an undelete
operation from the Salesforce user interface, Apex, or the API.)
H. Trigger.New:
Returns
a list of the new versions of the sObject records.Note that this sObject list
is only available in insert
and update
triggers, and the records can only be modified in before triggers.
- Trigger.newMap:
A
map of IDs to the new versions of the sObject records.Note that this map is
only available in before
update, after
insert, and after
update triggers.
- Trigger.Old:
Returns
a list of the old versions of the sObject records.Note that this sObject list
is only available in update
and delete
triggers.
- Trigger.OldMap:
A
map of IDs to the old versions of the sObject records.Note that this map is
only available in update
and delete
triggers.
- Size:
The total number of records in a
trigger invocation, both old and new.
Trigger.New
and Trigger.Old Usage:
Below is the table which indicates what are
the events which we can use in New and
Old triggers.
|
Trigger
Event
|
Trigger.New,
TriggerMap
|
Trigger.Old,
Trigger.oldMap
|
|
Before Insert
|
Yes
|
No
|
|
Before Update
|
Yes
|
Yes
|
|
Before Delete
|
No
|
Yes
|
|
Before UnDelete
|
No
|
Yes
|
|
After Insert
|
Yes
|
No
|
|
After Update
|
Yes
|
Yes
|
|
After Delete
|
No
|
Yes
|
No comments:
Post a Comment