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
Rev NivasRev Nivas 

custom rollup summary

hi,
   scenario is implement rollup summary to a lookup relationship
   i have taken account---parent
                        contact---child
  my code is______ 
public class triggeronContact

    public list<id> accidlist{get; set;}
   
    public triggeroncontact(){
       
     accidlist = new list<id>();
 
    }
    public void Trigconmeth(list<contact> connew)
    {
       
        if(trigger.isinsert || trigger.isundelete)
        {
            for(contact con : connew)
            {   
                if(con.AccountId != null)  {
                accidlist.add(con.accountid);
                system.debug('*****accidlist are*****:'+accidlist);               
            }
        }
        }
        if(trigger.isdelete)
        {
            for(contact condel : [select id from contact] )
            {
                accidlist.add(condel.accountid);
               
            }
        }
        list<account> accupdatelist = new list<account>();
       
        for(account acc : [select total_contacts__c,(select id from contacts)from account where id =: accidlist])
        {
            acc.total_contacts__c = acc.contacts.size();
            accupdatelist.add(acc);
        }
        try
        {
            update accupdatelist;
        }  
        catch(exception e)
        {
            system.debug('Exception:'+e.getMessage());
        }
    }      
}
while executing below error is coming...line number 13 means  here...  if(trigger.isinsert || trigger.isundelete)
 User-added image
Sitanshu TripathiSitanshu Tripathi
if(trigger.isdelete)
        {
            for(contact condel : [select id from contact] )
            {
                accidlist.add(condel.accountid);
               
            }
        }



Change your query and get Account ID.
like - Select id, AccountId from contact where AccountId != null

Please mark as a Best Answer if Error is not coming ye on your Line no 13.
Rev NivasRev Nivas
no same error is coming.......total_contacts__c is the custom field in account object(parent)