Let's talk briefly about Salesforce.
Salesforce is a cloud based online solution for customer relationship management, or CRM. Salesforce provides all of our departments like marketing, sales, commerce, and service with a shared view of our customers with a single integrated CRM platform.
"Apex is a strongly typed, object-oriented programming language that allows developers to execute flow and transaction control statements on Salesforce servers in conjunction with calls to the API. Using syntax that looks like Java and acts like database stored procedures, Apex enables developers to add business logic to most system events, including button clicks, related record updates, and Visualforce pages. Apex code can be initiated by Web service requests and from triggers on objects." – Salesforce
Apex triggers enable you to perform custom actions before or after changes to Salesforce records, such as insertions, updates, or deletions. A trigger is Apex code that executes before or after the following types of operations: insert, update, delete, merge, upsert, undelete.
Trigger Context Variables means all triggers define implicit variables that allow developers to access run-time context. These variables are isExecuting, isInsert, isUpdate, isDelete, isBefore, isAfter, isUndelete, new, newMap, old, oldMap, operationType and size contained in the System.Trigger class.
Reading this article, you can learn and test the simple Apex Trigger in SalesForce.
Apex Trigger in SalesForce
The prerequisites for testing Apex Trigger in SalesForce are:
R· Register a Salesforce Free Trail account using the following
link.
Step 1
Login your Salesforce Account and Click the Developer Console
Step 2
The General Syntax for Apex Trigger is,
- trigger TriggerName on ObjectName (trigger_events) {
- code_block
- }
Now, we can create new Apex Trigger HelloTrigger and select the sObject as Account and the file named as HelloTrigger.apxt,
After creating Apex Trigger HelloTrigger and add the following code for displaying the message before inserting a new account in the Standard Object Account,
- trigger HelloTrigger on Account (before insert) {
- System.debug('Hello World!');
- }
Step 3
For Debugging the Apex Trigger HelloTrigger,
Click Debug menu and Select Open Execute Anonymous Window,
Now, write the Apex code for Trigger, and enable the Open log option for Viewing output and click Execute,
- Account a = new Account(Name='Hello Trigger');
- insert a;
Step 4
Now we can verify the output. After clicking Execute, Log will open automatically and select "Debug Only" option,
Also in Account sObject added the newly created account name as Hello Trigger
Summary
Now you have successfully tested the Apex Trigger in a SalesForce environment.