function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
roni shoreroni shore 

Create record in custom object upon account merge

I have a custom object custom_obj__c, where i have to create records upon account merge activity. I am writing the following trigger to acheive
issue is that the Master_Account__c  on custom object is MD relation on Accout and whenever I am trying to merge ..getting this error
"Apex trigger AccountMergeTrigger caused an unexpected exception, contact your administrator: AccountMergeTrigger: execution of AfterDelete caused by: System.DmlException: Insert failed. First exception on row 0; first error: ENTITY_IS_DELETED, entity is deleted: []: Trigger.AccountMergeTrigger: line 9, column 1"
trigger AccountMergeTrigger on Account (after delete) {
    List<Account_Backup__c> listAccountBackup = new List<Account_Backup__c>();
    for(Account acct : trigger.old) {
        if(String.isNotBlank(acct.MasterRecordId)) { 
            listAccountBackup.add(new Account_Backup__c(Name = acct.Name, Master_Account__c = acct.id));  
        }         
    }
    if(listAccountBackup.size() > 0) {
        insert listAccountBackup;
    }    
}

Please suggest
Best Answer chosen by roni shore
Purushotham YellankiPurushotham Yellanki
Hi Rohan,

By design Master-Details relationship works that way, you cannot have Child records exist while you delete Master record in Master-Detail relationship model. You can try changing relationship from MD to Lookup so that you can have your Child records exist even after your Lookup records is deleted or try creating some stage object to store your Account information and create that record without having any relationship with Accont object. This Link has detailed explanation on MD and Lookup: http://sfdcsrini.blogspot.com/2015/10/difference-between-master-detail.html




Thank you

 

All Answers

Purushotham YellankiPurushotham Yellanki
Hi Rohan,

By design Master-Details relationship works that way, you cannot have Child records exist while you delete Master record in Master-Detail relationship model. You can try changing relationship from MD to Lookup so that you can have your Child records exist even after your Lookup records is deleted or try creating some stage object to store your Account information and create that record without having any relationship with Accont object. This Link has detailed explanation on MD and Lookup: http://sfdcsrini.blogspot.com/2015/10/difference-between-master-detail.html




Thank you

 
This was selected as the best answer
roni shoreroni shore
Thanks-this helps. Now another issue, I do not see any child record created even if made lookup
Purushotham YellankiPurushotham Yellanki
Hi Rohan,

In order to display your child records linked to your Parent Lookup Object, you have to link them first and pull your related list on to the page layout!




Thank you
roni shoreroni shore
I am trying to create the record using above trigger & the object is already in related list.