Conversion process of custom object records into Custom Metadata records using Metadata.Operations class enqueueDeployment method fails if deployment container contains more than 1488 records. However, this process is executed successfully if deployment container contains 1488 records or less
I receive different Gack Ids 882046342, -1760255134 and 29825863 when I try to deploy different amount of records.
I use the following code to convert and deploy custom metadata records https://gist.github.com/Patlatus/53ce49fde265c0aa3d0f2c3fdda8b900
public class MD {
public static List<String> getItems(List<String> parts, Map<String, Schema.SObjectField> fieldsMap, SObject r) {
List<String> items = new List<String>();
for (String part: parts) {
items.add( fieldsMap.containsKey( part ) ? String.valueOf( r.get( part ) ) : part );
}
return items;
}
public static Id updateAndDeployMetadata(List<SObject> sourceRecords, SObjectType dest, Map<SObjectField, SObjectField> mappings, String fullNameDef, String labelDef) {
Metadata.DeployContainer mdContainer = new Metadata.DeployContainer();
String destName = dest.getDescribe().getName();
List<String> fullNameParts = fullNameDef.split('\\+');
List<String> labelParts = labelDef.split('\\+');
if( sourceRecords != null && !sourceRecords.isEmpty() ) {
Map<String, Schema.SObjectField> fields = sourceRecords[0].getSObjectType().getDescribe().fields.getMap();
for (SObject r: sourceRecords ) {
Metadata.CustomMetadata customMetadata = new Metadata.CustomMetadata();
customMetadata.fullName = destName + '.' + String.join( getItems(fullNameParts, fields, r), '');
customMetadata.label = String.join( getItems(labelParts, fields, r), '').left(40);
for (SObjectField key: mappings.keySet() ) {
Metadata.CustomMetadataValue customField = new Metadata.CustomMetadataValue();
customField.field = String.valueOf( mappings.get(key) );
customField.value = r.get(key);
customMetadata.values.add(customField);
}
mdContainer.addMetadata(customMetadata);
}
return Metadata.Operations.enqueueDeployment(mdContainer, null);
}
return null;
}
}
https://gist.github.com/Patlatus/bb4c964f72ddfb60bbd4710bb34597b9
MD.updateAndDeployMetadata(
[ SELECT Field1__c, Field2__c, Field3__c, Field4__c FROM Object__c ],
CustomMetadata__mdt.sObjectType,
new Map<SObjectField, SObjectField>{
Object__c.Field1__c=> CustomMetadata__mdt.Field1__c,
Object__c.Field2__c=> CustomMetadata__mdt.Field2__c,
Object__c.Field3__c=> CustomMetadata__mdt.Field3__c,
Object__c.Field4__c => CustomMetadata__mdt.Field4__c
},
'X+Field1__c+_+Field2__c',
'Field3__c+ +Field4__c '
);