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
Cobb AndersonCobb Anderson 

Trigger, preventing user from deleting contacts unless account owner

hello, mighty developers.
I am new to APEX trigger, so I am having a lot of issue writing simple code.
I tried to write a trigger where an owner can only delete contact record when the contact's account owner is also the same.
there was no syntax error but now everyone cannot delete anything from contact.
so, please, have a mercy on this poor girl and a little help!!

============================================================

trigger Tri_AcContOwner2 on Contact (before delete, before insert, before update)  {
      Integer isa = Trigger.size;
  if(Trigger.isBefore){
     if(Trigger.isInsert || Trigger.isUpdate || Trigger.isDelete){
         Set<id> account_id_set = new Set<Id>{};
         for(Contact z1 : Trigger.new){
             account_id_set.add(z1.AccountId);
                                      } 
         //extract account name
         List<Account> account_list = [SELECT OwnerID From Account WHERE Id IN :account_id_set];
         for(Contact z1 : Trigger.new){
             for(Account z2 : account_list){
                 if(z2.OwnerID != UserInfo.getUserId()){
                     for(integer i = 0; i < isa; i++){
              Trigger.new[i].addError('Only the account owner can play with this');                 
              Trigger.old[i].addError('only the account owner can delete this');         
                         }//last for
                    }//last if
                }//last2 for
            }//last3 for
        }//up2 if
    }//up1 if
}


================================


thanks in advance!!!
EnreecoEnreeco
Hi Cobb,
first thing I neot is that you are iterating through Contacts, thant through Accounts and another time through all the contacts in the trigger: I think the first "for" can be omitted.
Secondo thing is that in the DELETE trigger there is not Trigger.new record but only Trigger.old...that's why you have to reference a generic "List<Case> contacts" that points to Trigger.new when inserting/updating, or Trigger.old when deleting record.

Hope this helps

--
May the Force.com be with you!
 
MissedCallMissedCall
Hello Cobb,

Here is the code for you, tested it and is working for me. Let me know if you have any issues.

Trigger:
trigger ContactDeletePrevent on Contact (before delete) {
	if(Trigger.isBefore){
		if(Trigger.isDelete){
            PreventContactDelete.contactDelete(trigger.oldMap);  
        }
	}
}
Class:
public class PreventContactDelete {
	
	 public static void contactDelete(Map<Id,Contact> contacts){
	 	
	 	set<Id> accId = new set<Id>();
	 	set<Id> conId = new set<Id>();
	 	
	 	for(Contact ct: contacts.values()){
	 		accId.add(ct.AccountId);
	 		conId.add(ct.Id);
	 	}

	 	List<Account> lstAcc = [SELECT Id, OwnerId 
			 				    FROM Account
			 				    WHERE Id IN : accId];
 			 				    		   
 		List<Contact> cons = [SELECT c.OwnerId, c.Name, c.Id, c.Account.OwnerId, c.Account.Name, c.Account.Id, c.AccountId 
 							  FROM Contact c
 							  WHERE c.Id IN : conId
 							  AND c.AccountId IN : accId];
 		
 		if(lstAcc.size()>0 && cons.size()>0 ){					  
		 	for(Account acc : lstAcc){
			 	for(Contact cont: cons){			 		
			 		if(cont.OwnerId != acc.OwnerId){
			 			Contact shErr = contacts.get(cont.Id);
			 			shErr.addError('Only Contact/Account Owner can delete this contact.');
			 		}
			 	}
		 	}
 		}
	 }

}

Good Luck!