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
karunakarreddy bade 8karunakarreddy bade 8 

Illegal assignment from LIST<Account> to LIST<Account> in Apex Trigger

When I am execute this Trigger I am getting an error at line Query Illegal assignment from LIST<Account> to LIST<Account> at line in Salesforce Trigger


trigger ContactsOnAccount on Contact (after insert, after delete) {
    Set<Id> aId = new Set<Id>();
   
    if(Trigger.isInsert){
        for(Contact opp : Trigger.New){
            aId.add(opp.AccountId);
        }
        List<Account> acc = [select id,Count_id__c from Account where Id in:aId];
        List<Contact> con = [select id from contact where AccountId in :aId];
       
        for(Account a : acc){
            a.Count_id__c=con.size();
           
        }update acc;
    }
   
    if(Trigger.isDelete){
        for(Contact opp : Trigger.old){
            aId.add(opp.AccountId);
        }
        List<Account> acc = [select id,Count_id__c from Account where Id in:aId];
        List<Contact> con = [select id from contact where AccountId in :aId];
       
        for(Account a : acc){
            a.Count_id__c=con.size();
           
        }update acc;
    }
 
}
  
   
       
Best Answer chosen by karunakarreddy bade 8
madhu sannapamadhu sannapa

hi karunakar reddy this code working



trigger countoncontactupdate on contact(After insert,after update,After delete)
{
if(trigger.IsAfter && trigger.isinsert)
{
set<ID>Idset1=new set<Id>();
for(contact con:trigger.new)
{
Idset1.add(con.AccountID);
}
list<Account>accupdatelist=new list<Account>();
list<Account>acclist=new list<Account>([select Id,Name,Count_id__c,(select id,name,AccountID from Contacts ) from Account where ID In:Idset1]);
for(Account acc:acclist){
acc.Count_id__c=Acc.contacts.size();
accupdatelist.add(acc);
}
update accupdatelist;
}
if(trigger.IsAfter && trigger.isupdate)
{
set<ID>IDset1=new set<ID>();
for(contact con:trigger.new)
{
if(con.AccountID!=trigger.oldmap.get(con.ID).AccountID)
{
Idset1.add(con.AccountID);
idset1.add(trigger.oldmap.get(con.ID).AccountID);
}
}
list<Account>accupdatelist=new list<Account>();
list<Account>acclist=new list<Account>([select Id,Name,Count_id__c,(select id,name,AccountID from Contacts ) from Account where ID In:Idset1]);
for(Account acc:acclist){
acc.Count_id__c=Acc.contacts.size();
accupdatelist.add(acc);
}
update accupdatelist;
}
if(trigger.IsAfter && trigger.isdelete)
{
set<ID>Idset1=new set<Id>();
for(contact con:trigger.old)
{
idset1.add(con.AccountID);
}
list<Account>accupdatelist=new list<Account>();
list<Account>acclist=new list<Account>([select Id,Name,Count_id__c,(select id,name,AccountID from Contacts ) from Account where ID In:Idset1]);
for(Account acc:acclist){
acc.Count_id__c=Acc.contacts.size();
accupdatelist.add(acc);
}
update accupdatelist;
}
}

All Answers

Aniket Malvankar 10Aniket Malvankar 10
Hello Karuna,

What is your "Count_id" ? is it a roll-up on "Contact" object?

Thanks
Aniket
karunakarreddy bade 8karunakarreddy bade 8
No
 
Aniket Malvankar 10Aniket Malvankar 10
Could you please add more information on Cound_id field?
Amit Chaudhary 8Amit Chaudhary 8
Hi karunakarreddy bade 8 ,

Please check in org you have any class with "Account" Name if yes then please rename the class or delete the class and try again above code.
 
karunakarreddy bade 8karunakarreddy bade 8
thanks amit i will working know
karunakarreddy bade 8karunakarreddy bade 8
Hi,

 error in this below code:

Illegal assignment from Integer to String at line 12 column 13
 
trigger ContactsOnAccount on Contact (after insert, after delete,after update) {
    Set<Id> aId = new Set<Id>();

    if(Trigger.isInsert){
        for(Contact opp : Trigger.New){
            aId.add(opp.AccountId);
        }
        List<Account> acc = [select id,Count_id__c from Account where Id in:aId];
        List<Contact> con = [select id from contact where AccountId in :aId];

        for(Account a : acc){
            a.Count_id__c =con.size();

        }update acc;
    }

    if(Trigger.isDelete){
        for(Contact opp : Trigger.old){
            aId.add(opp.AccountId);
        }
        List<Account> acc = [select id,Count_id__c from Account where Id in:aId];
        List<Contact> con = [select id from contact where AccountId in :aId];

        for(Account a : acc){
            a.Count_id__c =con.size();

        }update acc;
    }

    if(Trigger.isUpdate){
       Set<Id> OldAId = new Set<Id>(); 
        for(Contact opp : Trigger.new){
        if(opp.AccountId != Trigger.oldMap.get(opp.id).AccountId || 

opp.Primary_Contact__c != Trigger.oldMap.get(opp.id).Primary_Contact__c)
            aId.add(opp.AccountId);
            OldAId.add(Trigger.oldMap.get(opp.id).AccountId);

        }
        if(!aId.isEmpty()){
       
        List<Account> acc = [select id,Count_id__c from Account where Id in:aId];
        
        List<Contact> con = [select id from contact where AccountId in :aId];

        

        
        List<Account> Oldacc = [select id,Count_id__c from Account where Id in:OldAId];

        List<Contact> OldCon = [select id from contact where AccountId in :OldAId];

        
        for(Account a : acc){
            a.Count_id__c =con.size();

        }update acc;

       
        for(Account a : Oldacc){
            a.Count_id__c =OldCon.size();

        }update Oldacc;
        }
    }
}

 
madhu sannapamadhu sannapa
trigger countoncontact on contact(After insert,after update,After delete)
{
if(trigger.IsAfter && trigger.isinsert)
{
set<ID>Idset1=new set<Id>();
for(contact con:trigger.new)
{
Idset1.add(con.AccountID);
}
list<Account>accupdatelist=new list<Account>();
list<Account>acclist=new list<Account>([select Id,Name,Count_id__c,(select id,name,AccountID from Contacts ) from Account where ID In:Idset1]);
for(Account acc:acclist){
acc.Count_id__c=Acc.contacts.size();
accupdatelist.add(acc);
}
update accupdatelist;
}
if(trigger.IsAfter && trigger.isupdate)
{
set<ID>IDset1=new set<ID>();
for(contact con:trigger.new)
{
if(con.AccountID!=trigger.oldmap.get(con.ID).AccountID)
{
Idset1.add(con.AccountID);
idset1.add(trigger.oldmap.get(con.ID).AccountID);
}
}
list<Account>accupdatelist=new list<Account>();
list<Account>acclist=new list<Account>([select Id,Name,Count_id__c,(select id,name,AccountID from Contacts ) from Account where ID In:Idset1]);
for(Account acc:acclist){
acc.Count_id__c=Acc.contacts.size();
accupdatelist.add(acc);
}
update accupdatelist;
}
if(trigger.IsAfter && trigger.isdelete)
{
set<ID>Idset1=new set<Id>();
for(contact con:trigger.old)
{
idset1.add(con.AccountID);
}
list<Account>accupdatelist=new list<Account>();
list<Account>acclist=new list<Account>([select Id,Name,Count_id__c,(select id,name,AccountID from Contacts ) from Account where ID In:Idset1]);
for(Account acc:acclist){
acc.Count_id__c=Acc.contacts.size();
accupdatelist.add(acc);
}
update accupdatelist;
}
}




 
madhu sannapamadhu sannapa

hi karunakar reddy this code working



trigger countoncontactupdate on contact(After insert,after update,After delete)
{
if(trigger.IsAfter && trigger.isinsert)
{
set<ID>Idset1=new set<Id>();
for(contact con:trigger.new)
{
Idset1.add(con.AccountID);
}
list<Account>accupdatelist=new list<Account>();
list<Account>acclist=new list<Account>([select Id,Name,Count_id__c,(select id,name,AccountID from Contacts ) from Account where ID In:Idset1]);
for(Account acc:acclist){
acc.Count_id__c=Acc.contacts.size();
accupdatelist.add(acc);
}
update accupdatelist;
}
if(trigger.IsAfter && trigger.isupdate)
{
set<ID>IDset1=new set<ID>();
for(contact con:trigger.new)
{
if(con.AccountID!=trigger.oldmap.get(con.ID).AccountID)
{
Idset1.add(con.AccountID);
idset1.add(trigger.oldmap.get(con.ID).AccountID);
}
}
list<Account>accupdatelist=new list<Account>();
list<Account>acclist=new list<Account>([select Id,Name,Count_id__c,(select id,name,AccountID from Contacts ) from Account where ID In:Idset1]);
for(Account acc:acclist){
acc.Count_id__c=Acc.contacts.size();
accupdatelist.add(acc);
}
update accupdatelist;
}
if(trigger.IsAfter && trigger.isdelete)
{
set<ID>Idset1=new set<Id>();
for(contact con:trigger.old)
{
idset1.add(con.AccountID);
}
list<Account>accupdatelist=new list<Account>();
list<Account>acclist=new list<Account>([select Id,Name,Count_id__c,(select id,name,AccountID from Contacts ) from Account where ID In:Idset1]);
for(Account acc:acclist){
acc.Count_id__c=Acc.contacts.size();
accupdatelist.add(acc);
}
update accupdatelist;
}
}
This was selected as the best answer
ReddyReddy
u can delete Account class. and its working ur code
ReddyReddy
and also u can chage or rename ur classname