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  




115 comments:

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

    ReplyDelete
    Replies
    1. IEEE Final Year projects Project Centers in Chennai are consistently sought after. Final Year Students Projects take a shot at them to improve their aptitudes. IEEE Final Year project centers ground for all fragments of CSE & IT engineers hoping to assemble.Final Year Projects for CSE

      Spring Framework has already made serious inroads as an integrated technology stack for building user-facing applications. Spring Framework Corporate TRaining .

      Specifically, Spring Framework provides various tasks are geared around preparing data for further analysis and visualization. Spring Training in Chennai

      The Angular Training covers a wide range of topics including Angular Directives, Angular Services, and Angular programmability.Angular Training

      Delete
  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. Salesforce is a cloud based CRM software. Today's most of the IT industry use this software for customer relationship management. To get more details about salesforce please refer this site.

    Regards..
    Salesforce Developer Training in Chennai

    ReplyDelete
  7. Excellent post!!! In this competitive market, customer relationship management plays a significant role in determining a business success. That too, cloud based CRM product offer more flexibility to business owners to main strong relationship with the consumers. Salesforce Training Institutes in Chennai | Salesforce Training in Chennai

    ReplyDelete
  8. 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
  9. 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
  10. In coming years, cloud computing is going to rule the world. The cloud based CRM tool provider like Salesforce have massive demand in the market. Thus talking salesforce training in Chennai from reputed Salesforce training institutes in Chennai will ensure bright career prospects for aspiring professionals.

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

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

    ReplyDelete
  13. 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
  14. Thanks for the good topic. Very useful information.
    We IT hub Online Training are good in giving the salesforce Training

    ReplyDelete
  15. Nowadays, most of the businesses rely on cloud based CRM tool to power their business process. They want to access the business from anywhere and anytime. In such scenarios, salesforce CRM will ensure massive advantage to the business owners. Amazon Web Services Training in Chennai

    ReplyDelete
  16. 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
  17. 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
  18. what is scope of triggers like for which event which trigger is allowed or not

    ReplyDelete

  19. This is excellent information. It is amazing and wonderful to visit your site.Thanks for sharing this information,this is useful to me...
    Android Training in Chennai
    Ios Training in Chennai

    ReplyDelete
  20. Hai Author, Very Good informative blog post,
    Thanks

    ReplyDelete
  21. 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
  22. Your very own commitment to getting the message throughout came to be rather powerful and have consistently enabled employees just like me to arrive at their desired goals.
    Best Java Training Institute Chennai

    ReplyDelete
  23. 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
  24. 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
  25. The information which you have provided is very good. It is very useful who is looking for
    salesforce Online Training Bangalore

    ReplyDelete
  26. 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
  27. 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
  28. 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
  29. 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
  30. 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
  31. Good Post! Thank you so much for sharing this pretty post, it was so good to read and useful to improve my knowledge as updated one, keep blogging.
    Devops training in tambaram|Devops training in velachery|Devops training in annanagar|Devops training in sholinganallur

    ReplyDelete
  32. 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
  33. 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
  34. 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
  35. Useful information.I am actual blessed to read this article.thanks for giving us this advantageous information.I acknowledge this post.and I would like bookmark this post.Thanks

    java training in tambaram | java training in velachery

    java training in omr | oracle training in chennai

    ReplyDelete
  36. 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
  37. A very nice guide. I will definitely follow these tips. Thank you for sharing such detailed article. I am learning a lot from you.

    rpa training in electronic-city | rpa training in btm | rpa training in marathahalli | rpa training in pune

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

    ReplyDelete
  39. 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
  40. 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

  41. 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
  42. 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
  43. It has been simply incredibly generous with you to provide openly what exactly many individuals would’ve marketed for an eBook to end up making some cash for their end, primarily given that you could have tried it in the event you wanted.
    safety course in chennai

    ReplyDelete
  44. 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.
    iosh course in chennai

    ReplyDelete
  45. 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.

    Software Testing Training in Chennai | Best Software Testing Institute
    Authorized Dotnet Training in Chennai | Dotnet Training in Chennai
    PHP Training in Chennai | Best PHP Training Institute |PHP syllabus



    Advanced Android Training in Chennai | Best Android Training in Chennai


    ReplyDelete
  46. This blog is the general information for the future. You got a good work for these blog.We have a developing our creative content of this mind.Thank you for this blog. This for very interesting and useful.

    Software Testing Training
    QTP Training in Chennai
    Selenium Training in Chennai
    LoadRunner Training in Chennai

    ReplyDelete
  47. 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
  48. 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
  49. Those guidelines additionally worked to become a good way to recognize that other people online have the identical fervor like mine to grasp great deal more around this condition.
    iosh safety course in chennai

    ReplyDelete
  50. 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
  51. Amazing article. Your blog helped me to improve myself in many ways thanks for sharing this kind of wonderful informative blogs in live. I have bookmarked more article from this website. Such a nice blog you are providing ! Kindly Visit Us @ Best Travels in Madurai | Tours and Travels in Madurai | Madurai Travels

    ReplyDelete
  52. 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.
    angularjs online training

    apache spark online training

    informatica mdm online training

    devops online training

    aws online training

    ReplyDelete
  53. 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
  54. Good Post. I like your blog. Thanks for Sharing
    Salesforce Course in Delhi

    ReplyDelete

  55. Thank you for sharing the article. The data that you provided in the blog is informative and effective.
    Best Devops Training Institute

    ReplyDelete
  56. 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
  57. Awesome blog. I enjoyed reading your articles. This is truly a great read for me. I have bookmarked it and I am looking forward to reading Python classes in pune new articles. Keep up the good work!

    ReplyDelete
  58. 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
  59. Wonderful thanks for sharing an amazing idea. keep it...

    Looking for Software Training in Bangalore , learn from Softgen Infotech Software Courses on online training and classroom training. Join today!

    ReplyDelete
  60. 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
  61. 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
  62. 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
  63. 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
  64. Very good post. Nice example and excellent detail. oracle training in chennai

    ReplyDelete
  65. Nice Post, I hope really gather more information about python its very useful for me. thanks for sharing your idea.
    Python Online Training
    Python Online Training in Chennai
    Python Online Course in Chennai

    ReplyDelete
  66. Awesome blog. It was very informative. I would like to appreciate you. Keep updated like this!

    Data Science Training in Gurgaon
    Bigdata Hadoop Training in Gurgaon
    Spark Training in Gurgaon

    ReplyDelete
  67. Thanks for posting the best information and the blog is very helpful.python course in Bangalore

    ReplyDelete
  68. Sharing the same interest, Infycle feels so happy to share our detailed information about all these courses with you all! Do check them out
    Best Hadoop training in chennai & get to know everything you want to about software trainings.

    ReplyDelete