1

I wrote an Apex trigger and when I try to delete a record then its giving me the following error. "There's a problem saving this record. You might not have permission to edit it, or it might have been deleted or archived. Contact your administrator for help."

My Trigger is as follows.

    trigger ContactCount on hed__Relationship__c  (after insert, after update, after delete) {
    Map<Id, List<hed__Relationship__c >> AcctContactList = new Map<Id, List<hed__Relationship__c >>();
    Set<Id> AcctIds = new Set<Id>();    

    List<Contact> AcctList = new List<Contact>();
    List<hed__Relationship__c> ConList = new List<hed__Relationship__c>();

    if(trigger.isInsert || trigger.isUPdate) {
        for(hed__Relationship__c Con : trigger.New) {
            if(String.isNotBlank(Con.hed__Contact__c)){
                AcctIds.add(Con.hed__Contact__c);  
            }   
        }  
    }

    if(trigger.isDelete || trigger.isUPdate) {
        for(hed__Relationship__c Con : trigger.Old) {
            AcctIds.add(Con.hed__Contact__c);     
        }  
    }           

    if(AcctIds.size() > 0){
        ConList = [SELECT Id, hed__Contact__c FROM hed__Relationship__c WHERE hed__Contact__c IN : AcctIds];

        for(hed__Relationship__c Con : ConList) {
            if(!AcctContactList.containsKey(Con.hed__Contact__c)){
                AcctContactList.put(Con.hed__Contact__c, new List<hed__Relationship__c>());
            }
            AcctContactList.get(Con.hed__Contact__c).add(Con);      
        }                           

        System.debug('Account Id and Contact List Map is ' + AcctContactList);

        AcctList = [SELECT Number_Of_Dependents__c FROM Contact WHERE Id IN : AcctIds];

        for(Contact Acc : AcctList) {
            List<hed__Relationship__c> ContList = new List<hed__Relationship__c>();
            ContList = AcctContactList.get(Acc.Id);
            Acc.Number_Of_Dependents__c= ContList.size();
        }    

        System.debug('Account List is ' + AcctList);
        update AcctList;    
    }

}
  • I have a feeling that given error has nothing to do with given trigger. Can you try to disable that trigger and check if issue still occurs? – kurunve Jan 2 at 10:24
  • 3
    make this trigger lightweight, put all processing logic in TriggerHandler type of class. Also, check that if person has a permission to perform certain operation – Santanu Boral Jan 2 at 10:25
  • Hello Kurunve, If I disable the trigger then I am able to perform delete operation but if this trigger is active then I am getting the issue. – user61203 Jan 2 at 10:33
  • are you a System Administrator and has Modify All permission on Contact object? – Naval Sharma Jan 2 at 10:38
  • 1
    Are there any triggers or so on Account? can you try to manually (or by dev console) to update Number_Of_Dependents__c on single account record? – kurunve Jan 2 at 11:15

Your Answer

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