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
anil Anil5anil Anil5 

Error: Compile Error: Identifier name is reserved: object at line 9 column 28

I am perform trigger operation  on Company obj, trigger acts like rollup summary functionallity--->sum
Company is chlid
client is parent    R.S:Lookup
How to slove this error and any forward link for trigger rollup summary functionallity

trigger rollupTrigger on Company__c (after insert, after update, after delete) {
    
    double totalSum = 0;
  
    Company__c myObjects = trigger.new;
    
    if(Trigger.isUpdate || Trigger.isInsert)
    {
            for(Company__c object : myObjects) //assume more than 1 record in trigger.new
            {
              for(Client__c parent : [select Id, Name from Client__c where Id = :object.Clients__c]) //get the parent object
              {
                for(Company__c childObject : [select Id, Name, Amount__c from Company__c where Clients__c = :parent.Id]) //get all the objects that belong to the parent object
                {
                  totalSum += childObject.Amount__c;   //sum the field of choice
                }
                parent.Sum__c = totalSum;   //set the total in the parent
                update parent                       // update the parent
              }
            }
    }
    else if(Trigger.isDelete)
    {
        Company__c [] oldObjects = Trigger.old;
        
      for(Company__c oldObject :oldObjects)    //assume deletion of more than 1 record
      {
        for (Client__c parentObject : [select id, Name, Sum__c where Id = :oldObject.Clients__c]) //get the parent object(s)
        {
          for (Company__c childObject : [select id, Name, Amount__c from Company__c where Clients__c = :parentObject.id])   //get the records that belong to the parent
          {
             totalSum += childObject.Amount__c;  //sum the fields after a record has been deleted
          }
            parentObject.Sum__c = totalSum;   //set new total in parent
            update parentObject;                      //update parent
        }   
      } //end oldObject for loop
    } //end else if
    
} //end trigger
GarryPGarryP
Hi,

Error is due to this line
" for(Company__c object : myObjects) //assume more than 1 record in trigger.new"
keyword OBJECT is reserved as it is generic datatype.


For your case you want to have aggregate results like ROLLUP summary which SHOULD be implemented using AGGREGATERESULTS in Salesforce. The for loops may hit the governor limit if you have large amount of records.

========================================
// Below Query will give you aggreagted results
Map<ID,String> mapIdAggresults = new map<Id,String>();
AggregateResult[] groupedResults  = [SELECT Sum(Amount__c),Client__c total  FROM Company__c  where ID in:Tigger.new GROUP BY Client__c];
//Loop on list and do desired manipulations
for (AggregateResult ar : groupedResults) {
    mapIdAggresults .put(ar.get('Cleint__c'),ar.get('Total'));
}

/*Query Parent records and use aboce map to update SUM__c field

replace  the above logic in your trigger code and you should be able to get desired results.

Suggestion : The logic that is written in you trigger should be moved to an APex class, This is best practise and recommended approach. in triggers just route logic to appropriate classes on the basis of trigger events/context