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
Sambit SoravSambit Sorav 

Create an account with name 'test account' & create a field on account - > number of contacts. Write a trigger such that when whenever a new contact is created it is automatically added to 'test account'.

Hi, 

Create an account with name 'test account' & create a field on account - > number of contacts. Write a trigger such that when whenever a new contact is created it is automatically added to 'test account'.

Please help me writing this trigger. I have written the following code. Pls tell me where am I going wrong.
Thanks

trigger InsertContact1 on Contact (after insert) {
      
    Account acList = [Select id, Name from Account where Name = 'Test Account'];
    List<Contact> contList = new List<contact>();
    
    for(Contact c: trigger.new){
        contList.add(c);
    }    insert contList;
    
    if(contList.size()>0){
        for(Contact c: contList){
            c.AccountId = acList.id;
            acList.Number_Of_Contacts__c = integer.valueOf(contList.size());
    }
    }
}
Best Answer chosen by Sambit Sorav
Surya GSurya G
Hi Akshay,

This can be done with before insert trigger
1, need not to insert contact again, inside contact trigger, that will create recursion.
2, You are trying to update the number of contacts field inside for loop, which will show only the contacts inserted at that point of time. it will not sum up the total number of contacts for that account. 

you can try this below code.
trigger InsertContact1 on Contact (before insert) {
    
    Account acList = [Select id, Name, Number_Of_Contacts__c, (select id from contacts) from Account where Name='Test Account'];
    
    for(contact con : trigger.new) {
        con.accountId= acList.Id;
    }
    
    acList.Number_Of_Contacts__c = acList.contacts.size();
    update acList;
        
        
}
Thanks
Surya G

All Answers

CharuDuttCharuDutt
Hii Akshay Garg
Try Below Code
trigger InsertContact1 on Contact (before insert) {
      
    Account acList = [Select id, Name from Account where Name = 'Test Account' limit 1];
    List<Account> accList=new List<Account>();
    for(Contact c: trigger.new){
       c.AccountId = acList.Id;
    }  
    
    for(Account acc :[Select id,Number_Of_Contacts__c,(Select id,name from contacts) from Account where Id = :acList.Id]){
        acc.Number_Of_Contacts__c = acc.contacts.size();
        acclist.add(acc);
    }
    if(acclist.size()>0){
        update accList;     
    }
}
Please Mark It As Best Answer If It Helps
Thank You!

 
Surya GSurya G
Hi Akshay,

This can be done with before insert trigger
1, need not to insert contact again, inside contact trigger, that will create recursion.
2, You are trying to update the number of contacts field inside for loop, which will show only the contacts inserted at that point of time. it will not sum up the total number of contacts for that account. 

you can try this below code.
trigger InsertContact1 on Contact (before insert) {
    
    Account acList = [Select id, Name, Number_Of_Contacts__c, (select id from contacts) from Account where Name='Test Account'];
    
    for(contact con : trigger.new) {
        con.accountId= acList.Id;
    }
    
    acList.Number_Of_Contacts__c = acList.contacts.size();
    update acList;
        
        
}
Thanks
Surya G
This was selected as the best answer
Suraj Tripathi 47Suraj Tripathi 47

Hi,

Try this below code. You have not query the Number_Of_Contacts__c it will give you error.

Account acList = [Select id,Number_Of_Contacts__c , Name from Account where Name = 'Test Account' limit 1];
    List<Contact> contList = new List<contact>();
    
    for(Contact c: trigger.new){
        contList.add(c);
    }    
    
    if(contList.size()>0){
        for(Contact c: contList){
            c.AccountId = acList.id;
      
    }
        if(contList.size()>0){
                update contList;
    }
        if(acList.numberOfContacts__c!=null){
            acList.Number_Of_Contacts__c =acList.Number_Of_Contacts__c + integer.valueOf(contList.size());
        }else{
              acList.Number_Of_Contacts__c = integer.valueOf(contList.size());

        }
        update acList;
        
        
    }

Please mark it as the Best Answer if it helps you

Thank You