Triggers in Salesforce Interview Questions and Answer
Triggers in Salesforce Interview Questions and Answer
Example:
trigger prefixName on Account (before Insert, before Update){
}
NOTE : In before insert context your Trigger.NewMap will always be null because in before context records are
not submitted to the database, so the Id is not generated yet. That's why in before insert we don't
use Trigger.NewMap But in After insert, Id is generated so we can use Trigger.NewMap
In case of before and after update, the Id has already been generated in the insert event. So we can
use Trigger.NewMap in before and after update.
Trigger.oldMap: A map of IDs to the old versions of the sObject records.Note that this map is only
available in the update and delete triggers.
Trigger.OldMap: Trigger.oldMap returns map of old records which are updated with new values.
These List of records already there in Database. Trigger.oldmap available in Before update, after
update, Before Delete and After Delete triggers.
2) Logic-less Triggers
If you write methods in your Triggers, those can’t be exposed for test purposes. You also can’t expose logic to
be re-used anywhere else in your org.
Example :
public class RecursiveTriggerHandler{
public static Boolean isFirstTime = true;
}
if(RecursiveTriggerHandler.isFirstTime){
RecursiveTriggerHandler.isFirstTime = false;
Scenario 1 : When we are trying to insert new record into object. If there is any record existing with same
account name it should prevent duplicate record.
Scenario 2: Write a trigger to prefix Account Name with ‘Mr’ when new record is inserted.
Scenario 3: Whenever a new record is created into account object . Before this new record is inserted into
Account, delete all the contacts records with this account name.
Scenario 4: Whenever a new transaction is performed successfully then update the customer object balance
field based on
If Transaction Type=Deposit, Balance= balance +amount ; withdraw balance= balance-amount;
Note: Customers and Transaction has lookup Detail Relation.
Scenario 5: Whenever a new contact is created for account update the corresponding account phone with
the new contact phone field.
Scenario 6: Whenever customer record is updated, before updating the record create new record in test
object with old values of customer record.
Scenario 7: To update the owner of a case based on the values selected within a picklist and populate the
owner field with the created by field data. When we have selected any Field name=Status
Picklist Values=
priced-(Initial)
Priced-(Re-priced)
Price(File Loaded)
Scenario 8: Write a trigger that will prevent a user from creating a lead that already exists as a contact. We
will use the lead /contact email address to detect duplicates.
Lead is created or updated.
1. Lead has an email address.
2. 2. Try to find a matching contact based on email address.(Using SOQL)
3. If a match is found give an error
4. If a match is not found do nothing.
Scenario 9: When we are trying to delete customer record delete all the corresponding child records from test
object, where customer is lookup field in the object.
Scenario 10: When we create the opportunity with probability =50% then the opportunity owner will be
automatically added to Account Team of the associated account for the opportunity.
Scenario 11: Invoking the apex class from the trigger. Whenevr new customers record is created /updated
then income tax should be calculated based on salary and should be updated to field income tax (Currency
field).
Scenario 12: If we delete any of the existing customer record then first create new test object record with
customer record values then delete customer record.
Scenario 13: Whenever we try to update the phone of account record then update the related contact phone
number with the new Account phone number before account record is updated.
When we delete the account record then delete the corresponding contact records.
Scenario 16: Create ‘Sales Rep’ field with datatype (Text) on the account object. When we create account
record, the account owner will be automatically added to Sales Rep field. When we update the Account owner
of the record, then also the Sales Rep Will be automatically updated.
Scenario 17: Create the field Called ‘Contact Relationship’ checkbox on the contact object and create the
object called “Contact Relationship” which is related list to the contact.(Lookup Relationship).
Now logic is when we create Contact by checking Contact Relationship checkbox then contact relationship will
be created automatically for that contact.
Scenario 18: When we change the owner of the Contact Relationship, then the owner name will be
automatically populated in the Contact Relationship name field.
Scenario 19: Create the field Called ‘Contact Relationship’ checkbox on the contact object and create the
object called “Contact Relationship” which is related list to the contact.(Lookup Relationship).
When we delete the contact then contact Relationship will be deleted automatically.
Scenario 20: Create the field Called ‘Contact Relationship’ checkbox on the contact object and create the
object called “Contact Relationship” which is related list to the contact.(Lookup Relationship).
When we undelete the contact then contact Relationship will be Undeleted automatically.
Scenario 21: Create field Called ‘Count of Contacts’ on Account object. When we add the contacts for that
Account then count will populate in the field on Account details page. When we delete the contacts for that
account then count will update automatically.
Note: The above logic will be applicable when we have lookup relationship. But when we have the master
detail relationship, then we can create Rollup Summary field to get the count of child records using “Count”
function.
[14] Can you edit an apex trigger/ apex class in production environment? Can you edit a Visualforce page in
production environment?
Ans: No, it is not possible to edit apex classes and triggers directly in production environment. It needs to be
done first in Developer edition or testing org or in Sandbox org. Then, to deploy it in production, a user with
Author Apex permission must deploy the triggers and classes using deployment tools. However, Visualforce
pages can be created and edited in both sandbox and in production. Only if the page has to do something unique
(different values), it would have to be developed via Sandbox.