I am writing a trigger to roll up a formula field, Elapsed_Time_days__c, in a detail object, NEW_KIND_Ticket_timer__c, and have it display in a number field, Total_Time_Case_Elapsed_Days__c, on master object Case. This is to occur after every update for Case.

I have written the below, and it saves, and when I run unit test classes from developer console, it passes. However, in real life, when I actually edit a case, and then save, I get error:

Error: Invalid Data. Review all error messages below to correct your data. Apex trigger trigCaseElapsedTime caused an unexpected exception, contact your administrator: trigCaseElapsedTime: execution of AfterUpdate caused by: System.TypeException: Invalid id: 21.29: Trigger.trigCaseElapsedTime: line 16, column 1

The trigger is:

trigger trigCaseElapsedTime on Case (after update) {
map<id,Case> CaseMap = new map<id,Case> ([
       id in (SELECT RelatedCase__c FROM NEW_KIND_Ticket_timer__c)]); 

for ( sObject caserec : [ SELECT SUM(Elapsed_Time_days__c) TotalElapsedtimeDays

                        FROM NEW_KIND_Ticket_timer__c
                        WHERE RelatedCase__c in :CaseMap.keyset()
                 ]) { 
             case updatecaserecord = CaseMap.get((id)caserec.get('TotalElapsedtimeDays'));  
             updatecaserecord.Total_Time_Case_Elapsed_Days__c = double.valueOf(caserec.get('TotalElapsedtimeDays'));
             CaseMap.put((id)caserec.get('TotalElapsedtimeDays'), updatecaserecord);

     upsert CaseMap.values();


I am not sure where I have gone wrong.

  • 1
    Do you use a lookup or master/detail relationship? In case of lookup, could you have NEW_KIND_Ticket_timer__c with empty reference to case? Try also to avoid the select of case-map in the trigger, because you have the cases in trigger.newMap. Further I anticipate you'll need a second trigger on NEW_KIND_Ticket_timer__c or only there because that's what drives the rollup.
    – Uwe Heim
    Commented Jul 27, 2014 at 10:14
  • Thank you very much for your comment. eventually, our company decided not to use this.... so this became a non-issue. But It has been a learning experience. Commented Sep 2, 2014 at 21:10

1 Answer 1


I found a couple of possible issue which maybe not all apply. This said, here are my suggestions:

  • You probably need a trigger not on case but on the detail object.

  • If you want to go on with the case trigger, try to get rid of your own CaseMap and use trigger.newMap. Select all related detail records into an other map instead. Iterate over the details, sumup by matching case.id

  • Don't use Total_Time_Case_Elapsed_Days__c as a key for a map since if it possible at all (I never done it) duplicate sums might clash and messup your logic.

  • Check if you can use a master detail relationship. If possible you can use a rollup summary field an leave all that grunt work to salesforce.

  • Thank you very much for your reply. I will consider your answer, and give it a try Commented Jul 28, 2014 at 0:29
  • Unfortunately, formula fields do not work with rollups. But the other suggestions, I will attempt to try. Thank you Commented Jul 29, 2014 at 2:41

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .