+ Start a Discussion
Lisa Horne 1Lisa Horne 1 

Help with this Roll up Trigger error

I needed a trigger that triggered a roll up summary on the Accounts (Total_Active_Contracts__c field)  from Contracts(Active_Contract_Count__c Field).  I keep getting this error.... could someone please help?

Error Error: Compile Error: Invalid field: 'Account' at line 25 column 27



trigger ContractRollUpActiveContractCount on Account (after delete, after insert, after update) {

  //Limit the size of list by using Sets which do not contain duplicate elements
  set<id> AccountIds = new set<id>();

  //When adding new Contracts or updating existing Contracts
  if(trigger.isInsert || trigger.isUpdate){
    for(Contract p : trigger.new){
      AccountIds.add(p.Account);
    }
  }

  //When deleting Contracts
  if(trigger.isDelete){
    for(Contract p : trigger.old){
      AccountIds.add(p.Account);
    }
  }

  //Map will contain one Contract Id to one sum value
  map<id,double> AccountMap = new map <id,double>();

  //Produce a sum of Total_Active_Contracts__c and add them to the map
  //use group by to have a single Contract Id with a single sum value
  for(AggregateResult q : [select Account,sum(Active_Contract_Count__c)
    from Contract where Account IN :AccountIdsgroup group by Account]){
      AccountMap.put((Id)q.get('Account'),(Double)q.get('expr0'));
  }

  List AccountsToUpdate = new List();

  //Run the for loop on Contract using the non-duplicate set of Contract Ids
  //Get the sum value from the map and create a list of Opportunities to update
  for(Account o : [Select Id, Total_Active_Contracts__c from Account where Id IN :AccountIds]){
   Double Active_Contract_Count__cSum = AccountMap.get(o.Id);
    o.Total_Active_Contracts__c = ContractSum;
    AccountToUpdate.add(o);
  }

  update AccountToUpdate;
logontokartiklogontokartik
Hi Lisa,

Your trigger is defined on Account

trigger ContractRollUpActiveContractCount on Account

and you are iterating on Contracts.

for(Contract p : trigger.new){
Can you please correct this and see if it works. 

Thank you.

Lisa Horne 1Lisa Horne 1
Thanks for your response!

Still getting error on line 25.

User-added image
logontokartiklogontokartik
Hi Lisa,

The field name is AccountId on Contract for referring Account. Account is the Relationship name.

Thank you.