22
Apex Ten Commandments Francis Pindar @radnip Cloud Sherpas

Salesforce Apex Ten Commandments

Embed Size (px)

DESCRIPTION

Learn the top 10 things you must know when developing using Salesforce APEX!

Citation preview

Page 1: Salesforce Apex Ten Commandments

Apex Ten Commandments Francis Pindar@radnip Cloud Sherpas

Page 2: Salesforce Apex Ten Commandments
Page 3: Salesforce Apex Ten Commandments

#1 Thou shalt not put queries in ‘for’ loops

Page 4: Salesforce Apex Ten Commandments

#1 Thou shalt not put queries in ‘for’ loops

Page 5: Salesforce Apex Ten Commandments

#2 Thou shalt not put DML in ‘for’ loops

Page 6: Salesforce Apex Ten Commandments

#2 Thou shalt not put DML in ‘for’ loops

Page 7: Salesforce Apex Ten Commandments
Page 8: Salesforce Apex Ten Commandments
Page 9: Salesforce Apex Ten Commandments

#3 Thou shalt have a happy balance between clicks & code Code that replicates declarative functionality

– Roll-up Summary– Workflows– Email Templates– Global Settings– …

Good Salesforce Architectural Design

Page 10: Salesforce Apex Ten Commandments

#4 Thou shalt only put one trigger per object

Page 11: Salesforce Apex Ten Commandments

#5 Thou shalt not put code in triggers other than calling methods and managing execution order

Page 12: Salesforce Apex Ten Commandments

#6 Thou shalt utilize maps for queries wherever possible

Page 13: Salesforce Apex Ten Commandments

#7 Thou shalt make use of relationships to reduce queries wherever possible

Page 14: Salesforce Apex Ten Commandments

#8 Thou shalt aim for 100% test coverage

In general test your methods for:– Positive effects.

• Given proper input it should act like this.• Not just happy path, but all logic branches.

– Negative effects.• Given bad data it should error like this.

– Role/Profile/User effects• Given a user with X profile and Y role it should act like this.

Page 15: Salesforce Apex Ten Commandments
Page 16: Salesforce Apex Ten Commandments
Page 17: Salesforce Apex Ten Commandments
Page 18: Salesforce Apex Ten Commandments

#9 Thou shalt write meaningful and useful tests It’s not a test without assertions.

– Assert(A==B, “Assert failed comment”)– AssertEquals(A,B, “Assert failed comment”)– AssertNotEquals(A,B, “Assert failed comment”)

Page 19: Salesforce Apex Ten Commandments

#9 Thou shalt write meaningful and useful tests Test with your own data

– Use Test.StartTest() / Test.StopTest()

Page 20: Salesforce Apex Ten Commandments

#9 Thou shalt write meaningful and useful tests Test one thing at a time

– Maintain Focus– Just One Thing!

Page 21: Salesforce Apex Ten Commandments

#10 Thou shalt limit future calls and use asynchronous code where possible In general bias towards batch apex

– Ensure it runs as efficiently as possible. • Limit and tune time outs for callouts.• Tune soql queries for efficiency – dev console can help

here.– If you need @future methods.

• Optimize them the same way you would optimize your batch apex.

• Resist methods that would queue many @future calls at once. (governor limits!)

Page 22: Salesforce Apex Ten Commandments

Thanks… Questions?How to find me:Ask an Expert – 3pm to 6pmTwitter: @radnip