You need to sign in to do that
Don't have an account?

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;
}
}
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;
}
}
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
What is your "Count_id" ? is it a roll-up on "Contact" object?
Thanks
Aniket
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.
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;
}
}
}
{
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;
}
}
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;
}
}