+ Start a Discussion
kotipalli srikanthkotipalli srikanth 

'Apex class' vs ' Anonymous window code '

Hello, SFDC people.
I am a beginner,
My doubt is that we write the program using APEX Class and also as well as through Anonymous window, what is the difference in writing code on either of them.
please explain me, Thank you.
Abdul KhatriAbdul Khatri
Apex Class
  • They are stored as metadata in Salesforce.
  • You can move them between orgs (Sandbo to Production and vice versa, Sandbox to Sandbox etc)
You can read this for details

Anonymous window:
  • It helps you to execute any apex code (logic) that you would like to make it a part of your apex class.
  • It helps you execute your class method in an Apex Class in isolation for generating log.
  • They are not basically stored as a file or meta data so once removed they are gone. It is just a way temporarily way running your apex code, apex class etc. to see the results.
  • You cannot move them between different org (Sanbox to Production and vice versa, Sandbox to Sandbox) etc.

For details you can follow the below URL

Based on above, answering to your question. You use Apex Classes to write a program that can be called from either visualforce page, triggers etc.

I hope it would have helped you.
mukesh guptamukesh gupta
Hi srikanth,

Anonymous window code: here we can excute the code by EXECUTE button, example Create a accout object and

Account acc = Account();
acc.name = 'test';
insert acc;

after this code you need to click on execute button, sothat new account will be insert. 


apex class code is dependent with visualforce page, apex trigger, process builder.  Apex class can be excute by Anonymous window code for this first need to create this class object in Anonymous window and click Excute button.

Kindly mark my solution as the best answer if it helps you.

Out of experience:
 - You can't write controllers/extensions in anonymous window.
 - Apex classes needs to go through testing and deployment offcourse
 - Anonyomous window can be used for debuging a existing code, creating and testing a simple logic, can be used to update/analyze data but this should be done carefully.
More over Apex Class can be used by within number of Visualforce pages, Lightning Components,  Apex Triggers. They can also be a Batch Classes or Schedulable Classes themselves. Such possibilities only with CLASS.. Apex Class. Something like Bond.. Games Bond.

Anonymous window is friend of developers.