function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
sfdc startsfdc start 

best pratices..........

1.plz answer best pratice of TESTCLASSES
2.plz answer best pratice of TRIGGERS
3. APEX CLASS
Amit Chaudhary 8Amit Chaudhary 8
1.plz answer best pratice of TESTCLASSES
Please check below post for test classes
1) http://amitsalesforce.blogspot.com/search/label/Test%20Class
2) http://amitsalesforce.blogspot.com/2015/06/best-practice-for-test-classes-sample.html


Please follow below salesforce Best Practice for Test Classes :-

1. Test class must start with @isTest annotation if class class version is more than 25
2. Test environment support @testVisible , @testSetUp as well
3. Unit test is to test particular piece of code working properly or not .
4. Unit test method takes no argument ,commit no data to database ,send no email ,flagged with testMethod keyword .
5. To deploy to production at-least 75% code coverage is required
6. System.debug statement are not counted as a part of apex code limit.
7. Test method and test classes are not counted as a part of code limit
9. We should not focus on the  percentage of code coverage ,we should make sure that every use case should covered including positive, negative,bulk and single record .
Single Action -To verify that the the single record produces the correct an expected result .
Bulk action -Any apex record trigger ,class or extension must be invoked for 1-200 records .
Positive behavior : Test every expected behavior occurs through every expected permutation , i,e user filled out every correctly data and not go past the limit .
Negative Testcase :-Not to add future date , Not to specify negative amount.
Restricted User :-Test whether a user with restricted access used in your code .
10. Test class should be annotated with @isTest .
11 . @isTest annotation with test method  is equivalent to testMethod keyword .
12. Test method should static and no void return type .
13. Test class and method default access is private ,no matter to add access specifier .
14. classes with @isTest annotation can't be a interface or enum .
15. Test method code can't be invoked by non test request .
16. Stating with salesforce API 28.0 test method can not reside inside non test classes .
17. @Testvisible annotation to make visible private methods inside test classes.
18. Test method can not be used to test web-service call out . Please use call out mock .
19. You can't  send email from test method.
20.User, profile, organization, AsyncApexjob, Corntrigger, RecordType, ApexClass, ApexComponent ,ApexPage we can access without (seeAllData=true) .
21. SeeAllData=true will not work for API 23 version eailer .
22. Accessing static resource test records in test class e,g List<Account> accList=Test.loadData(Account,SobjectType,'ResourceName').
23. Create TestFactory class with @isTest annotation to exclude from organization code size limit .
24. @testSetup to create test records once in a method  and use in every test method in the test class .
25. We can run unit test by using Salesforce Standard UI,Force.com IDE ,Console ,API.
26. Maximum number of test classes run per 24 hour of period is  not grater of 500 or 10 multiplication of test classes of your organization.
27. As apex runs in system mode so the permission and record sharing are not taken into account . So we need to use system.runAs to enforce record sharing .
28. System.runAs will not enforce user permission or field level permission .
29. Every test to runAs count against the total number of DML issued in the process .

2.plz answer best pratice of TRIGGERS
1) http://amitsalesforce.blogspot.com/search/label/Trigger
2) http://amitsalesforce.blogspot.com/2015/06/trigger-best-practices-sample-trigger.html

Trigger Best Practices | Sample Trigger Example | Implementing Trigger Framework

1) One Trigger Per Object
A single Apex Trigger is all you need for one particular object. If you develop multiple Triggers for a single object, you have no way of controlling the order of execution if those Triggers can run in the same contexts

2) Logic-less Triggers
If you write methods in your Triggers, those can’t be exposed for test purposes. You also can’t expose logic to be re-used anywhere else in your org.

3) Context-Specific Handler Methods
Create context-specific handler methods in Trigger handlers


4) Bulkify your Code
Bulkifying Apex code refers to the concept of making sure the code properly handles more than one record at a time.

5) Avoid SOQL Queries or DML statements inside FOR Loops
An individual Apex request gets a maximum of 100 SOQL queries before exceeding that governor limit. So if this trigger is invoked by a batch of more than 100 Account records, the governor limit will throw a runtime exception

6) Using Collections, Streamlining Queries, and Efficient For Loops
It is important to use Apex Collections to efficiently query data and store the data in memory. A combination of using collections and streamlining SOQL queries can substantially help writing efficient Apex code and avoid governor limits

7) Querying Large Data Sets
The total number of records that can be returned by SOQL queries in a request is 50,000. If returning a large set of queries causes you to exceed your heap limit, then a SOQL query for loop must be used instead. It can process multiple batches of records through the use of internal calls to query and queryMore

8) Use @future Appropriately
It is critical to write your Apex code to efficiently handle bulk or many records at a time. This is also true for asynchronous Apex methods (those annotated with the @future keyword). The differences between synchronous and asynchronous Apex can be found

9) Avoid Hardcoding IDs
When deploying Apex code between sandbox and production environments, or installing Force.com AppExchange packages, it is essential to avoid hardcoding IDs in the Apex code. By doing so, if the record IDs change between environments, the logic can dynamically identify the proper data to operate against and not fail

3. APEX CLASS
1) https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_defining.htm
2) https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_qs_class.htm

Let us know if this will help you
sfdc startsfdc start
Schema Builder and uses
Amit Chaudhary 8Amit Chaudhary 8
Please check below trailhead module
1) https://trailhead.salesforce.com//en/data_modeling/schema_builder

Schema Builder provides a dynamic environment for viewing and modifying all the objects and relationships in your app. This greatly simplifies the task of designing, implementing, and modifying your data model, or schema.

You can view your existing schema and interactively add new custom objects, custom fields, and relationships, simply by dragging and dropping. Schema Builder automatically implements the changes and saves the layout of your schema any time you move an object. This eliminates the need to click from page to page to find the details of a relationship or to add a new custom field to an object in your schema.

Schema Builder provides details such as the field values, required fields, and how objects are related by displaying lookup and master-detail relationships. You can view the fields and relationships for both standard and custom objects.

Schema Builder is enabled by default and lets you add the following to your schema:
  • Custom objects
  • Lookup relationships
  • Master-detail relationships
  • All custom fields except: Geolocation

 
ArunKumar KumkumathArunKumar Kumkumath
Can you please help me out on this..? I am trying to write a test class for a simple trigger. But some of the lines (italics) are not covered under the code coverage. Can anyone please guide me... Also i have not written any test classes for this trigger but it is showing coverage without a test class..?

trigger AccountDuplicateTrigger on Account (before insert)
{        
    for(Account a:Trigger.new)    
    {
        if(Trigger.isInsert)
        {                   
            if(a.Name !='') 
            {       
                string dupItem = '';
                List<Account> acc= new List<Account>();                   
                if((a.BillingStreet != null) && (acc.isEmpty())) 
                {
                    acc=[select ID from account where Name=:a.Name and BillingStreet=:a.BillingStreet]; 
                    if(acc.isEmpty()==False)                                                              
                        dupItem='Name \''+ String.escapeSingleQuotes(a.Name)+'\' and Billing street \''+
                        String.escapeSingleQuotes(a.BillingStreet)+'\'';

                }
                if((a.BillingCity != null) && (acc.isEmpty())) 
                {
                    acc=[select ID from account where Name=:a.Name and BillingCity=:a.BillingCity];    
                    if(acc.isEmpty()==False) 
                        dupItem='Name \''+ String.escapeSingleQuotes(a.Name)+'\' and Billing city \''+
                        String.escapeSingleQuotes(a.BillingCity)+'\'';

                }                                      
                else{                
                }
                
                //Alerts the user                    
                if(acc.isEmpty()==False) {
                    a.adderror('You cannot create a duplicate account here as the '+dupItem+' already belongs to a different account.');     
                }

            }
        }
    }
}