Trigger AccountWithContact on Contact(before insert){
List<Id> accIds= new List<Id>();
for(Contact con: Trigger.New){
accIds.add(con.AccountId);
}
List<Account> accList = [Select id,Name,(Select Id, name from Contacts) from Account where Id IN: accIds ];
for(Account acc : accList){
for(Contact con : Trigger.New){
if(acc.Contacts.size() == 5) {
con.addError(' You can not add more then 5 contacts');
}
}
}
}
if you need any assistanse, Please let me know!!
Kindly mark my solution as the best answer if it helps you.
Trigger AccountWithContact on Contact(before insert){
List<Id> accIds= new List<Id>();
for(Contact con: Trigger.New){
accIds.add(con.AccountId);
}
List<Account> accList = [Select id,Name,(Select Id, name from Contacts) from Account where Id IN: accIds ];
for(Account acc : accList){
for(Contact con : Trigger.New){
if(acc.Contacts.size() == 5) {
con.addError(' You can not add more then 5 contacts');
}
}
}
}
if you need any assistanse, Please let me know!!
Kindly mark my solution as the best answer if it helps you.
Trigger checkcontactCreation on Contact(before insert){
List<Id> accIds= new List<Id>();
for(Contact con: Trigger.New){
accIds.add(con.AccountId);
}
List<Account> accList = [Select id,Name,(Select Id, name from Contacts) from Account where Id IN: accIds ];
for(Contact con : Trigger.New){
if(acc.Contacts.size() == 5) {
con.addError(' You can not add more then 5 contacts');
}
}
}
Hi Mukesh,
Thankyou so much for the code.
I have modified few lines in the code,pls check once. I mentioned comments where i changed,hope its correct.Its not allowing to create new 6th record but throwing error instead of throwing error message.Please check n letme know
Trigger checkcontactCreation on Contact(before insert){
List accIds= new List();
for(Contact con: Trigger.New){
accIds.add(con.AccountId);
}
List accList = [Select id,Name,(Select id,LastName from Contacts) from Account where Id IN: accIds ];
for(Account acc:accList){ //changed here
if(acc.Contacts.size() == 5) { //changed here
acc.addError(' You can not add more then 5 contacts'); //changed here
}
}
}
Thanks,
Yagna
in below line you need to add con instaed of acc acc.addError(' You can not add more then 5 contacts');
for(Account acc : accList){
for(Contact con : Trigger.New){
if(acc.Contacts.size() == 5) {
con.addError(' You can not add more then 5 contacts');
}
}
}
if you need any assistanse, Please let me know!!
Kindly mark my solution as the best answer if it helps you.
Please double-check a few of the code's updated lines. I hope what I said about the comments being changed is accurate fnf mod (https://fnfmod.co). It prevents the creation of a new sixth record and instead throws an error rather than an error message.
Please use below trigger:-
if you need any assistanse, Please let me know!!
Kindly mark my solution as the best answer if it helps you.
Thanks
Mukesh
All Answers
Please use below trigger:-
if you need any assistanse, Please let me know!!
Kindly mark my solution as the best answer if it helps you.
Thanks
Mukesh
try with below code.
If this helps, Please mark it as best answer.
Thanks!!
in below line you need to add con instaed of acc
acc.addError(' You can not add more then 5 contacts');
if you need any assistanse, Please let me know!!
Kindly mark my solution as the best answer if it helps you.
Thanks
Mukesh