Check Metadata deploy result form Apex

PHOTO EMBED

Mon Oct 10 2022 11:20:54 GMT+0000 (Coordinated Universal Time)

Saved by @Justus #apex

// (example from Daniel Ballinger)
public void checkDeployStatus(Id deployRequestId) {
    HttpRequest req = new HttpRequest();
    req.setHeader('Authorization', 'Bearer ' + UserInfo.getSessionID());
    req.setHeader('Content-Type', 'application/json');

    req.setMethod('GET');     
    String instanceName = System.Url.getSalesforceBaseUrl().toExternalForm();
    // Visualforce can't make the direct API calls. Might need to massage the instanceName
    req.setEndpoint(instanceName + '/services/data/v49.0/metadata/deployRequest/' + deployRequestId + '?includeDetails=true');
    
    Http http = new Http();
    try {
        HTTPResponse res = http.send(req);                 
        System.debug('STATUS:' + res.getStatus());
        System.debug('STATUS_CODE:' + res.getStatusCode());
        String responseBody = res.getBody(); 

        if(res.getStatusCode() == 200) {

            DeployResultJson jsonResult = (DeployResultJson)JSON.deserialize(responseBody, DeployResultJson.class);

            if(jsonResult.deployResult.details != null) {
                for(Metadata.DeployMessage dm : jsonResult.deployResult.details.componentSuccesses) {
                    if(dm.fileName == 'package.xml') {
                        continue;
                    }

                    // Do something with the DeployMessage

                }
            }
        }
    } catch(System.CalloutException e) {
        // Exception handling goes here
    }
}

// Support unpacking via JSON.deserialize from deployRequest metadata api call
public class DeployResultJson {       
    public String id;
    public Object validatedDeployRequestId;
    public Object deployOptions;
    public Metadata.DeployResult deployResult;
}
content_copyCOPY