+ Start a Discussion

Apex Trigger - Script Exception due to multiple records referencing the same record

I have multiple custom objects.  In our Sandbox, Policy is the Master Object to both Claim and Coverage.  Coverage (in some cases) can be considered to be like a master to Claim so I have written Apex Triggers to act as a rollupsummary of all claim records that have a lookup to a specific Coverage record.  This trigger works great when we are manually update the claim records.


We use another system in which we import a refresh of data on a daily basis through a CSV file using the data management tool in SalesForce.  This currently works great in our production environment but in Sandbox we are developing new Master-Detail Relationships and Apex Triggers which appear to be causing an issue with this daily import.


In our business, there are multiple Claims that can have the same Coverage referenced.  When the Claim refresh file is imported , I recieve an exception error on the trigger because the trigger is for AfterUpdate.  I believe this is happening because the refresh file is updating multiple claim records at once that reference the same coverage and the apex trigger is firing AfterUpdate for for a single Coverage record and doesn't know how to handle the multiple Claim records that are causing it to fire.  (I hope this makes sense.)


Is there anyway to refresh multiple records at once that reference the same record of another object in which an AfterUpdate apex trigger is fired?


Any help on this is greatly appreciated.




I believe I need to add Map and/or List to my code so that every time it updates it bring a unique ID for the coverage (this way even if its the same Coverage it identifies it differently and allows this type of bulk update).  I have being reading about Maps and Lists but can't figure out how to add it and get the code to work.  Below is the code for my trigger that currently works when the updates are done individually.  Any ideas on how I can modify it to support a Map to permit the bulk update involving the same Coverage that is being updated?






//This trigger includes all financial rollups to the Coverage

trigger UpdateRollupsToCoverage on Claim__c (after update)

//Define variables
{double sumCoverageTotalExpenseOS = 0.0;
double sumCoverageTotalExpensePD = 0.0;
double sumCoverageTotalLegalOS = 0.0;
double sumCoverageTotalLegalPD = 0.0;
double sumCoverageTotalIndemnityOS = 0.0;
double sumCoverageTotalIndemnityPD = 0.0;
Coverage__c [] coveragesToUpdate = new Coverage__c[]{};

{Claim__c [] teNew = trigger.new;

for(Claim__c te : teNew)
{for (Coverage__c claimCoverage : [select Id, Name, Coverage_Total_Expense_O_S__c from Coverage__c where Id = :te.Claim_Coverage__c])

//Sum all the claimCoverage entries
//Total Expense OS
for (Claim__c expenseOSs: [select Id, Expense_OS__c from Claim__c where Claim_Coverage__c = :claimCoverage.id])
{sumCoverageTotalExpenseOS += expenseOSs.Expense_OS__c;}
claimCoverage.Coverage_Total_Expense_O_S__c = sumCoverageTotalExpenseOS;
//Total Expense PD
for (Claim__c expensePDs: [select Id, Expense_PD__c from Claim__c where Claim_Coverage__c = :claimCoverage.id])
{sumCoverageTotalExpensePD += expensePDs.Expense_PD__c;}
claimCoverage.Coverage_Total_Expense_PD__c = sumCoverageTotalExpensePD;
//Total Legal OS
for (Claim__c legalOSs: [select Id, Legal_OS__c from Claim__c where Claim_Coverage__c = :claimCoverage.id])
{sumCoverageTotalLegalOS += legalOSs.Legal_OS__c;}
claimCoverage.Coverage_Total_Legal_O_S__c = sumCoverageTotalLegalOS;
//Total Legal PD
for (Claim__c legalPDs: [select Id, Legal_PD__c from Claim__c where Claim_Coverage__c = :claimCoverage.id])
{sumCoverageTotalLegalPD += legalPDs.Legal_PD__c;}
claimCoverage.Coverage_Total_Legal_PD__c = sumCoverageTotalLegalPD;
//Total Indemnity OS
for (Claim__c indemnityOSs: [select Id, Indemnity_OS__c from Claim__c where Claim_Coverage__c = :claimCoverage.id])
{sumCoverageTotalIndemnityOS += indemnityOSs.Indemnity_OS__c;}
claimCoverage.Coverage_Total_Indemnity_O_S__c = sumCoverageTotalIndemnityOS;
//Total Indemnity PD
for (Claim__c indemnityPDs: [select Id, Indemnity_PD__c from Claim__c where Claim_Coverage__c = :claimCoverage.id])
{sumCoverageTotalIndemnityPD += indemnityPDs.Indemnity_PD__c;}
claimCoverage.Coverage_Total_Indemnity_PD__c = sumCoverageTotalIndemnityPD;

//add claimCoverage to list to be updated outside of the loop

//commit the changes to Salesforce
update coveragesToUpdate;}