Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Apex for Admins Workshop
1. Apex Workshop for Admins
Salesforce WIT Trichy, India
Salesforce WIT Paris, France
June, 12th 2021
2.30PM (India)
11.00AM (France)
2. Bavadharani Ganesan
Salesforce Business system analyst
Trichy Women in Tech Group Leader
Doria Hamelryk
Salesforce Solution Architect
Paris Women in Tech Group Leader - MVP
8. You don’t have an org?
Use this link to create a new one: http://bit.ly/mysforg
Code resources :
All exercices available at: http://bit.ly/apexworkshopwit
9. 1st Part Query Editor
2nd Part Apex Anonymous
3rd Part Apex Triggers
4th Part Code optimization
AGENDA
24. As an administrator, I need to be able to create quickly Leads Test data.
Use case : Test Data creation
25. Introduction to Loops
This is a LIST
This is an INSTANCE
The LIST and the INSTANCE
are of the Type “Rose”
The LIST can be
decompose in several
INSTANCES
Each instance will have an
index representing its
position in the LIST
3
1
5
2
8
4
6 17
9
12
10
11
1 2 3
4 5 6
7 8 9
10 11 12
3
1
5
2
8
4
6 17
9
12
10
11
Going from one instance to the next one
in a LIST is what we call “iterate”.
An iteration is done with a LOOP
26. Introduction to FOR loops
Rose myFirstRose = new Rose (color='Red');
Rose mySecondRose = new Rose (color='Red');
...
List <Rose> myBouquet = new List <Rose> {myFirstRose, mySecondRose, ...};
For (Rose oneRose : myBouquet){
//do something with my oneRose
// do this until there is no rose left in myBouquet
}
Create the bouquet
Do something with each
rose in the bouquet
28. Use case : Test Data creation
List<Lead> : myLeads
Lead : aDev
=> myLeads[0]
Lead : anAdmin
=> myLeads[1]
Lead : aUser
=> myLeads[2]
Next Lead exists? Loop !
Next Lead exists? Loop !
Next Lead exists?
Nop! Exit !
Anonymous.txt
Exercice 1
36. As an administrator, I need to be able to replace all Leads having « CA » as
state by « California »
Use case : Update records with Apex Anonymous
37. Use case : Update records with Apex Anonymous
Anonymous.txt
Exercice 4
38. Use case : Update records with Apex Anonymous
Lead 1 administrator
Lead 2 administrator
Lead 3 developer
Lead 4 administrator
Lead 5 user
LastName
=
administrator?
List : UpdatedLeads
Lead 1 futureDeveloper
Lead 2 futureDeveloper
Lead 4 futureDeveloper
LEAD OBJECT
Lead 1 futureDeveloper
Lead 2 futureDeveloper
Lead 3 developer
Lead 4 futureDeveloper
Lead 5 user
Update
List : Query Results
39. Use case : Update records with Apex Anonymous
Anonymous.txt
Exercice 5
40. Use case : Update records with Apex Anonymous
Lead 1 administrator
Lead 2 administrator
Lead 4 administrator
Replace
LastName
List : Query Results
Lead 1 futureDeveloper
Lead 2 futureDeveloper
Lead 4 futureDeveloper
LEAD OBJECT
Lead 1 futureDeveloper
Lead 2 futureDeveloper
Lead 3 developer
Lead 4 futureDeveloper
Lead 5 user
Update
List : Query Results
43. Saving Records : automation order
Old Record
New Record
See also : https://sforce.co/3ghaFVy
44. Notion of Trigger.Old and Trigger.New
Old Values (Trigger.Old) New Values (Trigger.New)
Create Record
Update Record
Delete Record
45. Notion of LIST
List<Lead> myLeads = [Select id,LastName from Lead];
for(Lead leadRecord : myLeads){
// Loop in Records
}
Index Value
0 Lead {Id:00Q8A000005InhUUAS,LastName:James}
1 Lead {Id:00Q8A000005InhTUAS,LastName:Feager}
2 Lead {Id:00Q8A000005InhbUAC,LastName:Cotton}
myLeads
LeadRecord #1
LeadRecord #2
LeadRecord #3
leadRecord #1 = myLeads[0];
46. Notion of MAP
Map<Id,Lead> myLeads = new Map <Id,Lead>{[Select id,FirstName,LastName from Lead]};
for(Lead leadRecord : myLeads.values()){
// Loop in Records
}
Key Value
00Q8A000005InhUUAS Lead {Id:00Q8A000005InhUUAS,LastName:James}
00Q8A000005InhTUAS Lead {Id:00Q8A000005InhTUAS,LastName:Feager}
00Q8A000005InhbUAC Lead {Id:00Q8A000005InhbUAC,LastName:Cotton}
myLeads
LeadRecord #1
LeadRecord #2
LeadRecord #3
leadRecord #1 = myLeads.get('00Q8A000005InhUUAS');
47. As a developer, I need the display the old and the new values of my Record
with the Debug Logs.
Use case : display new & old values
49. Use of LIST in Triggers : Trigger.new
trigger LeadTrigger on Lead (before insert,before update) {
//Trigger.new = 1 or several Records with the new values - Used for creation and update
for(Lead rec : Trigger.new){
System.debug('new LastName >>'+rec.LastName);
}
}
Trigger.txt
Exercice 1.1
50. Use of LIST in Triggers : Trigger.old
trigger LeadTrigger on Lead (before insert,before update) {
//Trigger.new = 1 or several Records with the new values - Used for creation and update
for(Lead rec : Trigger.new){
System.debug('new LastName >>'+rec.LastName);
}
//Trigger.old = 1 or several Records with the old values - Used for update and delete
if(Trigger.old!=null) //check if we are in an update or delete event
for(Lead rec : Trigger.old){
System.debug('old LastName >>'+rec.LastName);
}
else System.debug('Trigger.old is null');
}
Trigger.txt
Exercice 1.2
51. Use of MAP in Triggers
trigger LeadTrigger on Lead (before insert,before update) {
//Trigger.new = 1 or several Records with the new values - Used for creation and update
for(Lead rec : Trigger.new){
System.debug('new LastName >>'+rec.LastName);
}
//Trigger.old = 1 or several Records with the old values - Used for update and delete
if(Trigger.old!=null) //check if we are in an update or delete event
for(Lead rec : Trigger.old){
System.debug('old LastName >>'+rec.LastName);
}
else System.debug('Trigger.old is null');
system.debug('oldMap contains : ');
for(String idOldRecord : Trigger.oldMap.keyset()){
system.debug('id : '+idOldRecord+'--- LastName : '+Trigger.oldMap.get(idOldRecord).LastName+')');
}
}
Trigger.txt
Exercice 1.3
52. As a developer, I need the Lead LastName to be saved in uppercase.
Use case : set the LastName to uppercase
53. Use case : set the LastName to uppercase
trigger LeadTrigger on Lead (before insert,before update) {
//Trigger.new = 1 or several Records with the new values
for(Lead rec : Trigger.new){ //for creations and updates
rec.LastName = rec.LastName.toUppercase();
}
//No need to update the record – new values will be commited to the database.
}
Trigger.txt
Exercice 2
57. Create a new Class
public class LeadHandler {
}
public class LeadHandler {
public static void upperCaseName(List<Lead> myLeads){
// TODO #2: make a Loop on the list leads, store the record in a variable named LeadRecord
for(Lead leadRecord : myLeads){
// TODO #3: assign to the field LastName the same value, in uppercase
leadRecord.LastName = leadRecord.LastName.toUppercase();
}
}
}
Trigger.txt
Exercice 3.1
58. trigger LeadTrigger on Lead(before insert,before update) {
//Trigger.new = 1 or several Records with the new values
LeadHandler.upperCaseName(Trigger.new); //call new method created
//No need to update the record – new values will be commited to the database.
}
Call the method from the Trigger
Trigger.txt
Exercice 3.2
59. As a developer, I need to split the behaviour of the Trigger between the
« Before Insert » and the « Before Update »
Use case : set the LastName to uppercase
Trigger
Before
Insert Update
After
Insert Update
60. trigger LeadTrigger on Lead (before insert,before update) {
if (trigger.isBefore) {
if(trigger.isUpdate){ // we want to manage the update with a ISCHANGED like
} else if (trigger.isInsert){ // will always be executed
}
}
}
Put the basic logic
Trigger.txt
Exercice 3.3
61. trigger LeadTrigger on Lead (before insert,before update) {
if (trigger.isBefore) {
if(trigger.isUpdate){ // we want to manage the update with a ISCHANGED like
} else if (trigger.isInsert){ // will always be executed
LeadHandler.upperCaseName(Trigger.new);
}
}
}
Call the method from the Trigger (insert)
Trigger.txt
Exercice 3.4
62. trigger LeadTrigger on Lead (before insert,before update) {
if (trigger.isBefore) {
if(trigger.isUpdate){ // we want to manage the update with a ISCHANGED like
List<Lead> leadToReallyUpdate = new List<Lead>();
for(Lead record : Trigger.new){
Lead oldRecord = Trigger.oldMap.get(record.Id);
if(record.LastName!=oldRecord.LastName){ // check if ISCHANGED
leadToReallyUpdate.add(record);
}
}
if(leadToReallyUpdate.size()>0) LeadHandler.upperCaseName(leadToReallyUpdate);
} else if (trigger.isInsert){
LeadHandler.upperCaseName(Trigger.new);
}
}
Call the method from the Trigger (update)
Trigger.txt
Exercice 3.5
63.
64. Erica Erika Fabrice Insa Julie
Samuel Sarah Thierry Yamini Yosra
Bavadharani Doria