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
Jon FoyJon Foy 

Code Error Help - writing a Trigger for a rollup summary on a cross object formula field.

Here's my code.  Getting the following error: "Compile Error: Variable does not exist: ts.case_id @ line 9 column 79.  That is the correct field.  Any help would be greatly appriciated.

trigger UpdateCaseBillableAmount on TimeSlip__c (after insert, after update) {
     Integer rollUpAmount;
     for(TimeSlip__c ts : Trigger.New){
          if(ts.Billable_Amount__c != Null){
               rollUpAmount += ts.Billable_Amount__c;     //Suppose you want to add all in Rollup logic
          }
     }
     if(rollUpAmount != Null){
          Case c = [select id, total_billable_amount__c from case where id =: ts.case_id];
          c.total_billable_amount__c = rollUpAmount;
          update c;
     }
}

NaveenReddyNaveenReddy
Hi,

 You are trying to access the variable ts.case_id from outside for loop, which obviously results in variable does not exist error.

Keep the below code in for loop it will work.

if(rollUpAmount != Null){
          Case c = [select id, total_billable_amount__c from case where id =: ts.case_id];

And we should not use DML statements within for loop it fires a governer limit.Instead use Lists or Maps and execute DML operation on them out side for loop.

Regards,
Naveen
SSE , Salesforce CI expert group
http://www.autorabit.com"
Jon FoyJon Foy
Now I'm getting an error: illegal assignment from decimal to integer @ line 5 column 16:
rollUpAmount += ts.Billable_Amount__c;
NaveenReddyNaveenReddy
You have defined rollUpAmount as  an integer.
ts.Billable_Amount__c will return a currency which contains decimals also.Change the data type of rollUpAmount.