Wednesday, December 14, 2022

How to Delete (Bulk) Custom Metadata Records in Salesforce.

 Scenario:

You might have used the "Custom Metadata Loader" to perform the bulk insert and bulk update. Sometimes it's might be required to perform the bulk delete also ,but unfortunately this tool doesn't support as of today.

Solution:

So as a workaround we can have apex code snippet which can be used to perform the bulk deletion(in a single run max 200 records) of metadata .Please use the below code snippet for the same.

Source Code:

MetadataService.MetadataPort service = new MetadataService.MetadataPort();

// Set the session id
service.SessionHeader = new MetadataService.SessionHeader_element();
service.SessionHeader.sessionId = UserInfo.getSessionId();

//Add all your metadata records developer name to list
List<String> recordsToDelete = new List<String>();
For(YourMedataDataName__mdt m :[SELECT Developername from YourMedataDataName__mdt Limit 200])
{
	String s = 'YourMedataDataName__mdt.'+m.Developername;
	recordsToDelete.add(s);
}

//Perform the bulk deletion at a time max of 200 records
MetadataService.DeleteResult [] results = new MetadataService.DeleteResult []{};
results = service.deleteMetadata('CustomMetadata', recordsToDelete);


No comments:

Post a Comment

Understanding Wire vs Imperative Apex Method Calls in Salesforce Lightning Web Components (LWC)

Understanding Wire vs Imperative Apex Method Calls in Salesforce Lightning Web Components (LWC) Introduction: Salesforce Lightning Web ...