Skip to content
This repository was archived by the owner on Jul 30, 2025. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .vs/CustomMetadataLoader/v15/.suo
Binary file not shown.
27 changes: 27 additions & 0 deletions custom_md_loader/classes/CustomMetadataUploadController.cls
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,33 @@ public class CustomMetadataUploadController {
}
}
}

public PageReference deleteCustomMetaData() {
ApexPages.getMessages().clear();
showRecordsTable = false;
deleteCustomMetadtaRecords();
// TODO: Add Success Message / Display Errors
return null;
}

private void deleteCustomMetadtaRecords() {
if(selectedType == 'Select type') {
ApexPages.Message errorMessage = new ApexPages.Message(ApexPages.severity.ERROR, 'Please choose a valid custom metadata type.');
ApexPages.addMessage(errorMessage);
return;
}

String query = 'SELECT DeveloperName FROM ' + selectedType;

List<sObject> customRecords = Database.query(query);
List<String> recordsToDelete = new List<String>();

for (sObject record : customRecords) {
recordsToDelete.add(selectedType + '.' + record.get('DeveloperName'));
}

MetadataUtil.deleteMetadata('CustomMetadata', recordsToDelete);
}

public PageReference upsertCustomMetadata() {
ApexPages.getMessages().clear();
Expand Down
32 changes: 28 additions & 4 deletions custom_md_loader/classes/MetadataUtil.cls
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,6 @@ public class MetadataUtil {
List<String> fieldValueList = new List<String>();

for (String singleRowFieldValues :singleRowOfValues) {
if (String.isBlank(singleRowFieldValues)) {
continue;
}

fieldValueList.addAll(singleRowFieldValues.split(';'));
}
for(String fieldName : fieldNameSet) {
Expand Down Expand Up @@ -152,6 +148,34 @@ public class MetadataUtil {
customMetadata.values = customMetadataValues;
return customMetadata;
}

public static void deleteMetadata(String type_x,String[] fullNames) {
List<MetadataService.DeleteResult> results = getPort().deleteMetadata(type_x, fullNames);
if (results!=null) {
for (MetadataService.DeleteResult deleteResult : results) {
if (deleteResult==null || deleteResult.success) {
continue;
}
// Construct error message and throw an exception
if (deleteResult.errors!=null) {
List<String> messages = new List<String>();
messages.add((deleteResult.errors.size()==1 ? 'Error ' : 'Errors ') + 'occured processing component ' + deleteResult.fullName + '.');
for(MetadataService.Error error : deleteResult.errors){
messages.add(error.message + ' (' + error.statusCode + ').' + ( error.fields!=null && error.fields.size()>0 ? ' Fields ' + String.join(error.fields, ',') + '.' : '' ) );
}
if(messages.size()>0){
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Error, String.join(messages, ' ')));
System.debug(LoggingLevel.ERROR, String.join(messages, ''));
return;
}
}
if(!deleteResult.success){
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Error, 'Request failed with no specified error.'));
return;
}
}
}
}


private static void upsertMetadataAndValidate(MetadataService.Metadata[] records) {
Expand Down
9 changes: 9 additions & 0 deletions custom_md_loader/pages/CustomMetadataRecordUploader.page
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@
</apex:pageBlockSection>
</apex:ActionRegion>
</apex:pageBlock>

<div align="center" draggable="false" >
<apex:ActionRegion >
<apex:commandButton value="Delete Custom MetaData Type" action="{!deleteCustomMetaData}" rendered="true"/>
<apex:actionSupport event="onclick" action="{!deleteCustomMetaData}" rendered="{!showRecordsTable}" oncomplete="window.opener.location.reload();"/>
</apex:ActionRegion>
</div>
<br/>
<br/>

<div align="center" draggable="false" >
<apex:ActionRegion >
Expand Down