Skip to content

Instantly share code, notes, and snippets.

@dancinllama
Created April 4, 2019 01:12
Show Gist options
  • Save dancinllama/100ddb04c22b322c0af44560f7e66d4a to your computer and use it in GitHub Desktop.
Save dancinllama/100ddb04c22b322c0af44560f7e66d4a to your computer and use it in GitHub Desktop.
//The enqueue deployment call (last call above), can optionally take a callback argument.
//I was running into an issue and needed a bit more debugging, so here's my callback for that.
//This call can be an inner class, by the way.
public class CustomMetadataCallback implements Metadata.DeployCallback {
public void handleResult(Metadata.DeployResult result, Metadata.DeployCallbackContext context) {
System.debug('status: ' + result.status);
if(!result.success){
System.debug('error1: ' + result.errorMessage);
System.debug('error2: ' + result.errorStatusCode);
System.debug('error3: ' + result.errorMessage);
System.debug('error4: ' + result.numberComponentErrors);
for(Metadata.DeployMessage msg : result.details.componentFailures){
System.debug('msg problem: ' + msg.problem);
System.debug('msg problem type: ' + msg.problemType);
}
}
}
}
Metadata.CustomMetadata settings = new Metadata.CustomMetadata();
settings.fullName = 'Your_Custom_Metadata_API_Name_WITHOUT_UNDERSCOREUNDERSCOREMDT_GOES_HERE.Default'; //Default is the name on 1 of many of my custom metadta type records
settings.label = 'Default'; //Default is also a label. and you will need the label for the update to succeed.
//Only updating one field.. if you need more fields updated, just instanciate more CustomMetdataValues and add them to the values list at line 9.
Metadata.CustomMetadataValue dateValue = new Metadata.CustomMetadataValue();
dateValue.field = 'Current_Sales_Date__c'; //The field I'm updating
dateValue.value = Date.newInstance(2019,3,8); //Value can be anything, really, but I needed a date.
settings.values.add(dateValue);
Metadata.DeployContainer mdContainer = new Metadata.DeployContainer();
mdContainer.addMetadata(settings);
Metadata.Operations.enqueueDeployment(mdContainer, new CustomMetadataCallback());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment