Trigger Scenarios

Write a trigger that updates the Account ‘Number of Opportunities’ field when an Opportunity is inserted, updated, or deleted.

This Apex trigger ensures that the custom field Number_of_Opportunities__c on the Account object always reflects the current number of related Opportunity records. It runs after insert, update, and delete operations on Opportunity. Trigger- Apex class-

Write a trigger that updates the Account ‘Number of Opportunities’ field when an Opportunity is inserted, updated, or deleted. Read More »

Write a trigger to update a field on a child record to a default value if the child record is the only child of the parent.

Let’s say – The parent object is Account, The child object is Contact, We want to update the child field Status__c to ‘Primary’ only if that contact is the only contact related to the parent Account. Trigger: Key Concepts:

Write a trigger to update a field on a child record to a default value if the child record is the only child of the parent. Read More »

How to Count Contacts on Each Account Using Trigger

Count related contacts on each Account in Salesforce using a custom Apex Trigger. A simple solution for real-time rollups. This trigger runs after insert, update, or delete operations on the Contact object. It collects all relevant Account IDs and queries the number of Contacts for each, updating a custom field called Number_of_Contacts__c on the Account. This approach

How to Count Contacts on Each Account Using Trigger Read More »

Implement a trigger to ensure that no more than one Opportunity can be created per Account within a single day.

Implement a trigger to ensure that no more than one Opportunity can be created per Account within a single day. This scenario was asked in Deloitte interview -If a user attempts to create a second Opportunity for the same Account on the same day, display an appropriate error message. Trigger Apex Class Alternative Approach –

Implement a trigger to ensure that no more than one Opportunity can be created per Account within a single day. Read More »