Get your Salesforce project stats using Apex

I mentioned recently that I was working an a rather large project and posted some stats on how many classes, custom objects, etc were involved. Of course, I didn’t count those by hand. I wrote a little script that I run via Execute Anonymous either in Eclipse or in the System Log. Thought I’d share that out.

In the code below I filter out any managed packages because I just wanted stats on stuff that I had done. Feel free to modify the queries to your specific needs.


Integer triggerCount = 0;
Integer codeLines = 0;
Integer classCount = 0;
Integer componentCount = 0;
Integer pageCount = 0;
Integer customObjectCount = 0;
for (ApexTrigger t : [Select a.NamespacePrefix, a.Name, a.LengthWithoutComments From ApexTrigger a where NamespacePrefix = null]){
codeLines += t.LengthWithoutComments;
for (ApexClass a : [Select a.NamespacePrefix, a.Name, a.LengthWithoutComments From ApexClass a where NamespacePrefix = null]){
codeLines += a.LengthWithoutComments;
for (ApexComponent c : [Select a.NamespacePrefix, a.Id From ApexComponent a
From ApexComponent a where NamespacePrefix = null]){
for (ApexPage p : [Select a.NamespacePrefix, a.Id From ApexPage a
From ApexPage a where NamespacePrefix = null]){
Map gd = Schema.getGlobalDescribe();
for (Schema.SObjectType s : gd.values()){
Schema.DescribeSObjectResult r =s.getDescribe();
if (r.isCustom()){
system.debug('---------Project Stats---------')
system.debug( 'triggerCount = ' + triggerCount);
system.debug( 'codeLines = ' + codeLines);
system.debug( 'classCount = ' + classCount);
system.debug( 'componentCount = ' + componentCount);
system.debug( 'pageCount = ' + pageCount);
system.debug( 'customObjectCount = ' + customObjectCount);

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s