Salesforce Apex Triggers Interview Questions/Scenarios
Hello,
This post is regarding some apex trigger interview questions. These questions are for Novice to Intermediate level developers. I feel these questions/scenarios would be helpful for practice.
PS: The questions below are collected from the internet and some of them are which I was asked in the interviews I appeared for.
1. Create "Sales Rep" field with the datatype (Text) on the Account object. When we create an Account record, the Account's owner name will be automatically added to the sales rep field. When we update the Account owner of the record, then also the Sales Rep will be automatically updated.
2. Create a field called "Contact Relationship"(checkbox) on the contact object and create the object called "Contact Relationship" which is related to Contacts (Lookup Relationship). Now build a logic when we create contact by checking the Contact Relationship checkbox, a Contact Relationship record will be created automatically for that contact. When we change the owner of the Contact Relationship, then the owner name will be automatically populated in the Contact Relationship Name Field.
3. Whenever a Case is created with 'Origin' as email then set 'Status' as 'New' and 'Priority' as 'Medium'.
4. Whenever a Lead is created with 'LeadSource' as 'Web' then give 'Rating' as 'Cold' otherwise 'Hot'.
5. Create a field 'Batch' and 'Sequence' on Contact. Whenever you create an account, 5 child contact records should be created and 'Batch' field should have 6 characters long value which should be similar in all 5 records and unique to these records, and 'Sequence' field should have a sequence in which these records were created.
Example: Account Record created -> 5 contact records created with Sequence field being 0 in the first contact record, 1 in the second contact record, and so on and the Batch field should have 'ABC456' in all the five records which is unique to these 5 records only.
6. Whenever the Account is created with 'Industry' as 'Banking' then create a contact for the account, Contact 'Lastname' as Account name, and contact 'Phone' as account phone.
7. Create the number of contacts that are equal to the number which we will enter in the 'Number of Locations' field on the Account Object
8. Whenever Opportunity 'Stage' is modified to 'Closed Won' then set 'Close Date' as 'Today Date' and 'Type' as 'New Customer'.
9. Whenever a new contact is created for an existing account then set contact 'Other Phone' as the account's phone.
10. Write a trigger to avoid the creation of duplicate accounts (based on AccountName).
11. Whenever Account's 'Billing Address' is changed, the same address should be updated in all child contact records 'Mailing Address'.
12. Write a trigger that will count the number of contact records for an account. This count should be automatically be updated whenever a contact record is created or deleted.
While practicing please take care of the context variables and trigger best practices.
If you have any more interesting trigger scenarios, Please do post them in the comments so that the community can benefit from them.
Thank you. π
Amazing set of questions!
ReplyDeleteThanks for sharing these questions. Looking forward to see similar posts. πππ