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

Update the same Opportunity in a batch multiple times via Data Loader

What happens in the following scenario:

I have a custom object, call it MDP, that is associated with an opportunity.  There can be many MDP records for each opportunity.  I have written a trigger that updates fields in the Opportunity when certain fields in a MDP record is modified.

The problem is that the data loader does not seem to be updating the same opportunity if two MDP records associated to the same Opportunity is updated.  What I would need is each time the MDP record is updated, the Opportunity is updated.  Regardless if it is the same opportunity in a batch update via dataloader.  Is it possible?
There's nothing that precludes this, what does your trigger code look like?
if (Trigger.isUpdate) {
  for (Integer i = 0; i <; i++) {
         for (Media_Delivery_Placement__c {
    if ( (Trigger.old[i].Contracted_Total__c !=[i].Contracted_Total__c)
       || (Trigger.old[i].Earned_To_Date__c !=[i].Earned_To_Date__c)
       || (Trigger.old[i].Impressions_Earned_Thru__c !=[i].Impressions_Earned_Thru__c)
       || (Trigger.old[i].Start_Date__c !=[i].Start_Date__c)
       || (Trigger.old[i].End_Date__c !=[i].End_Date__c)
     || (Trigger.old[i].Impressions_Calculated__c !=[i].Impressions_Calculated__c)
     || (Trigger.old[i].Expected_Delivery_To_Date__c !=[i].Expected_Delivery_To_Date__c)
       || (Trigger.old[i].Expected_Earnings_To_Date__c !=[i].Expected_Earnings_To_Date__c)
       || (Trigger.old[i].Impressions_Earned_to_Date__c !=[i].Impressions_Earned_to_Date__c) ) {

     //update the Media Delivery totals in this current Opportunity
          opp = [select id, Media_Delivery_Total__c, Total_Earned__c from Opportunity where Id= :mdc.Opportunity__c limit 1];
    System.Debug('SKM-Opportunity = ' + opp);
    for (Media_Delivery_Placement__c mds : [SELECT id, Name, Opportunity__c, Contracted_Total__c, Earned_to_Date__c,
      Impressions_Earned_Thru__c, Impressions_Earned_to_Date__c, Impressions_Calculated__c,
      Expected_Delivery_To_Date__c, Expected_Earnings_To_Date__c, Start_Date__c, End_Date__c
      FROM Media_Delivery_Placement__c WHERE Opportunity__c =]) {

      //Add Up the Rollups for Media Delivery Placement object
      Media_Delivery_Total = Media_Delivery_Total + mds.Contracted_Total__c;
      Total_Earned = Total_Earned + mds.Earned_to_Date__c;
      Total_Impressions = Total_Impressions + mds.Impressions_Calculated__c;
      Total_Impressions_Earned = Total_Impressions_Earned + mds.Impressions_Earned_to_Date__c;
      Total_Expected_Delivery_To_Date = Total_Expected_Delivery_To_Date + mds.Expected_Delivery_To_Date__c;
      Total_Expected_Earnings_To_Date = Total_Expected_Earnings_To_Date + mds.Expected_Earnings_To_Date__c;
      if (mds.Impressions_Earned_Thru__c > Media_Delivery_Impressions_Earned_Thru) {
      Media_Delivery_Impressions_Earned_Thru = mds.Impressions_Earned_Thru__c;
      if (mds.End_Date__c > Media_Delivery_End_Date) {
        Media_Delivery_End_Date = mds.End_Date__c;
      if (mds.Start_Date__c < Media_Delivery_Start_Date) {
      Media_Delivery_Start_Date = mds.Start_Date__c;
    if (opp != null) {
    //Write the Rollup Totals to the opportunity
     opp.Media_Delivery_Total__c = Media_Delivery_Total;
     opp.Total_Earned__c = Total_Earned;
     opp.Media_Delivery_Impressions_Earned_Thru__c = Media_Delivery_Impressions_Earned_Thru;
     opp.Total_Impressions__c = Total_Impressions;
     opp.Total_Impressions_Earned__c = Total_Impressions_Earned;
     opp.Media_Delivery_Start_Date__c = Media_Delivery_Start_Date;
     opp.Media_Delivery_End_Date__c = Media_Delivery_End_Date;
     opp.Total_Expected_Delivery_To_Date__c = Total_Expected_Delivery_To_Date;
     opp.Media_Delivery_Expected_Earnings_to_Date__c = Total_Expected_Earnings_To_Date;
     update opp;   //save the opportunity
     //UpdatedOpps.add(opp); //save the opportunity to the list

You're calculating a rollup based only on the rows in the db (i assume you can't use summary fields for a good reason?), but in a batch update, you need to also take into account the other rows that apply to that opportunity that are in the batch, but not yet in the db.