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
lalit kumar 11lalit kumar 11 

basic difference between invoking apex in execute anonymous vs. unit tests

hi ,
 Can anyone guide me through the basic difference between invoking apex in execute anonymous vs. unit tests or if there is any specific document on this difference ? 


Thanks
lalit
Best Answer chosen by lalit kumar 11
Amit Chaudhary 8Amit Chaudhary 8
Executing code in anonymous window is just used to check the code flow or to check that code is working properly or not.
You can use anonymous blocks to quickly evaluate Apex on the fly, such as in the Developer Console or the Force.com IDE, or to write code that changes dynamically at runtime. For example, you might write a client Web application that takes input from a user, such as a name and address, and then uses an anonymous block of Apex to insert a contact with that name and address into the database

1)https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_anonymous_block.htm
2) https://developer.salesforce.com/docs/atlas.en-us.apex_workbook.meta/apex_workbook/apex1_2.htm

But unit test is required for code coverage. PLease check below post for test classes.
1) http://amitsalesforce.blogspot.in/search/label/Test%20Class
2) http://amitsalesforce.blogspot.in/2015/06/best-practice-for-test-classes-sample.html

Let us know if this will help you

All Answers

Amit Chaudhary 8Amit Chaudhary 8
Executing code in anonymous window is just used to check the code flow or to check that code is working properly or not.
You can use anonymous blocks to quickly evaluate Apex on the fly, such as in the Developer Console or the Force.com IDE, or to write code that changes dynamically at runtime. For example, you might write a client Web application that takes input from a user, such as a name and address, and then uses an anonymous block of Apex to insert a contact with that name and address into the database

1)https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_anonymous_block.htm
2) https://developer.salesforce.com/docs/atlas.en-us.apex_workbook.meta/apex_workbook/apex1_2.htm

But unit test is required for code coverage. PLease check below post for test classes.
1) http://amitsalesforce.blogspot.in/search/label/Test%20Class
2) http://amitsalesforce.blogspot.in/2015/06/best-practice-for-test-classes-sample.html

Let us know if this will help you
This was selected as the best answer
Shaijan ThomasShaijan Thomas
Please see few analysis

                Anonymous                                                                                                                       Unit Test
-----------------------------------------------------------------------------------------------------------------------------------------
1.            Record creation will store in DB                                                        Record creation – will not store
2.            Enforce user permission                                                                  Default system permission
3.            Output verify in Log                                                                        Coverage will calculate