Posts

Apex Triggers - 89 (Persistent Interview Scenario)

 When an Account already has 2 existing cases , and a 3rd new case is created under that same Account, the system must automatically create a High Priority Task assigned to the parent Account. ============================================================ trigger CaseCountTrigger on Case (after insert ) { // Ensure the execution only runs during the after insert context if (Trigger.isAfter && Trigger.isInsert) { CaseCountTriggerHandler.handleAfterInsert(Trigger. new ); } } ======================================================== public class CaseCountTriggerHandler {          public static void handleAfterInsert(List<Case> newCases) {         Set<Id> accountIds = new Set<Id>();                  // 1. Collect Account IDs from the incoming cases         for (Case c : newCases) {             if (c....

Apex Triggers - 90 (Accenture Interview Scenario)

  Business Requirement The business wants to protect important customer data by preventing users from deleting an Active Account if it has one or more related Contact records. Only users with the System Administrator profile are allowed to delete such accounts. All other users should receive an error message, and the deletion should be blocked. Scenario Details ABC Corporation is an active customer in Salesforce. The account contains multiple related contacts, such as John Doe and Jane Smith . A user attempts to delete the account. Before Salesforce deletes the record, a Before Delete Apex Trigger is executed. The trigger performs the following validations: Checks whether the Account Status = Active . Checks whether the Account has at least one related Contact . Checks whether the logged-in user's Profile is System Administrator . ================================================================ trigger AccountDeletionTrigger on Account (before delete) {   ...

Sales Cloud - Salesfroce

  Create an Account Team | Salesforce Fundamentals

Trigger 4 - Trigger to prevent duplication of an Account record based on Name whenever a record is inserted or updated.

 Trigger 4 scenario - Trigger to prevent duplication of an Account record based on Name whenever a record is inserted or updated. trigger AccountTrigger on Account (before insert , before update ) { if (Trigger.isBefore) { if (Trigger.isInsert) { AccountTriggerHandler.preventDuplicateNames(Trigger. new , null ); } else if (Trigger.isUpdate) { AccountTriggerHandler.preventDuplicateNames(Trigger. new , Trigger.oldMap); } } } public class AccountTriggerHandler {     public static void preventDuplicateNames(List<Account> newAccounts, Map<Id, Account> oldAccountMap) {         Set<String> newAccountNames = new Set<String>();         List<Account> recordsToValidate = new List<Account>();         // 1. Isolate the names being added or changed         for (Account acc : newAccounts) {   ...

Trigger 3 - Roll up Summary Trigger - Trigger to count the number of Contacts associated with an Account and display the Contacts count on the Account's custom field.

 Trigger to count the number of Contacts associated with an Account and display the Contacts count on the Account's custom field. trigger ContactTrigger on Contact (after insert, after update, after delete, after undelete) {     if (Trigger.isAfter) {         if (Trigger.isInsert || Trigger.isUndelete) {             ContactTriggerHandler.countContacts(Trigger.new, null);         }          else if (Trigger.isUpdate) {             ContactTriggerHandler.countContacts(Trigger.new, Trigger.oldMap);         }          else if (Trigger.isDelete) {             ContactTriggerHandler.countContacts(null, Trigger.oldMap);         }     } } public class ContactTriggerHandler {          public static void countContacts(List<Contact...

Trigger 2 - Whenever a Contact's description is updated, then its Parent Account's description should also get updated by it.

 Whenever a Contact's description is updated, then its Parent Account's description should also get updated by it. trigger AccountTrigger on Account (after update) {     if (Trigger.isAfter && Trigger.isUpdate) {         AccountTriggerHandler.handleAfterUpdate(Trigger.new, Trigger.oldMap);     } } public with sharing class AccountTriggerHandler {          public static void handleAfterUpdate(List<Account> newAccounts, Map<Id, Account> oldAccountMap) {         Set<Id> accountIdsWithUpdatedPhone = new Set<Id>();                  // Loop 1: Find parent accounts where the target field actually changed         for (Account newAcc : newAccounts) {             Account oldAcc = oldAccountMap.get(newAcc.Id);                     ...

Trigger 1 - Whenever Account's Phone field is updated, all related Contacts' Phone field should also be updated with the parent Account's Phone.

 Whenever Account's Phone field is updated, all related Contacts' Phone field should also be updated with the parent Account's Phone. trigger AccountTrigger on Account (after update) {     if (Trigger.isAfter && Trigger.isUpdate) {         AccountTriggerHandler.handleAfterUpdate(Trigger.new, Trigger.oldMap);     } } public with sharing class AccountTriggerHandler {          public static void handleAfterUpdate(List<Account> newAccounts, Map<Id, Account> oldAccountMap) {         Set<Id> accountIdsWithUpdatedPhone = new Set<Id>();                  // Loop 1: Identify accounts where the Phone field actually changed         for (Account newAcc : newAccounts) {             Account oldAcc = oldAccountMap.get(newAcc.Id);                ...