Skip to main content
Fixing code formatting
Source Link
Derek F
  • 64.1k
  • 15
  • 55
  • 104

///Note: Salesforce has built in validation rule to prevent deletion Accounts with related Contacts/ Opportunities.
///Another option is to use Record triggered flow

trigger preventAccountDeletion on Account (before delete) {
  // Check if the trigger is before delete
  if (Trigger.isBefore && Trigger.isDelete) {
    // Query for account IDs that have related contacts
    List<AggregateResult> accountWithContact = [
      SELECT AccountId
      FROM Contact
      WHERE AccountId IN :Trigger.oldMap.keySet()
      GROUP BY AccountId
      HAVING COUNT(Id) > 0
    ];
    // Throw an error if there are accounts with related contacts
    if (accountWithContact.size() > 0) {
      Trigger.old[0].addError(accountWithContact.size() + ' Accounts cannot be deleted because they have related contacts.');
    }
  }
}

trigger preventAccountDeletion on Account (before delete) {
    // Check if the trigger is before delete
    if (Trigger.isBefore && Trigger.isDelete) {
        // Query for account IDs that have related contacts
        List<AggregateResult> accountWithContact = [
            SELECT AccountId
            FROM Contact
            WHERE AccountId IN :Trigger.oldMap.keySet()
            GROUP BY AccountId
            HAVING COUNT(Id) > 0
        ];

        // Throw an error if there are accounts with related contacts
        if (accountWithContact.size() > 0) {
            Trigger.old[0].addError(accountWithContact.size() + ' Accounts cannot be deleted because they have related contacts.');
        }
    }
}

///Note: Salesforce has built in validation rule to prevent deletion Accounts with related Contacts/ Opportunities.
///Another option is to use Record triggered flow

trigger preventAccountDeletion on Account (before delete) {
  // Check if the trigger is before delete
  if (Trigger.isBefore && Trigger.isDelete) {
    // Query for account IDs that have related contacts
    List<AggregateResult> accountWithContact = [
      SELECT AccountId
      FROM Contact
      WHERE AccountId IN :Trigger.oldMap.keySet()
      GROUP BY AccountId
      HAVING COUNT(Id) > 0
    ];
    // Throw an error if there are accounts with related contacts
    if (accountWithContact.size() > 0) {
      Trigger.old[0].addError(accountWithContact.size() + ' Accounts cannot be deleted because they have related contacts.');
    }
  }
}

trigger preventAccountDeletion on Account (before delete) {
    // Check if the trigger is before delete
    if (Trigger.isBefore && Trigger.isDelete) {
        // Query for account IDs that have related contacts
        List<AggregateResult> accountWithContact = [
            SELECT AccountId
            FROM Contact
            WHERE AccountId IN :Trigger.oldMap.keySet()
            GROUP BY AccountId
            HAVING COUNT(Id) > 0
        ];

        // Throw an error if there are accounts with related contacts
        if (accountWithContact.size() > 0) {
            Trigger.old[0].addError(accountWithContact.size() + ' Accounts cannot be deleted because they have related contacts.');
        }
    }
}
deleted 4 characters in body
Source Link

 

///Note: Salesforce has built in validation rule to prevent deletion Accounts with related Contacts/ Opportunities.
///Another option is to use Record triggered flow

   trigger preventAccountDeletion on Account (before delete) {
      // Check if the trigger is before delete
  if   if (Trigger.isBefore &&&& Trigger.isDelete) {
          // Query for account IDs that have related contacts
    List<AggregateResult>     List<AggregateResult> accountWithContact = [
      SELECT       SELECT AccountId
      FROM       FROM Contact
      WHERE       WHERE AccountId IN :Trigger.oldMap.keySet()
      GROUP       GROUP BY AccountId
      HAVING       HAVING COUNT(Id) >> 0
    ];     ];
          // Throw an error if there are accounts with related contacts
    if     if (accountWithContact.size() >> 0) {
      Trigger       Trigger.old[0].addError(accountWithContact.size() + ' Accounts cannot be deleted because they have related contacts.');
          }
      }
   }
  

 

 

///Note: Salesforce has built in validation rule to prevent deletion Accounts with related Contacts/ Opportunities.
///Another option is to use Record triggered flow

 trigger preventAccountDeletion on Account (before delete) {
   // Check if the trigger is before delete
  if (Trigger.isBefore && Trigger.isDelete) {
     // Query for account IDs that have related contacts
    List<AggregateResult> accountWithContact = [
      SELECT AccountId
      FROM Contact
      WHERE AccountId IN :Trigger.oldMap.keySet()
      GROUP BY AccountId
      HAVING COUNT(Id) > 0
    ];
     // Throw an error if there are accounts with related contacts
    if (accountWithContact.size() > 0) {
      Trigger.old[0].addError(accountWithContact.size() + ' Accounts cannot be deleted because they have related contacts.');
     }
   }
 }
 

 

///Note: Salesforce has built in validation rule to prevent deletion Accounts with related Contacts/ Opportunities.
///Another option is to use Record triggered flow

  trigger preventAccountDeletion on Account (before delete) {
  // Check if the trigger is before delete
  if (Trigger.isBefore && Trigger.isDelete) {
    // Query for account IDs that have related contacts
    List<AggregateResult> accountWithContact = [
      SELECT AccountId
      FROM Contact
      WHERE AccountId IN :Trigger.oldMap.keySet()
      GROUP BY AccountId
      HAVING COUNT(Id) > 0
    ];
    // Throw an error if there are accounts with related contacts
    if (accountWithContact.size() > 0) {
      Trigger.old[0].addError(accountWithContact.size() + ' Accounts cannot be deleted because they have related contacts.');
    }
  }
  }
 

added 181 characters in body
Source Link

        trigger preventAccountDeletion on Account (before delete) {
  // Check if the trigger is before delete
  if (Trigger.isBefore && Trigger.isDelete) {
    // Query for account IDs that have related contacts
    List<AggregateResult> accountWithContact = [
      SELECT AccountId
      FROM Contact
      WHERE AccountId IN :Trigger.oldMap.keySet()
      GROUP BY AccountId
      HAVING COUNT(Id) > 0
    ];
    // Throw an error if there are accounts with related contacts
    if (accountWithContact.size() > 0) {
      Trigger.old[0].addError(accountWithContact.size() + ' Accounts cannot be deleted because they have related contacts.');
    }
  }
}

///Note: Salesforce has built in validation rule to prevent deletion Accounts with related Contacts/ Opportunities.
///Another option is to use Record triggered flow

trigger preventAccountDeletion on Account (before delete) {
  // Check if the trigger is before delete
  if (Trigger.isBefore && Trigger.isDelete) {
    // Query for account IDs that have related contacts
    List<AggregateResult> accountWithContact = [
      SELECT AccountId
      FROM Contact
      WHERE AccountId IN :Trigger.oldMap.keySet()
      GROUP BY AccountId
      HAVING COUNT(Id) > 0
    ];
    // Throw an error if there are accounts with related contacts
    if (accountWithContact.size() > 0) {
      Trigger.old[0].addError(accountWithContact.size() + ' Accounts cannot be deleted because they have related contacts.');
    }
  }
}


        trigger preventAccountDeletion on Account (before delete) {
  // Check if the trigger is before delete
  if (Trigger.isBefore && Trigger.isDelete) {
    // Query for account IDs that have related contacts
    List<AggregateResult> accountWithContact = [
      SELECT AccountId
      FROM Contact
      WHERE AccountId IN :Trigger.oldMap.keySet()
      GROUP BY AccountId
      HAVING COUNT(Id) > 0
    ];
    // Throw an error if there are accounts with related contacts
    if (accountWithContact.size() > 0) {
      Trigger.old[0].addError(accountWithContact.size() + ' Accounts cannot be deleted because they have related contacts.');
    }
  }
}

///Note: Salesforce has built in validation rule to prevent deletion Accounts with related Contacts/ Opportunities.
///Another option is to use Record triggered flow

trigger preventAccountDeletion on Account (before delete) {
  // Check if the trigger is before delete
  if (Trigger.isBefore && Trigger.isDelete) {
    // Query for account IDs that have related contacts
    List<AggregateResult> accountWithContact = [
      SELECT AccountId
      FROM Contact
      WHERE AccountId IN :Trigger.oldMap.keySet()
      GROUP BY AccountId
      HAVING COUNT(Id) > 0
    ];
    // Throw an error if there are accounts with related contacts
    if (accountWithContact.size() > 0) {
      Trigger.old[0].addError(accountWithContact.size() + ' Accounts cannot be deleted because they have related contacts.');
    }
  }
}

deleted 42 characters in body
Source Link
Loading
added 420 characters in body
Source Link
Loading
added 2 characters in body
Source Link
Loading
Source Link
Loading