You need to sign in to do that
Don't have an account?
Cobb 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!!!
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!!!
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!
Here is the code for you, tested it and is working for me. Let me know if you have any issues.
Trigger: Class:
Good Luck!