Salesforce Developer Tutorial: How to Implement a Trigger Framework

แชร์
ฝัง
  • เผยแพร่เมื่อ 11 ก.ค. 2024
  • In this video we go over how to implement a trigger framework in your salesforce environment and why you would ever care to do it. Learning how to do this will make your life much easier as a Salesforce Developer.
    Please consider donating to help continue to improve the content produced for Coding With The Force: www.paypal.com/donate?busines...
    Get Coding With The Force Merch Here: my-store-d20559.creator-sprin...
    GitHub repo with code from this video here: github.com/Coding-With-The-Fo...
    For more information on this check out the blog post here: codingwiththeforce.com/salesfo...
    sfdc-trigger-framework github repo: github.com/kevinohara80/sfdc-...
    Documentation on virtual classes: developer.salesforce.com/docs...
    Follow all of our social media here:
    Patreon: / codingwiththeforce
    Github: github.com/Coding-With-The-Force
    Facebook: / codingwiththeforce
    Twitter: / coding_force
    Instagram: / codingwiththeforce
    Salesforce development books I recommend:
    Advanced Apex Programming: amzn.to/3lXi8to
    Salesforce Lightning Platform Enterprise Architecture: amzn.to/2R0D4BQ
    Mastering Salesforce DevOps: amzn.to/3lTtTRN
    ----------------------------------------------------------------------------------------------------------------------------------------
    Table of Contents:
    0:00 - Intro
    0:26 - Why use a trigger framework?
    3:35 - The trigger framework I like to use.
    6:06 - How to use the trigger framework.
    22:42 - Demoing the trigger
    25:45 - Outro
    DISCLAIMER: The views and ideas expressed on this TH-cam channel and blog are based on past development experiences and may not always work for your particular scenarios. The advice in these videos may also become outdated as technology advances. They should be used as examples for educational purposes only. Using any shown solutions in your own org is not advisable since they are example scenarios.
    #salesforce #salesforcedeveloper #salesforceadmin

ความคิดเห็น • 24

  • @CodingWithTheForce
    @CodingWithTheForce  3 ปีที่แล้ว +8

    For some reason I just kept wanting to say, "Trigger Handler Framework" in this video... It's just a "Trigger Framework". I'm sorry my brain just couldn't stop feeding me those words. It's just a Trigger Framework.

  • @marvtheandroid
    @marvtheandroid 3 ปีที่แล้ว +2

    Thanks, super helpful to see these practices in action. I was given one sample trigger framework before and it had way too many layers of abstraction. Looking forward to more videos like this!

  • @iulianachiriac3623
    @iulianachiriac3623 ปีที่แล้ว

    Thank you very much for all your videos. They are very helpful.

  • @stevenduong8126
    @stevenduong8126 2 ปีที่แล้ว

    That is awesome. Thanks so much,Sir.

  • @FrenchyRider574
    @FrenchyRider574 ปีที่แล้ว +1

    Thank you !

  • @muhammadkhattak1573
    @muhammadkhattak1573 2 ปีที่แล้ว +1

    Very helpfull thank so for the help

  • @chethelesser
    @chethelesser 3 ปีที่แล้ว

    Aiight, first of all, thank you for your content! It's of the highest quality compared to the competition in regards to both topics and delivery.
    I want to discuss the "Why?" points because I was asked this at an interview, and didn't answer so great repeating the usual stuff that's listed on the web on this topic. So here's my 5 cents.
    1. "If you put your logic in the trigger, it becomes hard to unit test that logic." I'm on the fence about this. I don't really have a good argument for it, but I'd rather test DML operations and not trigger handler methods. Kinda because it's closer to the actual use case. As for helper methods - you can use them inside the trigger, with no handler class as a mediator. So there's no difference in that regard whether you put your logic inside the trigger or not, it's a matter of how you organize your tests.
    2. Reusability. I find this unrelated. Again, put the reusable parts in helpers/utilities and reference them right inside the trigger. No difference whether you're using a handler or not.
    3. Readability (nice, modular, beautiful-looking code). Yep. No objections.
    What I don't see mentioned is by using a handler inside a trigger you get to choose system vs user mode. Trigger always executes in system mode, while you can write a with sharing handler, and that's easily an equally important reason as reusability and readability in my opinion.

  • @coolboy5470
    @coolboy5470 3 ปีที่แล้ว +4

    Why dont you make a video for how we can use OOPs concepts in apex and best practices in apex with live examples

    • @CodingWithTheForce
      @CodingWithTheForce  3 ปีที่แล้ว +3

      That’s a great idea! I will definitely add it to the list. Thanks Ashutosh.

  • @aalailasafouane1067
    @aalailasafouane1067 5 หลายเดือนก่อน

    very helpfull

  • @marikenafeuereisen5338
    @marikenafeuereisen5338 2 ปีที่แล้ว +1

    Great video as always! I still have this confusion, in which scenarios to use Trigger "isBefore" and "isAfter"

    • @kenwilson8415
      @kenwilson8415 8 หลายเดือนก่อน

      I tend to use isBefore if I want to make direct updates to the record that initiated the trigger. I use isAfter if I want to do something to other records (child records, parent records, completely unrelated records).

  • @russellcopeland6487
    @russellcopeland6487 3 ปีที่แล้ว +1

    Very cool. I will look into this for my own projects.
    Have you ever used a Logger Framework? Logging has been a pain point with Salesforce and I've wondered if Logger Frameworks could help.

    • @CodingWithTheForce
      @CodingWithTheForce  3 ปีที่แล้ว +1

      Thank you so much! I definitely have investigated them. This is my favorite logger framework personally, I plan to do a video on it eventually, but it is excellent and makes life quite a bit easier when logging: github.com/rsoesemann/apex-unified-logging

  • @salesforcetutorialsbybill4148
    @salesforcetutorialsbybill4148 3 ปีที่แล้ว +2

    Any reason you prefer this framework over one that uses a Custom Metadata setting to control the execution of Apex?

    • @CodingWithTheForce
      @CodingWithTheForce  3 ปีที่แล้ว +1

      You can very easily add that to this framework. In fact I have a version of the code that does it in about 5 lines of code if you are interested. I actually prefer utilizing custom settings for this instead of custom metadata (if you're talking about turning the trigger on and off by flipping a switch or based on a running user). I have a video explaining why I prefer custom settings if you're interested: th-cam.com/video/eoTi_SQUjp4/w-d-xo.html&ab_channel=CodingWithTheForce

    • @CodingWithTheForce
      @CodingWithTheForce  3 ปีที่แล้ว

      To further elaborate on the question though. This framework is just super simple and easy to understand and it's also very flexible. I've tried using 5-6 different frameworks and this one was the easiet to implement, the easiest to teach developers, allowed me to easily add whatever I needed and it gave me all the core benefits of a typical trigger framework.

  • @russellcopeland6487
    @russellcopeland6487 3 ปีที่แล้ว +1

    The Trigger Framework is perfect for a use case I'm facing. Is there a similar concept of a Validation Rules Framework that I can use to bypass Validation Rules?

    • @CodingWithTheForce
      @CodingWithTheForce  3 ปีที่แล้ว +1

      Hey, I’m sorry for the late response, I must’ve missed this comment somehow. You can actually use this exact same tactic to create validation rule switches. In fact, I think (maybe) at some point I briefly mention that in the video. Here’s a pretty good article explaining how to achieve that in validation rules: salesforceprofs.com/hierarchy-custom-setting-in-validation-rules/

    • @russellcopeland6487
      @russellcopeland6487 3 ปีที่แล้ว

      @@CodingWithTheForce not late at all! I watched the video when you first released it and have a need for it now. Thanks for the validation rule suggestion, I will try that out.

  • @clydetan7731
    @clydetan7731 3 ปีที่แล้ว

    What if in a scenario where you want to do something in both after insert and after update? would you need to override both afterInsert() and afterUpdate() in the handler?

    • @clydetan7731
      @clydetan7731 3 ปีที่แล้ว

      I mean, that's going to be 2 method in 1 class calling the same logic from a utility class. Or is there any way to do it in a single override method?

    • @CodingWithTheForce
      @CodingWithTheForce  3 ปีที่แล้ว +1

      @@clydetan7731 If you wanted to do that, what I would suggest is making another virtual method in the TriggerHandler class called "AfterActions" or something and have it called in the run method for both afterInsert and afterUpdate triggercontexts. Then you can override that virtual method in your class and only call it instead of the individual afterInsert or afterUpdate methods. Hopefully that makes sense.

    • @clydetan7731
      @clydetan7731 3 ปีที่แล้ว

      @@CodingWithTheForce thank you