Triggers for Admins: A Five-step Framework for Creating Triggers
-
Upload
salesforce-developers -
Category
Technology
-
view
652 -
download
0
description
Transcript of Triggers for Admins: A Five-step Framework for Creating Triggers
Triggers for Admins: A Five-step Framework for Creating Triggers(a play in 5 acts)Veronica Beck Ashima Saigal
@gandhilover
Place
Customer or
Partner logo in
white area of
slide, centered
Ashima SaigalDatabase Sherpa founder
Summer 2014 MVP
Place
Customer or
Partner logo in
white area of
slide, centered
Veronica BeckBigger Boat Consulting Senior Consultant
Setting
“We want our own custom campaign
member statuses, but it’s a pain to
have to add them every time we
create a campaign.” – Celeste, yoga
studio owner
Audience Participation
We have some suggestions:
● Breathe! You can do it.
● Replace trepidation with curiosity.
● Know this won’t happen overnight.
● Remember, you are not alone.
Triggers for Admins
Act 1: Where do I write code?
Act 2: Gather Your Resources
Act 3: Clarify Your Requirements
Act 4: Code and Test
Act 5: Finalize and Launch!
Act 1: Where do I write code?
Developer Org: https://developer.salesforce.com/gettingstarted
Act 1: Where do I write code?
Go to Setup.
Find the object where you want the trigger.
Click on Triggers.
Act 1: Where do I write code?
Act 1: Where do I write code?
Click the New button, and voila!
Act 1: Where do I write code?
Some other tools for writing code:
• Salesforce Developer Console
• Eclipse
[there are many more!]
● www.sfdc99.com
● Creator David Liu presents: “Anyone
can learn to code Apex!” On
Wednesday at 11 am
Act 2: Gather your resources
Act 2: Gather your resources
● Power of Us HUB (nonprofits)
http://www.salesforcefoundation.org/help/power-of-us-hub/
● Success Community: (Salesforce online community)
● Local User Groups (in person meetings)
● Developer User Groups (in person meetings)
● Network at Dreamforce!
Act 3: Clarify Requirements
● What problem are you trying to solve?
o Inconsistent campaign member statuses
● What needs to happen?
o Auto creation of desired member statuses
● When does it need to happen?
o When a new campaign is created
● Should it always happen for all records?
o Only for workshop type campaigns
Act 3: Clarify Requirements
Write it “In English” (aka pseudo code):
When a campaign is inserted
● Check if it’s a workshop campaign
● If it is a workshop campaign …
o Get rid of the default Campaign Statuses
o Add the following campaign statuses:
Invited (default)
Accepted (responded)
Attended (responded)
No Show
Act 4: Code and Test
● Recap
o Set up your coding environment
o Go through some tutorials
o Clarify what you’re trying to do
● Get ready, set, go!
● Search for code, copy and paste.
Act 4: Code and Test
Act 4: Code and Test
Act 4: Code and Test
Trigger.new contains all records that are being inserted or updated
Loop through the trigger.new collection
Do something with each record
Code Sample:
For (Campaign c : trigger.new){ // This is the beginning of the loop
//do something with each record
} //this is the end of the loop
Design Pattern 1: Use Trigger.New
Act 4: Code and Test
Act 4: Code and Test
Act 4: Code and Test
Act 4: Code and Test
Act 4: Code and Test
Act 4: Code and Test
Act 4: Code and Test
Act 4: Code and Test
Design Pattern 2: Use Lists and Sets
Lists and Sets are like a
bucket to hold records for
later processing
Act 4: Code and Test
Act 4: Code and Test
Act 4: Code and Test
Act 4: Code and Test
Act 4: Code and Test
Act 4: Code and Test
For (Campaign c : workshopCampaigns) { //start of the loop
CampaignMemberStatus myCMS = …..
myStuff.add(myCMS);
} // end of the loop
Insert myStuff ; Insert is happening AFTER the loop
Design Pattern 3: Inserts, Updates, Deletes Outside of Loops
Act 4: Code and Test
● Common design elements
o Loop through trigger.new (or trigger.old)
o Use collections to hold records to process
o Do inserts, updates, and deletes outside of your loops
● Other helpful Tips
o Comment your code!
o Use system.debug for troubleshooting
Act 5: Finalize and Launch
• Ask users to test with real-life scenarios
• Revise code, have users re-test
• Write test methods
– Sfdc99.com has examples and tutorial
– Force.com Apex Developer Guide
http://www.salesforce.com/us/developer/docs/apexcode/index_Left.htm
• Deploy!
– Use Salesforce change sets and deployment connections in Setup area
That’s a Wrap
Begin Your Coding Journey Today!
● Set up your FREE developer account
● Find a mentor – introduce yourself to at least three people and tell them
you’re looking for help with coding.
● Visit sfdc99.com.
● Celebrate the beginning of your journey!
Resources
Beginner Tutorial - http://www.sfdc99.com/beginner-tutorials/
Salesforce Developer Forum - https://developer.salesforce.com/forums/
Trigger Best Practices - http://www.iterativelogic.com/salesforce-apex-trigger-best-practices/
Trigger Order of Execution -
http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_triggers_order_of_execu
tion.htm
Writing Bulk Triggers - http://blog.jeffdouglas.com/2009/04/20/writing-bulk-triggers-for-
salesforce/
Force.com Apex Developer Guide -
http://www.salesforce.com/us/developer/docs/apexcode/index_Left.htm