0

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. enter image description here However, this process is executed successfully if deployment container contains 1488 records or less enter image description here

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 '
);
0

I solved this problem by splitting records into chunks of 1000 records and deploying them separately.

I have one field with unique values. So I order my records by that field and write down values of 1000th, 2000th, 3000th and 4000th records

List rs = [ SELECT UniqueField__c FROM Object__c ORDER BY UniqueField__c ASC ];
System.debug(LoggingLevel.ERROR, ‘@@@ v: ‘ + rs[1000] );
System.debug(LoggingLevel.ERROR, ‘@@@ v: ‘ + rs[2000] );
System.debug(LoggingLevel.ERROR, ‘@@@ v: ‘ + rs[3000] );
System.debug(LoggingLevel.ERROR, ‘@@@ v: ‘ + rs[4000] );

Then I just modify my code to include where clause WHERE UniqueField__c > ‘value1000’ AND UniqueField__c <= 'value2000' ORDER BY UniqueField__c ASC and execute it five times with different threshold values corresponding to values of UniqueField__c on 1000th, 2000th, 3000th and 4000th records.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.