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
Ankita dixitAnkita dixit 

Method does not exist or incorrect signature: void add(Account) from the type Set<Id>

What is this type of error means: what is possible solution?
trigger ApexTrigger10 on Contact (After insert,After Delete) {
    
    set<id> contactSet = new set<id>();
    List<Account> newAccountList = new List<Account>();
    
    if(trigger.isInsert && trigger.isAfter){
        for(Contact con:trigger.new){
            contactSet.add(con.Account);
         }
    }

    if(trigger.isDelete && trigger.isAfter){
        for(Contact con:trigger.old){
            contactSet.add(con.Account);
         }
    }
    List<Account> Acc= [SELECT id, name,(SELECT id,name from contacts) FROM Account WHERE id IN:contactSet];
    for( Account a : Acc)
    {
        a.Total_number_of_contacts__c= a.contacts.size();
        newAccountList.add(a);
    }
    update newAccountList;
}
 
Best Answer chosen by Ankita dixit
Maharajan CMaharajan C
Hi Ankita,

You have reffered the Account Lookup Api name wrongly in below line so you are getting this error...

contactSet.add(con.Account);     ==>    contactSet.add(con.AccountId);

Please try the below code:
 
trigger ApexTrigger10 on Contact (After insert,After Delete) {
    
    set<id> contactSet = new set<id>();
    List<Account> newAccountList = new List<Account>();
    
    if(trigger.isInsert && trigger.isAfter){
        for(Contact con:trigger.new){
            contactSet.add(con.AccountId);
         }
    }

    if(trigger.isDelete && trigger.isAfter){
        for(Contact con:trigger.old){
            contactSet.add(con.AccountId);
         }
    }
    List<Account> Acc= [SELECT id, name,(SELECT id,name from contacts) FROM Account WHERE id IN:contactSet];
    for( Account a : Acc)
    {
        a.Total_number_of_contacts__c= a.contacts.size();
        newAccountList.add(a);
    }
	
	if(!newAccountList.isEmpty())
		update newAccountList;
}

Thanks,
Maharajan.C

All Answers

Maharajan CMaharajan C
Hi Ankita,

You have reffered the Account Lookup Api name wrongly in below line so you are getting this error...

contactSet.add(con.Account);     ==>    contactSet.add(con.AccountId);

Please try the below code:
 
trigger ApexTrigger10 on Contact (After insert,After Delete) {
    
    set<id> contactSet = new set<id>();
    List<Account> newAccountList = new List<Account>();
    
    if(trigger.isInsert && trigger.isAfter){
        for(Contact con:trigger.new){
            contactSet.add(con.AccountId);
         }
    }

    if(trigger.isDelete && trigger.isAfter){
        for(Contact con:trigger.old){
            contactSet.add(con.AccountId);
         }
    }
    List<Account> Acc= [SELECT id, name,(SELECT id,name from contacts) FROM Account WHERE id IN:contactSet];
    for( Account a : Acc)
    {
        a.Total_number_of_contacts__c= a.contacts.size();
        newAccountList.add(a);
    }
	
	if(!newAccountList.isEmpty())
		update newAccountList;
}

Thanks,
Maharajan.C
This was selected as the best answer
Ankita dixitAnkita dixit
Thanks sir 🙏🙏 Now its working 🎉🎉 Ankita