Saturday 17 December 2011

Triggers

This is the  tutorial in series and we will see that how to create a Trigger and Test Cases in salesforce.


A trigger is an Apex script that executes before or after specific data manipulation language (DML) events occur, such as before object records are inserted into the database, or after records have been deleted.
Triggers are stored as metadata in Salesforce.com. A list of all triggers in your organization is located at Setup | Develop | Apex Triggers. In addition to this list, triggers are associated and stored with specific objects.
To define a trigger:




1. For a standard object, click Setup | Customize, click the name of the object, then click Triggers.For a custom object, click Setup | Create| Objects and click the name of the object.For campaign members, click Setup | Customize | Campaigns | Campaign Member| Triggers.For case comments, click Setup | Cases | Case Comments | Triggers.
For email messages, click Setup | Cases | Email Messages| Triggers.


2. In the Triggers related list, click New.


3. Click Version Settings to specify the version of Apex and the API used with this trigger. If your organization has installed managed packages from the AppExchange, you can also specify which version of each managed package to use with this trigger. Generally, you should use the default values for all versions. This associates the trigger with the most recent version of Apex and the API, as well as each managed package. You can specify an older version of a managed package if you want to access components or functionality that differs from the most recent package version. You can specify an older version of Apex and the API to maintain specific behavior.


4. Select the Is Active checkbox if the trigger should be compiled and enabled. Leave this checkbox deselected if you only want to store the script in your organization’s metadata. This checkbox is selected by default.


5. In the Body text box, enter the Apex for the trigger. A single trigger can be up to 32,000 characters in length.


To define a trigger, use the following syntax:


trigger triggerName on ObjectName (trigger_events) {
code_block
}
where trigger_events can be a comma-separated list of one or more of the following events:
• before insert
• before update
• before delete
• after insert
• after update
• after delete
• after undelete
So, lets start with creating trigger.
I want that duplicate student should not be created on the basis of Email id. we can achieve this by other way also, like during creation of email field, we can specify it as unique field.
Open eclipse and right click on salesforce project and select Create new Trigger, as shown in below image


As you can see, to create trigger we have to select the Apex version and operations in wizard.


During Trigger creation, keep in mind that it may be required in bulk operations so governor limit may be problem.


So instead of getting query for all triggers individually, I created an array and after that created a set of email entered for all records, and loop through all records invidually and checked for duplicity.


Only one SOQL is fired instead of all triggers individually and thus can work for bulk insert.


For SOQL Best Practice refer :
http://wiki.developerforce.com/index.php/Best_Practice:_Avoid_SOQL_Queries_ Inside_FOR_Loops


Q:1I want that duplicate student should not be created on the basis of Email id. we can achieve this by other way also, like during creation of email field, we can specify it as unique field.
Open eclipse and right click on salesforce project and select Create new Trigger, as shown in below image




Code:

=========
trigger Trigonstudent on Student__c(before insert){


List<Student__c> studentlist=Trigger.new;
set<String> emailset=new set<String>();


        for(Student__c s:studentlist){
        
              emailset.add(s.Email__c);
  
}
List<Student__c> duplicatestudentlist=[select s.name,s.email__c from Student__c   s where s.Email__c in : emailset];
set<string> duplicateemailset=new set<string>();


for(Student__c s:duplicatestudentlist){


  duplicateemailset.add(s.Email__c);
  }
  for(Student__c s:studentlist){


    if(duplicateemailset.contains(s.Email__c)) {


      s.Email__c.addError('Record already exist with some emailid');
           }
  }
}




Then We get  the things like below:







Note: You can add, edit, or delete Apex using the Salesforce.com user interface only in a Developer Edition organization, a Salesforce.com Enterprise Edition trial organization, or sandboxorganization. In a Salesforce.com production organization, you can only make changes to Apex by using the Metadata API


deploy call, the Force.com IDE, or theForce.com Migration Tool. The Force.com IDE and Force.com Migration Tool are free resources provided by salesforce.com to support its users and partners, but are not considered part of our Services for purposes of the salesforce.com Master Subscription Agreement.

Test Cases in Salesforce :

Test case integral part of code developement.
·         You must have at least 75% of your Apex scripts covered by unit tests to deploy your scripts to production environments. In addition, all triggers should have some test coverage.
·         Salesforce.com recommends that you have 100% of your scripts covered by unit tests, where possible.
·         Calls to System.debug are not counted as part of Apex code coverage in unit tests.
So, here we are going to create Test Case for trigger which we have written:


Testclass for it:
=============
@isTest 
private class TestonstudentTriggers {
    static testMethod void myUnitTest() {
         Student__c s = new Student__c();
            s.Name = 'Om Test';
            s.Lastname__c = 'LastName';
            s.Installment__c = 2000;
            s.Email__c = 'admin@shivasoft.in';
      try{
             insert s;
        }
        catch(System.DMLException e){
                                                System.assert(e.getMessage().contains('Record already exist with same email Id'));
       }
 }
}

When we run this we get through setup->Develop->ApexClasses




3)Trigger on Account
===============
Q)When we create an account then automatically 5 or more  contacts are created with that account name.

trigger accountTestTrggr2 on Account (after insert,after update,after undelete) {
List<Account> acc=[select BillingCountry from Account];
List<Contact> con=new List<Contact>(); 
List<Case> lstcase=new List<Case>();
if(Trigger.isInsert){
for(Account a: Trigger.new){
  for(Integer i=1;i<=5;i++){
    Contact con1=new Contact(accountid=a.id,lastname=a.Name+'.'+i,MailingCountry=a.BillingCountry);
      con.add(con1);
    
  }
  }
if(con.size()>0){
     insert con;
   
}

 if(trigger.isUndelete){
for(Account actobj:trigger.new){
Case csobj=new  Case();
csobj.Accountid=actobj.id;
csobj.Status='New';
csobj.origin='Email';
csobj.Description='An Account :'+actobj.Name+' has been created from Recyclebin';
lstcase.add(csobj);
}
if(lstcase!=null && lstcase.size()>0){
insert lstcase;
}
 }
}







13 comments:

  1. Hi Balaji.......this is one of the greatest and simplest of blogs I have ever read.....well done.....

    ReplyDelete
  2. hi Balaji this was very helpful blogs for me ,,,,,,,,,thanks

    ReplyDelete
  3. please give more examples on trigger...

    ReplyDelete
  4. Hi vanaja reddy,
    See this links also for handling recursive calling of triggers.

    http://salesforce-walker.blogspot.in/2012/01/controlling-recursive-triggers.html

    http://salesforce-walker.blogspot.in/2012/08/handling-recursive-trigger.html

    i will give more examples as soon as possible.

    ReplyDelete
  5. Hi Balaji
    can you explain use of undelete and isdelete in this code..
    I am a bit new to Coding

    ReplyDelete
  6. Hi Sai,
    Whenever we want to achieve som functionality when someone undelete the record from recyclebin then we need to use Undelete event.

    And coming to "isDelete" is a trigger context variable to variate the functionality for delete only.Means that we can variate the functionality for different events in a single trigger by using trigger context variables. See the following link for more information.

    http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_triggers_context_variables.htm

    ReplyDelete
  7. Great Post! thanks!

    ReplyDelete
  8. excellent presentaion........thanks

    ReplyDelete
  9. Hi balaji i am new to salesforce , I this above code u define set emailid = new set();
    and assigning all the emailID values to it.
    my doubt is set is a collection of unorder values which does not allow duplicates. Here we are assigning all the email;id values to it where duplicates are present.
    Can any one help me out.

    Thanks in advance.

    ReplyDelete
    Replies
    1. This comment has been removed by the author.

      Delete

How to include a screen flow in a Lightning Web Component

 Hi, Assume  you have a flow called "Quick Contact Creation" and API Name for the same is "Quick_Contact_Creation". To i...