Skip to main content
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;

    }

 

}

  

   

       
10 个回答
  1. 2016年8月22日 11:20

    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;

    }

    }
0/9000