Saturday 9 March 2013

Concept of Salesforce Trigger

If I will ask question ,what is a  trigger in salesfore ? Is it a class,an Object or apex code ? Most of the people will answer Trigger is an apex code ,yes it is correct ,However rest two are also correct .
Trigger is an object where for each trigger we have written , Sales force will create a record in ApexTrigger object .To know more detail click on the link Trigger object details or login to workbench and select the object .
Trigger is also a class which contains twelve static context variable we will discuss in detail .

Salesforce  trigger executes when we do some DML operation in salesforce object.
In order to execution of trigger it is broadly  of two types ,After Trigger and Before Trigger.
Basic syntax of trigger:-
trigger triggerName on ObjectName (trigger_events) {
    //code_block
}
trigger TestTrigger on Case (Before Insert,After Insert,Before Update ,After Update ,Before Delete ,After Delete,After Undelete) {
 //Code Block
}

[Note:-Keep in mind that only the trigger of that particular object will invoke in which object we will do DML operation, not like  you will insert a record in child object and the trigger in parent object will invoke . It is possible to do  some data manipulation in different object but any way the trigger invoke only the object in which we will do Dml operation].

We can declare more than one trigger event in one trigger ,but each should be separated by comma.
Below are the possible event for trigger.

 >>Before Insert
>>Before Update
>> Before Delete
 >>After Insert
 >>After Update
 >>After Delete
>> After Undelete

There is a System defind class called Trigger which contains 12(twelve) implicit variable,which we can access at run time.
Below are the varibales with description.

1.isExecuting-It rutuns ture if the any code inside trigger context is executing.That means you can test whether your trigger is excuting or not by the help of this variable.It may be from salesforce user interface .apex or  any API


2.isBefore-It rutuns ture if the any code inside trigger context is executing before record is saved to the database.It may be from salesforce user interface .apex or  any API


3.isAfter-It rutuns ture if the any code inside trigger context is executing after record is saved to the database.It may be from salesforce user interface .apex or  any API


4.isInsert-It rutuns ture if the any code inside trigger context is executing due to an insert operation.It may be from salesforce user interface .apex or  any API

5.isUpdate-It rutuns ture if the any code inside trigger context is executing due to an update operation.It may be from salesforce user interface .apex or  any API


6.isDelete-It rutuns ture if the any code inside trigger context is executing due to a delete operation.It may be from salesforce user interface .apex or  any API


7.isUnDelete-It rutuns ture if the any code inside trigger context is executing due to undelete operation  i,e when we recovered data from recycle bin .It may be from salesforce user interface .apex or  any API .


8.new-It returns the new version of the object records.Suppose you have inserted/updated 10 records trigger.new will contain that 1o records .

9.newMap-It returns a map  which contains an IDs  as a key and  the old versions of the sObject records as value.This map is only available in before update, after insert, and after
update triggers.

10.old-It returns the old version of the object records

11.oldMap-It returns a map  which contains an IDs  as a key and  the new versions of the sObject records as value.This map is available for only update and delete trigger.

12.size- It return the size of the manipulated record .It will return one if you will insert one record, It will return the size of the record you are inserting ,updating or deleting or undeleting.

13.Operation Type- This will return an Enum(TriggerOperation) .  Below are the values in enum .
   
  • AFTER_DELETE
  • AFTER_INSERT
  • AFTER_UNDELETE
  • AFTER_UPDATE
  • BEFORE_DELETE
  • BEFORE_INSERT
  • BEFORE_UPDATE


Examples to test the implicit variable:
trigger  TestTrigger on Case (Before Insert,After Insert,Before Update ,After Update ,Before Delete ,After Delete,After Undelete) {
    System.debug('*********testExecution*************'+Trigger.isExecuting);
    System.debug('****************size of trigger***************'+Trigger.size);
    if(Trigger.isBefore){
        if(Trigger.isInsert){
            for(Case cs:Trigger.new){
                System.debug('********beforeInsertNew************'+Trigger.new);
            }
        }
        if(Trigger.isUpdate){
            for(Case cs:Trigger.new){
                System.debug('********beforeUpdateNew************'+Trigger.new);
                System.debug('**********beforeUpdateOld**********'+Trigger.old);
                System.debug('********beforeMapUpdateNewMap************'+Trigger.newMap);
                System.debug('**********beforeMapUpdateOldMap**********'+Trigger.oldMap);
            }
       }
        if(Trigger.isDelete){
            for(Case cs:Trigger.old){
             
                System.debug('**********beforeDeleteOld**********'+Trigger.old);
                System.debug('**********beforeMapDeleteOldMap**********'+Trigger.oldMap);
            }
      }
  }
    if(Trigger.isAfter){
        if(Trigger.isInsert){
             for(Case cs:Trigger.new){
                System.debug('**********afterInsertNew**********'+Trigger.new);
                System.debug('**********afterInsertNewMap**********'+Trigger.newMap);
             }
         }
        if(Trigger.isUpdate){
             for(Case cs:Trigger.new){
                System.debug('**********afterUpdateNew**********'+Trigger.new);
                System.debug('*************afterUpdateOld*******'+Trigger.Old);
                System.debug('**********afterUpdateNewMap**********'+Trigger.newMap);
                System.debug('*************afterUpdateOldMap*******'+Trigger.oldMap);
            }
        }
       if(Trigger.isDelete){
            for(Case cs:Trigger.old){
                System.debug('*************afterDeleteold*******'+Trigger.Old);
                System.debug('*************afterDeleteoldMap*******'+Trigger.oldMap);
            }
       }
       if(Trigger.isUnDelete){
            for(Case cs:Trigger.new){
                System.debug('**********afterunUndeleteNew**********'+Trigger.new);
                System.debug('**********afterunUndeleteNewMap**********'+Trigger.newMap);
             
           }
       }
    }
}
[Note: Just copy this code and paste in case object or else in which object you want to test just the object name you need to change in the above example.After saving the trigger you can insert ,update ,delete and undelete records and just create debug log for different situation and observe.]

Below is the steps how to add this trigger in sales force in case object.
click Your Name Setup Cases Case   Triggers  New
Just remove the existing code and paste the above code ans save the code .
After that follow the below steps to create the debug log.
click Your Name Setup Monitoring Debug logs  
Click on New button to set the debug log in your name .
Now you can create record for case object and try to observe the debug log and just realize all context variable.
Just Insert ,update delete and undelete one record from recycle bin.

Also you can write trigger from developer console 

In the Developer Console, click File | New | Apex Trigger

 Click the below link for some more concepts on before insert trigger .

Before insert conecpt  




75 comments:

  1. Its nice ... Keep it sir ji..

    ReplyDelete
  2. This blog is every helpful for begginers...Thanks for giving us

    ReplyDelete

  3. Thanks for sharing the concept; Salesforce crm cloud application provides special cloud computing tools for your client management problems. It’s a fresh technology in IT industries for the business management. It’s a good option for the fresher to take training get enter in IT field. Salesforce training in Chennai

    ReplyDelete
  4. Nice blog, here I had an opportunity to learn something new in my interested domain. I have an expectation about your future post so please keep updates.Salesforce training institute in Chennai

    ReplyDelete
  5. Thanks for sharing this informative blog. Salesforce is a cloud based CRM software. Today's most of the IT industry use this software for customer relationship management. FITA provides Salesforce Training in Chennai with years of experienced professionals and fully hands-on classes. To know more details about salesforce reach FITA Academy. Rated as No.1 Salesforce Training Institutes in Chennai.

    ReplyDelete
  6. Thanks for sharing the concept.... Salesforce training course is recommended in this cutting-edge competition. It is ideal for system administrators managing the configuration and maintaining the salesforce application in an organization.For more details salesforce online training in hyderabad

    ReplyDelete
  7. Thanks for sharing this niche useful informative post to our knowledge, Actually SAP is ERP software that can be used in many companies for their day to day business activities it has great scope in future.
    Regards,
    SAP institutes in chennai|SAP Training in Chennai|SAP Training Institute in Chennai| sap course in Chennai

    ReplyDelete
  8. your providing such a valuabe information about studying..and also have some good key points to every student. SalesForce Videos

    ReplyDelete
  9. Thanks for the Post Manoj. It gives clear concept about what triggers are and how we can handle those efficiently.

    ReplyDelete
  10. Nice blog, here I had an opportunity to learn something new in my interested domain. I have an expectation about your future post so please keep updates Salesforce Online Training

    ReplyDelete
  11. Thanks for the good topic. Very useful information.
    We IT hub Online Training are good in giving the salesforce Training

    ReplyDelete
  12. Briltus Technologies offer the most effective real time practical oriented Salesforce Training . Our sessions help your group to quickly procure the power they need. Gain from Affirmed specialists as they show building applications regulated and apply what you understand with hands-on activities.
    Visit: http://www.briiltus.com

    ReplyDelete
  13. Hi, do we have support of passive triggers in Salesforce means some batch job running and want to run a trigger if certain condition met?

    ReplyDelete
  14. what is scope of triggers like for which event which trigger is allowed or not

    ReplyDelete
  15. I am reading your post from the beginning, it was so interesting to read & I feel thanks to you for posting such a good blog, keep updates regularly Salesforce Training and Placement | Salesforce Developer Training

    ReplyDelete
  16. Hello! Someone in my Facebook group shared this website with us, so I came to give it a look. I’m enjoying the information. I’m bookmarking and will be tweeting this to my followers! Wonderful blog and amazing design and style.

    Hadoop Training in Chennai

    ReplyDelete
  17. Very Impressive Salesforce tutorial. The content seems to be pretty exhaustive and excellent and will definitely help in learning Salesforce course. I'm also a learner taken up Salesforce training and I think your content has cleared some concepts of mine. While browsing for Salesforce tutorials on YouTube i found this fantastic video on Salesforce. Do check it out if you are interested to know more.https://www.youtube.com/watch?v=5FTe-ah3WBU

    ReplyDelete
  18. Well done! It is so well written and interactive. Keep writing such brilliant piece of work. Glad i came across this post. Last night even i saw similar wonderful Salesforce tutorial on youtube so you can check that too for more detailed knowledge on Salesforce.https://www.youtube.com/watch?v=vp0lYTtVKhs

    ReplyDelete
  19. After seeing your article I want to say that the presentation is very good and also a well-written article with some very good information which is very useful for the readers....thanks for sharing it and do share more posts like this.
    Click here:
    angularjs training in velarchery
    Click here:
    angularjs training in sholinganallur

    ReplyDelete
  20. I read this post two times, I like it so much, please try to keep posting & Let me introduce other material that may be good for our community.
    Click here:
    Microsoft azure training in chennai
    Click here:
    Microsoft azure training in annanagar

    ReplyDelete
  21. Thank you for taking the time to provide us with your valuable information. We strive to provide our candidates with excellent care and we take your comments to heart.As always, we appreciate your confidence and trust in us

    Blueprism training in Chennai

    Blueprism training in Bangalore

    Blueprism training in Pune

    Blueprism training in tambaram

    Blueprism training in annanagar

    Blueprism training in velachery

    Blueprism training in marathahalli

    ReplyDelete
  22. Thank you for taking the time and sharing this information with us. It was indeed very helpful and insightful while being straight forward and to the point.
    Blueprism training in annanagar

    Blueprism training in velachery

    Blueprism training in marathahalli


    AWS Training in chennai

    AWS Training in bangalore

    ReplyDelete
  23. The knowledge of technology you have been sharing thorough this post is very much helpful to develop new idea. here by i also want to share this.
    Devops training in sholinganallur

    ReplyDelete
  24. Great Article… I love to read your articles because your writing style is too good, its is very very helpful for all of us and I never get bored while reading your article because, they are becomes a more and more interesting from the starting lines until the end.

    rpa training in electronic city | rpa training in chennai

    rpa online training | selenium training in training

    ReplyDelete
  25. All the points you described so beautiful. Every time i read your i blog and i am so surprised that how you can write so well.
    python training in Bangalore
    python training in pune
    python online training
    python training in chennai

    ReplyDelete
  26. Great Article… I love to read your articles because your writing style is too good, its is very very helpful for all of us and I never get bored while reading your article because, they are becomes a more and more interesting from the starting lines until the end.

    Data science training in tambaram | Data Science training in anna nagar
    Data Science training in chennai | Data science training in Bangalore
    Data Science training in marathahalli | Data Science training in btm

    ReplyDelete
  27. This comment has been removed by the author.

    ReplyDelete
  28. This is a good post. This post give truly quality information. I’m definitely going to look into it. Really very useful tips are provided here. thank you so much. Keep up the good works.
    Data Science course in kalyan nagar | Data Science course in OMR
    Data Science course in chennai | Data science course in velachery
    Data science course in jaya nagar | Data science training in tambaram

    ReplyDelete
  29. Your good knowledge and kindness in playing with all the pieces were very useful. I don’t know what I would have done if I had not encountered such a step like this.

    java training in chennai | java training in bangalore

    java interview questions and answers | core java interview questions and answers

    ReplyDelete

  30. Whoa! I’m enjoying the template/theme of this website. It’s simple, yet effective. A lot of times it’s very hard to get that “perfect balance” between superb usability and visual appeal. I must say you’ve done a very good job with this.

    Selenium Interview Questions and Answers

    Best Selenium Training in Chennai | Selenium Training Institute in Chennai | Besant Technologies

    Selenium Training in Bangalore | Best Selenium Training in Bangalore

    Free Selenium Tutorial |Selenium Webdriver Tutorial |For Beginners

    ReplyDelete
  31. The post is written in very a good manner and it entails many useful information for me. I am happy to find your distinguished way of writing the post. Now you make it easy for me to understand and implement the concept.

    angularjs-Training in velachery

    angularjs Training in bangalore

    angularjs Training in bangalore

    angularjs Training in btm

    angularjs Training in electronic-city

    angularjs online Training

    ReplyDelete
  32. Nice article i have ever read information's like this.it's really awesome the way you have delivered your ideas.i hope you will add
    more content in your blog.
    AWS Training in Guindy
    AWS Training in Saidapet
    AWS Certification Training in Anna nagar
    AWS Training in Ambattur

    ReplyDelete
  33. Nice article i have ever read information's like this.it's really awesome the way you have delivered your ideas.i hope you will add more content in your blog.
    Salesforce Training in Nolambur
    Salesforce Training in Perambur
    Salesforce Training in Saidapet
    Salesforce Training in Ashok Nagar

    ReplyDelete
  34. Worthful SalesForce tutorial. Appreciate a lot for taking up the pain to write such a quality content on SalesForce tutorial. Just now I watched this similar SalesForce tutorial and I think this will enhance the knowledge of other visitors for suresales force training institute in hyderabad

    ReplyDelete
  35. I likable the posts and offbeat format you've got here! I’d wish many thanks for sharing your expertise and also the time it took to post!!
    Microsoft Azure online training
    Selenium online training
    Java online training
    Python online training
    uipath online training

    ReplyDelete
  36. Good Post. I like your blog. Thanks for Sharing
    Salesforce Course in Delhi

    ReplyDelete
  37. Attend The Python training in bangalore From ExcelR. Practical Python training in bangalore Sessions With Assured Placement Support From Experienced Faculty. ExcelR Offers The Python training in bangalore.
    python training in bangalore

    ReplyDelete
  38. Hi,
    Good job & thank you very much for the new information, i learned something new. Very well written. It was sooo good to read and usefull to improve knowledge. Who want to learn this information most helpful. One who wanted to learn this technology IT employees will always suggest you take big data hadoop training in bangalore. Because big data course in Bangalore is one of the best that one can do while choosing the course.

    ReplyDelete
  39. Excellent and very cool idea and the subject at the top of magnificence and I am happy to comment on this topic through which we address the idea of positive re like this.CRM Software in Denmark

    ReplyDelete
  40. Thank you for sharing this post
    Very nice post here thanks for it I always like and search such topics and everything connected to them.
    CRM Software in Denmark

    ReplyDelete
  41. Well explanation with great coding knowledge. This blog gonna helpful to many. I am expecting these kind blogs in future too.
    AWS training in chennai | AWS training in annanagar | AWS training in omr | AWS training in porur | AWS training in tambaram | AWS training in velachery

    ReplyDelete
  42. Get the best nursing services baby care services medical equipment services and allso get the physiotherapist at home in Delhi NCR For more information visit our site keep on search more
    Ai & Artificial Intelligence Course in Chennai
    PHP Training in Chennai
    Ethical Hacking Course in Chennai Blue Prism Training in Chennai
    UiPath Training in Chennai

    ReplyDelete
  43. Very good post. Nice example and excellent detail. oracle training in chennai

    ReplyDelete
  44. Infycle Technologies, the
    No.1 software training institute in Chennai
    offers the leading Python course in Chennai for tech professionals and students at the best offers. In addition to the Python course, other in-demand courses such as Data Science, Selenium, Oracle, Java, Power BI, Digital Marketing also will be trained with 100% practical classes. After the completion of training, the trainees will be sent for placement interviews in the top MNC's. Call 7504633633 to get more info and a free demo.

    ReplyDelete
  45. Get the best Oracle course in chennai from Infycle Technologies, one of the excellent Software Training Institute in Chennai. Great place to study Oracle and we also provide all technical courses like Oracle, Java, Data Science, Big data, AWS, Python, etc. with the best trainers receiving the amazing training for the best career. For more details and demo classes call 7504633633.

    ReplyDelete
  46. incredibly great article! I pulsate individuals to acknowledge exactly how amicable this compensation for an assessment is for your article. Its tempting, convincing substance material. Your points of view are greatly gone my own getting into excuse to for this topic. Coffeecup Responsive Site Designer Crack

    ReplyDelete
  47. Intriguing test for a weblog. I've been filtering the net for diversion just and showed up re your web site page. impeccable lucid. Thankful to you a ton for sharing your knowledge! it is fortifying to look that specific people anyway supplement an undertaking into adjusting to their locales. I'll be genuine to check affirm inside the works over again unambiguous quickly. Earth Day Greetings

    ReplyDelete