Monday, January 7, 2013

Best Practices - Salesforce

1. Whenever writing test methods (for any test class) make sure test data is created from a single location. Factory pattern is the example.  By following this way, if any validations are added to an object, same can be tackled without breaking any code.

2. Try using standard set controller pagination option provided by Salesforce when doing pagination. Now we have also got OFFSET keyword which enables pagination using soql Query.

3. Only one trigger should be present for an Object. This helps us in having control over the execution of trigger.

4. Never write business logic in the trigger. Always delegate it to other classes

5. Use SOQL for loop while iterating over large amounts of data. This helps in keeping heap size in check. 

6. If you are sure that you need to have a map with Id as key and Value as the respective SObject, consider using Map soql Query syntax. This helps in eliminating lots of code and thus saving lot of script statements.
Example Map<Id,Account> m= new Map<Id,Account>([select id from Account])

7. While in trigger if there is an expection, never try to catch and suppress that exception. Always throw it to the main class. It is the responsibility of the main class to analyze that exception and decide whether to suppress that or ask for user intervention

8. I suggest maintaining an exception table and logging all those exceptions in that table. This helps us in debugging while working on Namespace orgs

9. Use transient keyword liberally in visual force pages. This helps in reducing the view state of the page. Especially in the case of document uploading use of transient is a must.

10. Why we write test methods-Because Salesforce test their new release code against the test methods to make sure new version doesn’t break old version. So if there are no assertions then Salesforce doesn’t know if the code is working or not. So please make sure assertions are done in test methods. It helps a lot in regression testing.

11. While writing dynamic queries please be sure if it works if uploaded to a namespace org. This problem could occur when using Maps in visual force pages. This would work in developer org ,but fields need to be appended with proper namespace when working on packaged orgs

12. When working on Visual force pages, all CSS should be on the top of the page and the script tags at the bottom of the page. This helps in improving the performance of the pages.

13. Always consider using cache attribute on the V.F page if the page is not dynamic in nature.

14. Even when a property on the vf page is set as private it will be a part of Visual force page view state though not accessible to the page. So, please avoid using private properties if not necessary.

15. It is not advisable to use soql queries in getters and setters. This would hamper the performance of the page.

16. While working on workflows, make sure fields on workflow rule are not dependent on another workflow rule. If there is an overlap, system doesn’t have any control on which workflow executes first and which next.

17. When using variables which are not local to a method, and not needed for further processing make that value as null. This would release a lot of heap size. Batch is one of the places where we should consider this.

18. While using batch apex, only get the ids in the query locator method. Other fields required for query should be queried later in the execute method. There is check on the time taken for the query locator to execute. Hence this best practice.

19. When using email service to call an internal Salesforce class, make sure subject is consistent in sandbox and production orgs. Doing some kind of processing based on subject should not break any code. If there is any discrepancy, put your subject into a predefined bracket and search for that position to extract exact information to do further processing.

20. While doing any data load take care of the fact that it doesn’t trigger any unnecessary emails to the recipients. Even when some of the time based workflows are deactivated, items already present in the time trigger queue will be processed. So care should be taken they are also removed from the queue. 

21. When working on VF pages, use void methods for in-between Ajax calls and Page Reference method for final call. When using page reference return type on complete attribute doesn’t work as the system assumes that code redirects the page to some other url.

22. If maintaining view state on the page is not a main criterion, always go with Java script remoting. Java script remoting is fast, and  developer has better control on the results that are returned

23. Use Firebug mode on Firefox, or developer tools on chrome to understand the whole life cycle of the page. It also helps in debugging browser compatibilities. 

24. Always use enums and labels while working with exceptions.

25. Load all the data required for processing into the memory and then process the data already available in the memory. This helps in reducing the number of soql queries used. Practice using relationship queries. Triggers is one place where most of the refactoring of the code can be done using this approach

26. Reverse engineering always helps in better assessing the best way to accomplish the things. Let’s say there is some account data that need to be validated against the existing data belonging to the same object. 
One way of doing this is writing multiple queries with where clause changing all the time. Other way is use a single query get all the required data, then match just against the fields. This approach can be used in survey deployment module where multiple queries are used to check which survey should be deployed to the user. Because of this only 100 surveys can be deployed to a user.

27. Consider using apex: actionsupport if it does your work. Never go with apex: action function. There is performance related mechanics that makes apex: actionsupport the best bet.

28. Query only the fields that are required. Never query unnecessary fields.

29. Master Map, set and list. These are the building blocks of Salesforce.

Sunday, September 23, 2012

Dev 401 Certification Mock Exam 4


1) Apex is a Object based language ?
A. True
B. False

2) Which type of application can be created using the Flex Framework for Force.com? Choose 3 answers
A. Browser-based Flash applications
B. Apex applications
C. Disconnected desktop applications
D. Connected desktop applications
E. VMforce applications

3) The showChatter attribute is available on which Visualforce component?
A. apex:chatter
B. apex:pageBlock
C. apex:detail
D. apex:page


4) A sales manager would like to view a dashboard from the perspective of different users and switch between users without editing the dashboard. How would an administrator enable this?
   Choose 2 answers
A. Create the dashboard as a dynamic dashboard.
B. Grant the sales manager the "Manage Dynamic Dashboards" permission.
C. Grant the sales manager the "Drag-and-Drop Dashboard Builder" permission.
D. Grant the sales manager the "View My Team's Dashboards" permission.

5) What can users do when Chatter feed tracking is enabled for dashboards?
   Choose 2 answers
A. Follow files and links for a dashboard.
B. Auto-follow dashboards created by the user.
C. Follow posts and comments for the dashboard source reports.
D. Follow posts and comments for a dashboard.

6) Which combination of objects is available when creating a custom report type for Chatter reports?
  Choose 2 answers
A. Chatter Groups, Members
B. Opportunities, Followers, User Feed
C. Accounts, User Feed, Comments
D. Users, User Feed, Comments

7) Which is a capability of the improved System Log console?
  Choose 3 answers
A. View feedback on usage against governor limits
B. Run as a specific user
C. Execute anonymous blocks
D. Generate a dialog box when a usage limit is reached
E. View a stack trace

8) Which is a capability of Visualforce components for Chatter?
  Choose 2 answers
A. Include a Chatter feed on a record detail.
B. Include Chatter followers on a record detail.
C. Include Chatter recommended people to follow on a record detail.
D. Include a list of Chatter-enabled fields on a record detail.

9) Which technology is used to create applications with the Adobe FlashBuilder for Force.com?
  Choose 3 answers
A. Java
B. MXML
C. Flex
D. Force.com Web Services API
E. Apex

10) Which function is available in the report builder interface, prior to running the report?
  Choose 2 answers
A. Printable view
B. Schedule future runs
C. Save
D. Export details
E. Show/hide details

11) Which is a consideration when creating a drill-to-detail dashboard component?
A. Drill-to-detail must be enabled on the custom report type.
B. The dashboard component type must be a gauge.
C. The source report must be grouped by record name, record owner, or feed post.
D. The source report must be stored in the drill-to-detail folder.

Answers:
1) A
2) A,C,D
3) C
4) A,D
5) A,D
6) A,D
7) A,C,E
8) A,B
9) B,C,D
10) C,E
11) C

Monday, September 17, 2012

How to call PageReference Method on page load

Using "action" attribute of Apex:page tag, we can call a page reference method that will return the new page reference or redirect to the new page.

Example:
<apex:page controller="TestController" action="{!checkAndRedirect}"...></apex:page>
Controller:
Public Class TestController{  
    public PageReference checkAndRedirect()
    {    
        // do checks    
        //return new pagereference('/apex/testVisualForcePageName'); //return a new PageReference    
        //       or    
        //return null to go back to the current page  
    }
}

Note: We cannot return page reference from the constructor.

Friday, September 14, 2012

Salesforce tabStyle attribute in apex

When you use a custom controller, the tabStyle attribute of an <apex:page> tag allows you to mimic the look and feel of the associated Salesforce page. If you only want portions of the page to be similar to a Salesforce page, you can use the tabStyle attribute on the <apex:pageBlock> tag

tabStyle in <apex:page> tag:
<apex:page standardController="Account" showHeader="true" tabStyle="account" >
For a custom tab we have to add "__tab" after the tab name.
Example: <apex:page standardController="Account" showHeader="true" tabStyle="CustomTab_Name__tab" >

Limitation: Salesforce does not support assigning tabStyle dynamically like below.
<apex:page standardController="Account" showHeader="true" tabStyle="{!vartabStyle}" >

Arithmetic operators not allowed in SOQL queries

Salesforce SOQL queries does not directly support arithmetic operations like below:

SELECT (FieldA+FieldB) FROM Account
SELECT (FieldA+FieldB)*100 FROM Account

We have to use formula fields to achieve this type of requirements.

Simply create a formula field and use it direclty in the query like below:

SELECT FormulaFieldA FROM Account