Friday 21 August 2015

A Detailed Salesforce Trigger Tutorial

This article is a tutorial on how to create a trigger in Salesforce. It is a rather involved process that requires getting your feet wet with programming logic.

Just like in traditional programming, this tutorial will help you learn the process by creating a “Hello World” project. Please note that while this Salesforce trigger tutorial will discuss the process of creating a custom object partially, you need to familiarize yourself in detail.

Salesforce Trigger Tutorial – Steps For Creating A Custom Object

Step 1- Log into your developer/sandbox organization

Step 2- Click on Your Name then Setup Then Create Then Objects Then ‘New Custom Object’

Step 3 – For the label enter ‘Book’ and for the plural label ‘Books’

Step 4 – Click on ‘Save’

Step 5 – In the ‘Custom Fields & Relationships’ section of the Book detail page click on ‘New’

Step 6 – Choose ‘Number’ as the data type then click on ‘Next’

Step 7 – For the field label enter ‘Price’ and enter 16 in the length text box

Step 8 – In the decimal places text box enter 2 and click on ‘Next’

Step 9 – To accept all other default values click on ‘Next’

Step 10 – Click on ‘Save’

Before proceeding further with this Salesforce trigger tutorial you need to be familiar with some programing conventions. You can consult C programming conventions tutorials before you proceed since Apex is based on C#, which is in turn based on C.

1 – Private, public, and static declarations

2 – Variables

3 – Float, integer, and string variable types

4 – Functions and classes

5 – Flow logic (else, if, switch, while, for, and then)

6 – Voids

7 – Returns

8 – Semicolon terminations

9 – Algebraic/parenthetical expressions

10 – Brackets for encapsulation of the code block

11 – Line comments

Moving on to writing your first Apex class

1 – Click on Your Name Then Setup Then Develop then Apex Classes then ‘New’

2 – Assign the class definition in the class editor as such:

Public class MyhelloWorld {

}

This is the class name and it will encapsulate the code for the class itself.

3 – Inside the class encapsulation, insert the following code:

Public stat void applyDiscount(Book_c[] books) {

for (Book_c b :Books){

b.Price_c *=0.9;

{

}

If you find this code’s functionality mystifying in some way you need to stop and consult some C and C# documentation before you proceed.

4 – Click on ‘Save’

Now you are ready to add the trigger itself

Step 1 – Click on your Name then Setup then Create then objects and click on the object you created earlier

Step 2 – Click on ‘New’ in the triggers section

Step 3 – Delete the default code in the trigger editor and enter the following:

Trigger HelloWorldTrigger on Book_c  {

Book_c[] books = Trigger.new;

My HelloWorld.applyDiscount(books);

}

The first line defines the trigger, gives it a name, and links it to an object. It provides an event for the trigger.

The second line creates a list of records named ‘Books’ instancing the trigger. If unfamiliar with instancing then refer to general programming documentation. The final line invokes the applyDiscount method you wrote in the class above.

Conclusion

If you have been following the instructions in this Salesforce trigger tutorial then you have successfully learned how to create a trigger in Salesforce. However, to deploy and test this class you must have advanced Apex knowledge not covered in this tutorial.



from http://monalisaofblogging.com/a-detailed-salesforce-trigger-tutorial

No comments:

Post a Comment