---
schema_version: "1"
id: 3f65f8df3c51222bcb78f957c19ae214b75f0f0cc20251b5f9151c4bdbc80967
title: ApexCodeCoverage
collection: legacydeveloper
version: current
locale: en-us
portal_url: https://docs.salesforce.com/legacydeveloper/current/en-us/api_tooling/tooling_api_objects_apexcodecoverage.md
canonical_url: https://developer.salesforce.com/docs/atlas.en-us.api_tooling.meta/api_tooling/tooling_api_objects_apexcodecoverage.htm
source_site: developer.salesforce.com
content_type: documentation
content_hash: 9c4e46cef1add95c31ad168885c3126d1554e5d43d105707ba19889d2fdd628b
description: Represents code coverage test results for an Apex class or trigger. Available in Tooling API version 29.0 and later.
---
# ApexCodeCoverage

Represents code coverage test results for an Apex class or trigger. Available in Tooling API version 29.0 and later.

## Supported SOAP API Calls

describeSObjects(), query(), retrieve()

## Supported REST API HTTP Methods

Query, GET

## Special Access Rules

In API version 49.0 and later, users must have the View Setup and Configuration and View All Data permissions to access this object.

## Fields

| Field | Details |
| --- | --- |
| ApexTestClassId | 
Type

string

Properties

Filter, Group, Sort

Description

The ID of the test class.



 |
| TestMethodName | 

Type

string

Properties

Filter, Group, Sort

Description

The name of the test method.



 |
| ApexClassorTriggerId | 

Type

string

Properties

Filter, Group, Sort

Description

The ID of the class or trigger under test.



 |
| NumLinesCovered | 

Type

int

Properties

Filter, Group, Sort

Description

The number of covered lines.



 |
| NumLinesUncovered | 

Type

int

Properties

Filter, Group, Sort

Description

The number of uncovered lines.



 |
| Coverage | 

Type

complexvalue

Properties

None

Description

Two lists of integers. The first is the covered lines, and the second is the list of uncovered lines. If a line is missing from both lists, the line isn’t executable and doesn’t require coverage.

Coverage includes these fields:

- coveredLines
- namespace
- uncoveredLines





 |

## Usage

To query for code coverage, specify an Apex class, test class, or both. The returned JSON or XML object contains two lists of integers: one for covered and one for uncovered lines.

The following example SOQL query retrieves code coverage results for a specific class or trigger covered by a specific test class:

SELECT Coverage
FROM ApexCodeCoverage 
WHERE ApexClassOrTriggerId = ‘01pD000000066GR’ 
AND ApexTestClassId = ‘01pD000000064pu’ 

For per-class code coverage, the query is:

SELECT Coverage
FROM ApexCodeCoverage 
WHERE ApexClassOrTriggerId = ‘01pD000000066GR’ 

![Note](https://developer.salesforce.com/docs/atlas.en-us.api_tooling.meta/api_tooling/images/icon_note.png)

#### Note

In this case, multiple rows can be returned, since there could be multiple test classes that cover the same test class.

Coverage is returned as two lists of integers. The first is the covered lines, and the second is the list of uncovered lines. If a line is missing from both lists, the line isn’t executable and doesn’t require coverage. For example, if the covered lines are 2, 9, and 11, and uncovered lines are 3, 4, 5, and 6; the result is: {2,9,11},{3,4,5,6}. The missing lines (1, 7, 8 and 10) aren’t executable.

Code coverage percentage is a simple calculation of the number of covered lines divided by the sum of the number of covered lines and the number of uncovered lines. For example, to calculate code coverage percentage in SOAP:  

ApexCodeCoverage acc = null; //Query for an ApexCodeCoverage object 
Coverage coverage = acc.coverage; 
int\[\] covered = coverage.coveredLines; 
int\[\] uncovered = coverage.uncoveredLines; 
int percent = covered.length / (covered.length + uncovered.length); 
System.out.println("Total class coverage is " + percent + "%.");

![Note](https://developer.salesforce.com/docs/atlas.en-us.api_tooling.meta/api_tooling/images/icon_note.png)

#### Note

If a single deployment has over 2,000 Apex classes, ApexCodeCoverage objects for the deployed classes are deleted even if the deployment fails or is rolled back. ApexCodeCoverageAggregate objects aren’t affected.