Loading

Get accurate code coverage from Metadata API

Udgivelsesdato: Jun 25, 2026
Beskrivelse

The "Run All Tests" option in the Salesforce UI may not return accurate Organization-wide code coverage results. The only reliable way to obtain valid Org coverage is from a recent deployment using the Metadata API. This article explains how to retrieve accurate code coverage results from a deployment, whether it succeeded or failed, using SoapUI and Microsoft Excel.

Løsning

To calculate accurate Salesforce Org code coverage, you must use a deployment's async response data, not the UI test runner. This process requires a recent deployment ID, the Metadata WSDL, SoapUI, and Microsoft Excel.

Step 1: Get the AsyncId of Your Latest Deployment

Navigate to the Deployment Status page in Setup and click your latest deployment. The asyncId appears in the URL as a query parameter — for example, asyncId=0AfD0000000uBQb. Copy this ID value.

Step 2: Download the Metadata WSDL

In Setup, navigate to Develop > API (Classic) or Integrations > API (Lightning) and click Generate Metadata WSDL. Save the file with a .wsdl extension (change the file type to "All Files" and rename it metadata.wsdl).

Step 3: Query the Deployment Status via SoapUI

  1. Open SoapUI and select File | New SoapUI Project.
  2. Click Browse and select the downloaded Metadata WSDL. Ensure "Create Requests" is checked.
  3. Expand the project and create a checkDeployStatus request.
  4. Set asyncProcessId to your deployment ID and includeDetails to true.
  5. Execute the request and save the XML response to a file.

Step 4: Open the XML Response in Microsoft Excel

  1. Open Excel and select the saved XML file using File | Open.
  2. In the "Open XML" dialog, choose Use the XML Source task pane and click OK.
  3. From the XML Source pane, select the fields Id, Name, numLocations, and numLocationsNotCovered under the runTestResult node.
  4. Drag these four elements to cell A1 in the worksheet.
  5. Go to the Data tab and click Refresh All to populate the cells.

Step 5: Calculate Org-Wide Code Coverage

  1. Go to the end of column C (numLocations) and note the last row number (for example, row 2342).
  2. In the cell below the last value in column C, enter the formula: =SUM(C2:C2342) to get total lines to cover.
  3. Do the same for column D (numLocationsNotCovered): =SUM(D2:D2342).
  4. In a new cell, calculate the coverage percentage: =(1-(D2342/C2342))*100

Note: This coverage result includes all Apex in the selected deployment, not just the target org's classes.

Vidensartikelnummer

000386853

 
Indlæser
Salesforce Help | Article