I have a requirement to create and update custom meta data using apex , I'm passing the required values from my lwc , though i have found no proper example of handling the error since enqueueDeployment only returns the id of the deployment and not if the deployment was a success or a failure.The handleresult does have result parameter but it says this method has to void type and not return any result.Here is the code i found on the internet and am using to create the metadata, any inputs would be helpful.
public class CreateUpdateMetadataUtility implements Metadata.DeployCallback {
public void handleResult(Metadata.DeployResult result,
Metadata.DeployCallbackContext context) {
if (result.status == Metadata.DeployStatus.Succeeded) {
system.debug('the result of the insertion is'+result);
//return result.status;
} else {
system.debug('the error is'+result.status);
//return result.errorMessage;
// mDebugUntruncated(result.toString());
}
}
public static string createUpdateMetadata(String fullName, String label, Map<String, Object> fieldWithValuesMap){
Metadata.CustomMetadata customMetadata = new Metadata.CustomMetadata();
customMetadata.fullName = fullName;
customMetadata.label = label;
for(String key : fieldWithValuesMap.keySet()){
Metadata.CustomMetadataValue customField = new Metadata.CustomMetadataValue();
customField.field = key;
customField.value = fieldWithValuesMap.get(key);
customMetadata.values.add(customField);
}
Metadata.DeployContainer mdContainer = new Metadata.DeployContainer();
mdContainer.addMetadata(customMetadata);
CreateUpdateMetadataUtility callback = new CreateUpdateMetadataUtility();
Id jobId = Metadata.Operations.enqueueDeployment(mdContainer, callback);
return jobId;
}
}