2

I am trying to merge leads with the Database.merge method of syntax merge(masterRecord, duplicateIds, allOrNone), but I'm getting the following error:

Unable to create/update fields: CreatedDate. Please check the security settings of this field and verify that it is read/write for your profile or permission set.

CreatedDate does not appear to be something you give access to at a profile or permission set level, and no where in my code am I trying to set the CreateDate.

What am I missing? I also plan on merging contacts as well through apex, are there any considerations I need to be aware of that are similar to this issue?

String mode = 'lead';
String emailListString = 'leadtest3@leadtest.com';
emailListString = emailListString.replace(' ', '');
List <String> emailList = new List<String>(); 
emailList = emailListString.split(',');
Database.MergeResult[] results;

for (integer i = 0; i < emailList.size(); i++) {
    List <Lead> leadList = new List <Lead>();
    List <Contact> contactList = new List <Contact>();
    List <Id> listOfDuplicates = new List <Id>();
    if (mode == 'lead') {
        leadList = [select Id, Email, CreatedDate from Lead WHERE Email = 'leadtest3@leadtest.com' ORDER BY CreatedDate DESC LIMIT 3];
        system.debug('leadList.size(): ' + leadList.size());
        for (integer x = 0; x < leadList.size(); x++) {
            if (x == 0) {
                system.debug('Master Record: ' + leadList[0]);
            } else {
                listOfDuplicates.add(leadList[x].Id);
            }
        }
        system.debug('listOfDuplicates: ' + listOfDuplicates);
        results = Database.merge(leadList[0], listOfDuplicates, false);

    } else if (mode == 'contact') {
        contactList = [select Id, Email, CreatedDate from Contact WHERE Email = 'leadtest3@leadtest.com' ORDER BY CreatedDate DESC LIMIT 3];
        system.debug('contactList.size(): ' + contactList.size());
        for (integer x = 0; x < contactList.size(); x++) {
            if (x == 0) {
                system.debug('Master Record: ' + contactList[0]);
            } else {
                listOfDuplicates.add(leadList[x].Id);
            }
        }
        results = Database.merge(contactList[0], listOfDuplicates, false);
    }

    for(Database.MergeResult res : results) {
    MergeRecord__c mergeRecord = new MergeRecord__c();    
    if (res.isSuccess()) {
    mergeRecord.masterId__c = res.getId(); 
    mergeRecord.duplicateIds__c = String.valueOf(res.getMergedRecordIds());  
    } 
    else {
        for(Database.Error err : res.getErrors()) {
        mergeRecord.error__c = err.getMessage();
        }    
    }
    if (leadList.size() > 0) {
        mergeRecord.leadList__c = String.valueOf(leadList);
    }
    else if (contactList.size() > 0 ) {
       mergeRecord.contactList__c = String.valueOf(contactList); 
    }
    insert mergeRecord;   
    }
}
2

Since you queried the CreatedDate field, Salesforce is trying to set the field value on merge, which it can't, because the field can't be modified. Remove the CreatedDate field from your query.

leadList = [select Email from Lead WHERE Email = 'leadtest3@leadtest.com' ORDER BY CreatedDate DESC LIMIT 3];

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.