Email Programs – Apex Programming Language

Email Programs by using Apex




When we want to send an email to the external system from salesforce or recive the email from extensl system then we use email programs. There are two types of emails.

1. Outbound email messaging            2. Inbound email messaging

Outbound email programs: Used to send an email to external system using apex. There are two types of outbound emails.

1. SingleEmailMessage: Used to send a single email message.

Syntax:

Messaging.SingleEmailMessage varEmail = new Messaging.SingleEmailMessage();

2. MassEmailMessage: By using this we can send email to a list oh recipients.

Sytax:

Messaging.MassEmailMessage varMemail = new Messaging.MassEmailMessage();

Messaging name space provides classes/methods for salesforce outbound & inbound functionolities.




How can we send attachments? We can send attachments by using EmailFileAttachment. We will see examples later.

Inbound email programs: This will recieve an email from external system to Salesforce and the apex class will process the email, attachements and perform requested operations.





Salesforce Apex Trigger Examples

Trigger Example scenarios:




Trigger Examples 1. Populate contact description when user creates contact
trigger ContactBeforeInsert on Contact (before insert) {
// Trigger.New hold new version of Contacts
for(Contact contact: Trigger.new){
contact.Description = ‘Contact created successfully by using ContactBeforeInsert trigger’;
}
// No Need to write DML statement, trigger. New will be take care.
}

Trigger Examples 2. Populate contact description with modified user name when user updates contact.
trigger ContactBeforeUpdate on Contact (before update) {
// Trigger.New hold new version of Contacts
for(Contact contact: Trigger.new){
contact.Description = ‘Contact updated successfully by ‘+ userInfo.getUserName() ;
}
// No Need to write DML statement, trigger. New will be take care.
}

Trigger Examples 3. How to write a trigger to inject the above 2 scenarios in one trigger
trigger ContactBeforeInsertUpdate on Contact (before insert, before update) {
// Trigger.New hold new version of Contacts
for(Contact contact: Trigger.new){
if(trigger.isInsert){
contact.Description = ‘Contact created successfully by using ContactBeforeInsert trigger’;
}
If(trigger.isUpdate){
contact.Description = ‘Contact updated successfully by ‘+ userInfo.getUserName() ;
}
}
// No Need to write DML statement, trigger. New will be take care.
}

Trigger Examples 4. Throw an error whenever the user try to delete the contact which is not associated to account
trigger contactBeforeDelete on Contact(before delete){
for(Contact contact: trigger.old){
if(contact.accountId == null){
contact.addError(“Hey! You are not authorized to perform this action.”);
}
}
}




Apex Trigger in Salesforce

What is an Apex trigger?




Trigger is piece of code that is executes before and after a record is Inserted/Updated/Deleted from the force.com database.
Syntax:
Trigger <trigger name> on <Object name> (trigger Events) {
// Implement the Logic here
}

Types of Triggers:
– Before Triggers
– After Triggers

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.
After Trigger: After triggers are used to perform the logic on the related objects and these triggers are used access the fields values that are created by system (Ex: CreatedBy, LasteModifiedBy , Record Id etc..).




Bulk Triggers:
By default every trigger is a bulk trigger which is used to process the multiple records at a time as a batch. For each batch of 200 records.

Trigger Context Variables:
All the trigger context variables are prefixed with “Trigger.” (Ex: Trigger.isInsert, etc..)
isInsert: Returns true if the trigger was fired due to insert operation.
isUpdate: Returns true if the trigger was fired due to update operation.
isDelete: Returns true if the trigger was fired due to delete operation.
isBefore: Returns true if the trigger was fired before record is saved.
isAfter: Returns true if the trigger was fired after record is saved.
New: Returns a list of new version of sObject records.
Old: Returns a list of old version of sObject records.
NewMap: Returns a map of new version of sObject records. (map is stored in the form of map<id,newRecords>)
OldMap: Returns a map of old version of sObject records. (map is stored in the form of map<id,oldRecords>)
Size: Returns a integer (total number of records invoked due to trigger invocation for the both old and new)
isExecuting: Returns true if the current apex code is a trigger.




The below table is tells what are the events we can use in the new trigger and old trigger

Trigger Event Trigger.New Trigger.Old
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

Trigger Context Variable considerations:
– Trigger.Old is always readOnly
– We cannot delete trigger.new
– In before triggers, trigger.new can be used to update the fields on the same object.
– In After trigger, we get run time exception is thrown when user try to modify the fields in the same object.