Preface – This post is part of the Write Your First Intermediate Trigger series.
This chapter is going to change your life.
Once you understand how to use SOQL in triggers, the whole world of Salesforce opens up to you. Sure, you can still do a lot with a simple trigger, but once you start combining SOQL, it’s hard to find something you can’t do with it!
Now why is it so important to mix SOQL with Apex? Traditionally in a trigger, you only really have access to the record that’s being created or edited. You can do anything you want, but only on that record! Once you throw SOQL into the mix, every record in Salesforce becomes available to you at any time. Anything! Even abstract records, like deleted records, approval processes, sharing rules, etc. Anything you can conceive and more is available to you – you’ll truly be amazed at what Salesforce has opened up to you with code!
What we’ll do this chapter is build a trigger that dedupes leads with contacts. So if anyone tries to create a new lead that has the same email as an existing contact, we’ll give the user an error.
Why is this trigger the perfect template for all your triggers moving forward?
This chapter is the most important chapter in all of Sfdc99! So pay close attention and stay focused, because things are gonna get real fun!
Next post: Using Apex variables inside a SOQL query!