In our CI process, we trigger a job for validation in Jenkins and have an ant target which runs the validation first with checkOnly as true and runs all the tests.

build.xml :

<target name="sf_build" depends="sf_prepare_deployment">
    <echo message="START: SFDC Deployment" />
    <echo message="../${diffDir}/src" />
    <sf:deploy  username="${sf.deploy.username}" password="${sf.deploy.password}"
                serverurl="${sf.deploy.serverurl}" sessionId="${sf.deploy.sessionId}"
                deployRoot="../${diffDir}/src" maxPoll="${sf.maxPoll}"
                pollWaitMillis="${sf.pollWaitMillis}" checkOnly="${sf.checkOnly}"
                runAllTests="${sf.runAllTests}" logType="${sf.logType}"

Is there a way to extract the test coverage from the org when only validation job has happened ? We would like to fail the build if the code coverage is below a certain threshold.

Is there any rest api or soap api which can give this result ?

2 Answers 2


You can call the checkDeployStatus method to check the DeployResult object. As far as I'm aware, this is still only currently available in the form of a SOAP call. It is also not exposed via the sf:deploy Ant task, so you'd have to patch in that feature or write your own Ant task, or use some other technique entirely (e.g. write your own deployment tool). This is fairly trivial (I've done one or two myself), but you'll need some Java expertise to make this happen.

Alternatively, you can move away from Ant tasks and instead use sfdx. Just execute the command for deployment with a wait timeout, and it can return a coverage report that you can then parse with a command script to determine total coverage and generate errors. This might be a bit easier to do, as you'll only have to deal with a shell script instead of compiling code.

  • Ok so you mean write a jar with com.force.api force-apex-api this dependency and get the DeployResult. and fire this jar from ant. Ok, this I can do, but will it work when checkOnly is true, will salesforce still calculate the code coverage when its just a validation job. Commented Apr 18, 2019 at 3:32
  • @NagendraSingh Yes, code coverage can be requested for any type of deploy. Try it out in SFDX before you commit to a more complicated path like writing code.
    – sfdcfox
    Commented Apr 18, 2019 at 3:37
  • With this approach, can yoy receive code coverage with same level of details as is provided when using sfdx force:apex:test:run?
    – patryk
    Commented Nov 22, 2021 at 10:07
  • @patryk Yes, the API itself returns this data, which both the Ant Migration Toolkit and SFDX use.
    – sfdcfox
    Commented Nov 22, 2021 at 12:52

Thanks @sfdcfox

I was able to fetch the details from Soap service :

public static void main(String[] args) {

    ConnectorConfig config = new ConnectorConfig();
    List<String> lstString = new ArrayList<String>();
    try {
        MetadataConnection metadataConnection = new MetadataConnection(config);
        DeployResult deployResult = metadataConnection.checkDeployStatus("0Af7F00001CKZuk", true);
        DeployDetails details = deployResult.getDetails();
        RunTestsResult runTestResult = details.getRunTestResult();
        for (CodeCoverageWarning codeCoverageWarning : runTestResult.getCodeCoverageWarnings()) {

        for (String eachMessage : lstString) {

    } catch (ConnectionException e) {

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .