Create own Code Coverage Solution!

A few years ago, Salesforce removed Code Coverage from the Trigger and Class GUI screens. Encouraging you to go to Developer Console and view from there. But it is a small panel, and not easy to find the ones you are looking for.

But what if you could view it in the GUI again? And while you were at it, made the columns sortable? And create a matrix of those that are <75% coverage, just over, or safely over? While we are at it, we'd like to be able to export it to Excel too! Gee, you want dinner with that too? Well, all of this is possible by using VisualForce and the Metadata API. Let's take a look...

Solution Background

We're not going to get into the nuances of the entire solution. That would take too long, and what fun would that be? Instead, we'll talk about the highlights, and then provide the code for the 2 pages and 3 classes.

Requirements

As mentioned in the beginning, we want the below:

  • Show us all Classes/Triggers and their coverage.
  • Make the columns sortable.
  • Have a matrix so that I know how many classes/triggers are within/outside standards.
  • Make this representation exportable to Excel.

So, essentially, our solution needs to look like:

No alt text provided for this image

So, first things first. Let's look at the Code Aggregate Class.

Code Aggregate Class

This is just a storage class to hold data representations and to return the data returned from the Metadata call properly. Later, we'll see the code.

Now we need to CONSUME this class. We're now ready for the Controller Class.

Controller Class

We have obviously a bunch of unremarkable properties and variables to create. But, there are a couple of key methods that drive the solution.

codeCoverage - this is both a constructor and a method that gets called from the constructor. It re-initializes the totals, gets the current coverage from the ApexCodeCoverageAggregate object, makes the request, and feeds the applicable properties/variables. In others, all of the heavy lifting is done here.

Within this class is where the compares for coverage is being accomplished and stratified. The "limits" for that coverage (i.e. <75%, 75-85, etc) are hard-coded. This could easily be moved to a Custom Setting so that you could dynamically change the aggregation if desired. For us, the above limits seem to work well to focus on where work should be targeted.

apexWrapper - this class is both a wrapper to represent the coverage results, and allows each of the columns to be sortable.

Later, we'll see the code.

Now that we have a Controller, we need VisualForce page to represent the logic.

VisualForce Page

This page has a number of unremarkable style definitions and then maps out the page. As you might guess, the work should be done with repeats and/or tables. In this case, the Controller is readying the data allowing the page to handle GUI interactions which it does well. This page is rather simple and straight-forward so not much discussion is needed.

Generate Coverage to Excel

Some of you may not be familiar with how to do this, but it's really pretty simple. You create a bunch of styles for the desired styling of the sheet (if desired). The actual HTML and APEX after that is really quite simple. The KEY is generating it as a text/XML file and then giving the filename an XLS extension as noted in the first line. Yes, Excel natively knows how to open an XML formatted file.

Later, we'll see the code.

Test Class

Of course, you need a Test Class to handle all of this. The link later will give you an example of one, but you’ll probably want to make it more encompassing with Asserts, etc.

Conclusion

Is that it? No, you might have some things you want to personalize it with. Include Namespaces, change coverage limits, add email or task notifications, etc. In addition, you need to probably create a Remote Site settings entry so that your page is able to call out. Remember, with the Metadata API, you are actually making a callout to yourself. For example, mine looks like:

https://c.cs17.visual.force.com

and I have another in another instance that looks like:

https://citrite--fdev--metadatasearch.cs93.visual.force.com

Yours might be different depending on the instance, domain configuration, etc.

This, of course, gets you going. The fun comes from customizing it the way you want. Give it a shot. Once you have it and run it in Production, you can manage your APEX so much more quickly and focus your work.

Download!

Wait! Didn't I mention we'll see the code later? Yes, I did. BUT, LinkedIn doesn't support so many characters in a "Snippet". Therefore, I've created a Dropbox folder with the files that you can get by going to this location below. Happy coding!


Thanks for sharing Robert!

Like
Reply

Woah. Thanks for sharing.

Like
Reply

“Gee, you want dinner with that too?” Thanks for writing and publishing, Robert.

To view or add a comment, sign in

More articles by Robert Nunemaker

  • APEX Triggers using pattern EDA3

    Introduction We all have triggers to create and most of us know some of the best practices around them. But creating…

    1 Comment
  • Create a YAML file for an APEX REST Class

    Introduction With so many web services written with APEX being done in REST and fewer in SOAP, we need to be able to…

    2 Comments
  • Get All Permutations (Combinations) Using APEX

    Huh? Permutations? Yeah, every industry likes to have their own words, right? Instead of the $5 word, we can just say…

  • (Re)Discover the Power of Salesforce Custom Permissions

    Custom Permissions are one of those obscure capabilities of the Salesforce platform that is not only under-appreciated,…

    5 Comments
  • The Power of "Why"!

    (For Non-Coders and Coders alike!) Introduction Often times we need to design solutions with incomplete information. It…

    1 Comment
  • Change Test Class Batch Size Dynamically

    Previously, we've discussed best practices for developing Test classes. And, as we all know, we should be testing in…

  • Test Class Best Practices - A Pattern

    Introduction – Why and What? As we all know, Test Classes are integral to the success of any APEX coding. Moreover, the…

  • Custom Labels, Settings, Metadata - which should I use?

    Salesforce is chock full of capabilities that allow a much more customized experience. Among these are Custom Labels…

    1 Comment
  • Upserting and Working with Custom Metadata Types

    Discussion Previously, we discussed how to use Custom Metadata Types in conjunction with generic sObjects to provide a…

    4 Comments
  • Custom Metadata & sObjects for full Generic Reads and Updates

    Discussion Ever wish you could create a service that would be able to read and/or write to a given object, but provide…

    1 Comment

Others also viewed

Explore content categories