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
Yagna DeepthiYagna Deepthi 

Hello,For account we should create only 5 contacts & it should stop creating 6th contact...Please help me in trigger code

Best Answer chosen by Yagna Deepthi
mukesh guptamukesh gupta
Hi Yagna,

Please use below trigger:-
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.

Thanks
Mukesh

All Answers

mukesh guptamukesh gupta
Hi Yagna,

Please use below trigger:-
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.

Thanks
Mukesh
This was selected as the best answer
AnkaiahAnkaiah (Salesforce Developers) 
Hi Yagna,

try with below code.
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'); 
 }    
}
 
}

If this helps, Please mark it as best answer.

Thanks!!​​​​​​​
Yagna DeepthiYagna Deepthi
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
mukesh guptamukesh gupta
Hi  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.

Thanks
Mukesh
Yagna DeepthiYagna Deepthi
Hi mukesh, Now the code is working..Thanks alot for ur great help!! Thanks, Yagna
feil edisonfeil edison
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.