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
Vaibhav BabrekarVaibhav Babrekar 

Can anyone help me out with this requirement (Urgent)

Create an Account Record with Name = “ABC”. Create associated contacts. Create aCustom field called Contact Count on Account. Query on Contact where Account.Name =“ABC” and count the associated contacts. Update the custom field on Accounts with thatcount.
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Vaibhav,

Do you want trigger to update the count of contacts or you want to create the code for above scenerio in anonomous window?

Thanks,
 
Vaibhav BabrekarVaibhav Babrekar
Hi Sai Praveen,
Firstly thank you so much for replying my question. I want to execute this in Anonymous window. Can you please help me.

Thanks in Advance
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Vaibhav,

Can you try the below code in anonomous block.
 
Account acc=new Account();
acc.name='ABC';
insert acc;

List<Contact> conlist= new List<Contact>();

contact c1= new Contact();
c1.lastname='sample';
c1.accountid=acc.id;
conlist.add(c1);
contact c2= new Contact();
c2.lastname='sample';
c2.accountid=acc.id;
conlist.add(c2);
contact c3= new Contact();
c3.lastname='sample';
c3.accountid=acc.id;
conlist.add(c3);
insert conlist;

List<Account> acc1=[SELECT Id,Name,Contact_Count__c,(Select Id from Contacts) from Account where Id =:acc.id];
system.debug('accounr'+acc1);
system.debug('count'+acc.Contact_Count__c);
acc.Contact_Count__c =acc1[0].Contacts.size();
update acc;

Let me know if you face any issues.

If this solution helps, Please mark it as best answer.

Thanks,
CharuDuttCharuDutt
Hii Vaibhav
Try Below Code Trigger As Well As Anonymous Code
trigger NumberOfChild on Contact (After Insert,After Update,After Delete) {
    
	List<Account> accList=new List<Account>();

    Set<Id> setAccIds = new Set<Id>();
    if(Trigger.isInsert){
         if(trigger.isAfter){
        for(Contact con : Trigger.new){

            if(con.AccountId != null){
            setAccIds.add(con.AccountId);
            	}
			}
		}
    } 
    if(Trigger.isUpdate){
         if(trigger.isAfter){
        for(Contact con : Trigger.new){ 

            if(con.AccountId != null && con.AccountId !=Trigger.oldMap.get(con.Id).AccountId){
               	setAccIds.add(con.AccountId);
                setAccIds.add(Trigger.oldMap.get(con.Id).AccountId);
            	}
          
			}        
        }
    }
    if(Trigger.isDelete){
        if(trigger.isAfter){
        for(Contact con : Trigger.old){ 
            if(con.AccountId != null){
            setAccIds.add(con.AccountId);
            	}
        	}
        }
    }    
    for(Account acc :[Select id,Contact_Count__c ,Description ,(Select id from contacts) from Account where Id in : setAccIds]){
	string AccName = acc.name;
		if(AccName.Contains('ABC')){
        acc.Contact_Count__c = acc.contacts.size();
		acclist.add(acc);
		}
        
        
    }
    if(acclist.size()>0){
        update accList;     
    }
	}


##############################################################################
To Run In Anonymous Window


Account acc=new Account();
acc.name='ABC Test Account';
insert acc;

List<Contact> conlist= new List<Contact>();

contact c1= new Contact();
c1.lastname='Test Conatct 1';
c1.accountid=acc.id;
conlist.add(c1);
contact c2= new Contact();
c2.lastname='Test Conatct 2';
c2.accountid=acc.id;
conlist.add(c2);
contact c3= new Contact();
c3.lastname='Test Conatct 3';
c3.accountid=acc.id;
conlist.add(c3);
insert conlist;

List<Account> acc1=[SELECT Id,Name,Contact_Count__c,(Select Id from Contacts) from Account where Id =:acc.id];
system.debug('accounr'+acc1);
system.debug('count'+acc.Contact_Count__c);
acc.Contact_Count__c =acc1[0].Contacts.size();
update acc;
Please Mark It As Best Asnwer If it Helps
Thank You!