Webinar du 28 mai 2020 du groupe Salesforce Paris Women in Tech. Plus d'info :
https://trailblazercommunitygroups.com/events/details/salesforce-paris-france-women-in-tech-group-presents-ecrire-son-premier-trigger-et-les-comprendre/
#sfpariswit
4. Agenda
1. What’s an Apex Trigger ?
2. When to use Apex Trigger?
3. What are types of Trigger?
4. Basic Syntax of Trigger
5. Difference between Trigger.New and Trigger.Old
6. Trigger example
7. Summer ’20 : New Flows Features
8. Quiz
5. Apex : langage de programmation utilisé sur la plateforme Salesforce.
Script : portion de code permettant d’effectuer un traitement spécifique.
Method : script identifié par un nom, inclus dans une classe, exécutable depuis
n’importe où dans d’autres scripts.
SOQL (Salesforce Object Query Language) : langage permettant d’accéder aux
enregistrements dans les objets.
Query : instruction écrite dans le langage SOQL (sélection d’enregistrements)
Before we begin, a bit of vocabulary
7. What’s an Apex Trigger?
A trigger is an Apex script that executes before or after data
manipulation language (DML) events occur. Apex triggers enable you to
perform custom actions before or after events to record in Salesforce,
such as insertions, updates, or deletions.
8. DML (Data Manipulation Language) : opération permettant de faire des…
Insert/Update : insérer / mettre à jour
Upsert : mettre à jour les enregistrements s’ils existent, et ajouter les nouveaux,
dans une même opération
Delete/Undelete : supprimer un enregistrement / annuler la suppression
Merge : fusionner deux enregistrements
What is a DML?
10. The benefit of using Apex Trigger
● Perform operations according to specific conditions
● Prevent certain operations from being executed
● Modify associated records
● Execute all possible actions in Apex, including executing SOQL
and DML queries
● Call custom Apex methods.
11. •It is a piece of code which is executed either before or after a
record is updated or inserted.
•150 DML operations can be used in a single trigger.
•50 000 is maximum number of records retrieved by SOQL
queries
•You can access triggers across an object and related to that
object.
Salesforce Trigger
12. Salesforce Trigger vs. Workflows
•Workflows cannot create or update a separate object.
•You can’t reference certain fields when using
workflows.
•You will not have your workflow doing more than just
field updates and emails.
13. Salesforce Trigger vs. Process Builder
•Process Builders cannot handle before DML It executes
after a record has been created or updated. Whereas Apex
triggers can handle both before and after DML operations.
•Process Builder cannot handle delete and undelete DML.
Whereas Apex triggers can handle all DML operations.
15. Types Of triggers
Before Trigger After Trigger
is used to either update or
validate the values of a
record before they can be
saved into the database.
is used to access the field
values set by the system
and affect any change in
the record.
18. Basic Syntax of Trigger
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
21. Context variables
What’s context variable ?
→ provide access to the records that caused the trigger fired
Trigger.new Trigger.old
Contains all the records
that were inserted in insert
or update triggers
Provides the old version of
sObjects before they were
updated in update triggers, or
a list of deleted sObjects in
delete triggers
22. Context variables
Triggers fired when one
record or many records
are inserted/updated
Context variable
can contain one or
many records
You can iterate
over Trigger.New
to get each
individual sObject
Ex 1: Insert one account → Trigger.new contain one record → Iteration on one record
Ex 2: Insert 10 accounts → Trigger.new contain 10 records → Iteration on 10 records
X10
23. How to write a trigger ?
Ex from developer console
28. Need to deactivate opportunities related to an
inactive account
We want that when the Account active field changes to “No”, we deactivate
all related opportunities
Account
Active : Yes ⇒ No
Opportunity 1
Active : Yes ⇒ No
Opportunity 2
Active : Yes ⇒ No
30. Update on Account
(field Active__c : Yes → No)
Iteration on updated record
Active field of updated
record = ‘No’ ?
If ok → Add account Id in a
list
Get Account Id
Get related opp
Get all related opportunities
in a list
Update active field on Opp
Iteration on all related Opp
Change value of active field
on Opp
Add updated Opp to a list
Update the list
31. In Summer ’20 : New Flows Features
In Summer ’20 there is a couple of major updates; firstly you can now trigger a flow after a record is
saved, in the same way a Before Trigger works in Apex.
You also have an easy way to kick off a Flow, with the new and improved Start Element.
Flow is receiving a huge amount of attention
from Salesforce, creating a declarative tool
that allows Admins to build Apex like
functions.
34. Upcoming Meeting
Thu, Jun 11, 6:00 PM (CEST)
Intégration Continue et Couverture de Test Avec
GitHub Actions et Codecov.io
Paris, FR Developers Group
https://trailblazercommunitygroups.com/events/details/salesforce-paris-fr-developers-group-presents-integration-continue-et-
couverture-de-test-avec-github-actions-et-codecovio/#/