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
Aron Schor 24Aron Schor 24 

Issue with using two triggers to update a record and then related records

Hi, I am using two Triggers and it seems the record that is initially being updated does not stay in sync with the other records.
Example, for Pitch (custom) record 123, let's say I add a $2 Opportunity and then Property tied to Property Group A.
What happens is Total Amount is $102 for all related records EXCEPT 123 which shows $100.
It seems the amount is set for that record prior to it being tied to the Property Group.
After, if I update record 123 by making any change, then it becomes $102.  I want it to become $102 with these triggers.
I should say I am no expert and have been getting assistence from the forums (thanks Prateek P!) and ChatGPT.
Thanks.

trigger PropertyCalc1 on Pitch__c (before insert, before update) {

  // Get the records that are being triggered on.
  List<Pitch__c> records = Trigger.new;

  // Get the property group amount for each record.
  for (Pitch__c record : records) {
    List<AggregateResult> propertyGroupAmounts = [
      SELECT SUM(Opportunity__r.Amount)
      FROM Pitch__c
      WHERE Property_Group__c = :record.Property_Group__c
      GROUP BY propertyhighspot__r.Property_Group_Text__c
    ];

    // Set the property group amount for each record.
    if (propertyGroupAmounts != null && propertyGroupAmounts.size() > 0) {
      record.Property_Group_Amount__c = (Decimal)propertyGroupAmounts[0].get('expr0');
    } else {
      // Set the property group amount to 0 if there is no amount.
      record.Property_Group_Amount__c = 0;
    }
  }
}

trigger PropertyCalc2 on Pitch__c (after update) {

  // Get the records that are being triggered on.
  List<Pitch__c> records = Trigger.new;

  // Check if the trigger is currently running.
  if (RecursiveTriggerHandler.isFirstTime) {
    // Set isFirstTime to false to prevent recursion.
    RecursiveTriggerHandler.isFirstTime = false;

    // Update all records with the same value for Property_Group__c.
    List<Pitch__c> matchingRecordsToUpdate = new List<Pitch__c>();
    for (Pitch__c record : records) {
      if (record.Property_Group__c != null) {
        // Get all records with the same value for Property_Group__c.
        List<Pitch__c> matchingRecords = [
          SELECT Id, Property_Group_Amount__c
          FROM Pitch__c
          WHERE Property_Group__c = :record.Property_Group__c
          AND Id != :record.Id
        ];

        // Update the property group amount for all matching records.
        for (Pitch__c matchingRecord : matchingRecords) {
          matchingRecord.Property_Group_Amount__c = record.Property_Group_Amount__c;
          matchingRecordsToUpdate.add(matchingRecord);
        }
      }
    }

    // Update all matching records outside of the trigger context.
    if (!matchingRecordsToUpdate.isEmpty()) {
      update matchingRecordsToUpdate;
    }
  }
}

                     
Maverick BlazeMaverick Blaze
Finding same issue at TM WhatsApp (https://apkwa.net/tm-whatsapp/).