Friday, September 30, 2016

Starting related test cases for a given Apex test class

Way back in the day (v27.0 and earlier) it was possible to inline the test cases directly with the apex class. This wasn't always ideal, but it did have one big advantage. Finding the test cases for a class was super easy - they were right there in the class!

This functionality was torn away from us in Summer '13 (v28.0) with the Apex Test Code Segregation act. Maybe that phrasing was a bit melodramatic. The change was actually for the better and heralded in improvements to Apex performance. It did leave a gap with finding the applicable test classes for a given class. This was partially acknowledged with the following planned improvement:

"Test-to-Parent class linking – You will be able to have a test class explicitly related to regular class or trigger. Currently, many of you have implicit connections by adding a suffix or prefix to a class name, but this doesn’t make managing or interacting with your codebase all that much easier. The explicit linkage will allow us to do better UI treatments for related classes. A direct mapping will also make the upcoming Fast Deploy feature much easier to utilize."

Unfortunately for us developers I think the safe harbour this one pulled up in is a bit too comfortable. Since 2013 it's been sitting on a golden beach sipping a cold drink without a care in the world. Maybe we will see it one day.

Back to the now, I've been tinkering with the APIs to try and find an interim solution. There are a couple of challenges with this.

  1. How do you find the Apex classes that directly call a given class?
  2. How do you identify which of those classes is a test class?

The SymbolTable that is exposed by the Tooling API can be useful for the first problem. The ExternalReferences can be used to find all the Apex Classes (and Triggers) that have a direct reference to the given class. It isn't ideal as we really want to find the inbound rather than outbound references. I.e. what calls me, not what do I call. Also, the ExternalReferences returns the class name and namespace, but not the Id. Ugh, so I need to do an extra query to resolve the Id for each class.

There are also less savory ways of finding the reference information if you are willing to pull apart the developer console. Do so at your own risk. One of these is to borrow an unofficial API from the developer console. The short version is that you POST to /_ui/common/apex/debug/ApexCSIAPI and pull out the JSON response. That will give you the list of Apex classes that depend on a given class.

This brings us back to the second problem. How to determine if an Apex class contains Test cases?
My current approach is to do a SOSL query looking for Apex classes that contain @IsTest or testMethod. It can get a few false positives, but at least narrows down the referenced Apex class Ids to those that are likely to contain test methods.

I've put the accumulation of these ideas into the latest release of the FuseIT SFDC Explorer. If you are viewing an individual Apex class you can used the double green arrow to start all the associated test classes.

See also:

FuseIT SFDC Explorer 3.3.16274.7

The latest v3.3 release of the FuseIT SFDC Explorer is out and contains a number of new features. Just in time for Dreamforce '16.

Metadata Deploy

Select a folder to monitor for changes (1). Anything that changes in that folder and is a supported metadata type is added directly to the package.

Select a folder to scan for files modified today (2). Finds any supported metadata types that were added today and adds them to the package.

Redeploy the most recent package (3). If you last deployed from a collection of selected files it will rebuild the package from those and deploy them.

SOSL query support

The SOQL Query tab now doubles as a SOSL query tab (no syntax highlighting yet). You can also export the results to a CSV or the clipboard as with SOQL.

Code Coverage

On the code coverage tab the Percent column has some color coding to indicate if you are hitting the minimum 75% for that class. The individual selected classes is also shown plus an org wide coverage at the top.

The green double arrow to the right of the classes code coverage will attempt to find all the test cases that contribute the the coverage of that class. Then it queues those tests up to run.

Note that as of the time of writing there is a bug in the SOAP version of the Tooling API that is preventing individual line coverage results from being returned.

Apex test execution

If Max Failures is set to a number greater than -1 it will stop after the specified number of test case failures in an asynchronous run.

Individual Entity Viewer

When viewing an individual record by ID the field metadata will be used to expand the property grid and show all the accessible fields. Note that I'm seeing some oddities with the Entity Viewer and sometimes need to toggle between the Categorized and Alphabetical views to get it to render. It might just be something with my display drivers.

Other changes

  • Detect and highlight a "Skipped X bytes of detailed log" message in the debug log
  • WSDL2Apex: Handle class name conflicts within a namespace.
  • WSDL2Apex: Add warning for "INVALID_FIELD_FOR_INSERT_UPDATE - Number of parameters exceeds maximum: 32" - Include names for elements in warnings.
  • WSDL2Apex: Improve error handling if WSDL schema element is missing type attribute
  • WSDL2Apex: ensure enum name starts with a letter. It is not allowed to start with the number in Apex.

Saturday, September 10, 2016

Getting involved in the Salesforce Developer Community

I'll let you in on a little secret. Salesforce as a platform is really big, and it is only getting bigger. See all those colored circles, those are the Sales, Service, Marketing, Community, Analytics, Platform & Apps products. Then there is IoT in the middle integrating with many of those areas. Plus at Dreamforce '16 we are going to learn more about Salesforce Einstein, the new AI product and where it will fit in.

I'll go out on a limb here and say there probably isn't anyone with really in depth knowledge of all these products. And by that I mean deep vertical knowledge that would only come from working with it day in and day out solving real world problems. The sort of person you turn to late on a Friday when the deployment fails with some newly discovered error that even Google has never heard of.

How are you, as a developer, supposed to work with any number of these products at any given time?

No developer is an Island

Thankfully, it isn't all doom and gloom.

There are a number of existing Salesforce communities to help fill in the gaps with your Salesforce knowledge. Best of all, they are all free.

Salesforce StackExchange (SFSE)

This is my personal favorite, so it goes first in the list. Think Stack Overflow specifically for Salesforce. It's a well tuned site for asking and answering questions specific to working with Salesforce. It isn't a discussion forum, so you avoid the back and forward chit chat and associated noise that you get in other places. Plus content gets moderated and refined over time by the community.

An example of community moderation in the StackExchange format is the drive towards removing duplicates and having one conical answer where possible. We don't really need one question per person who encounters a System.QueryException: List has no rows for assignment to SObject exception. One is sufficient to outline how to check if a SOQL query returns at least one result.

SFSE is definitely my first stop for any Salesforce related questions I might have. More often than not the system will suggest a related question to the one you are forming, saving you from creating a new one.

If there is a downside to the SFSE system, it is that it can be a bit unfriendly/daunting to new users. Approaching it like any other forum system likely won't get you the results you want. Before asking a new question, have a read of How do I ask a good question? You want the question to be on topic to Salesforce, about a specific problem, and with the right amount of relevant details that someone could answer it.

Do

  • Include the applicable lines of code.
    If your asking a question about an exception, include the code that caused it. More importantly, indicate which of those lines of code caused the exception. It isn't always obvious when dealing with a snippet of code as line numbers aren't meaningful.
  • Put some effort into formatting your question. Code blocks in particular.
    Use the built in code formatting syntax. A bit of clean formatting makes it much easier for anyone attempting to answer your question to focus on the actual problem rather than misaligned code samples.
  • Vote for the constructive benefit of the site.
    Use your votes to encourage good questions and useful answers.
  • Acknowledge and link to any resources you used.
  • Use the tags.
    These help users identify questions they can potentially answer faster.

Don't

  • Post without showing any effort on your part to solve the problem.
    Someone else is giving up their time to try and help you. Include some details about what you have already tried and why it didn't work.
  • Post comments as an answer.
    It will get removed. Instead try and gain sufficient reputation so you can use the commenting system.
  • Post answers as comments.
    Comments are considered transient. They can drop off the page if there are several other interactions occurring. Also, voting on comments only serves to reorder them, nothing else.

Salesforce Developer forums

The Salesforce Developer Forum predates the SFSE. It still offers a broad trove of knowledge in the various threads.

The difficulty here is finding the nuggets of current information. It suffers from a number of problems that Stack Overflow was designed to address with Q&A sites. Lots of duplicates, stale posts that can't be edited, a limited notification system for interacting on multiple threads, painfully bad hyperlinks, etc....

Twitter

If you've got problems that can be expressing in 140 character then the #askforce hash tag can provide 140 character solutions.

Also follow and interact with MVPs and UG leaders.

Local User Groups

Check if there is already a local user group that you could attend. This is a great way to network, attend presentations, and share ideas.

Salesforce blogs

Many members of the Salesforce Developer community also run blogs that run the gamut of products. Rather than trying to compile such a list here I'll refer you to Johan Yu's complied a list of Salesforce Blogs.

Trailhead

First off, you got me, this isn't really a community where you can interact with other developers directly. At least not on the face of it. Initially it is an excellent learning resource for Salesforce. You can take modules on topics of interest and then complete some challenges to help cement what you have just learned. However, the more time you spend doing it the more you find there is a whole community of people behind it as well. See #Trailhead. Also, it is not uncommon to see Trailhead specific events at usergroups.

Ideas

Suggest new enhancements or vote on existing ideas to see the products changed.

See also: