You are on page 1of 10

LinkedIn

Scenario: How would you prevent a user from deleting an


Account record if it has related Opportunity records?

Question: How can you implement this logic using triggers?

Answer: We can use a before delete trigger on the Account


object to check if there are any related Opportunity records. If
there are, we can prevent the deletion by throwing an
exception. Here's a sample trigger:

trigger PreventAccountDelete on Account (before delete)


{ Set<Id> accountIds = new Set<Id>();

for (Account acc : Trigger.old) { accountIds.add(acc.Id);


}

List<Opportunity> relatedOpps = [SELECT Id FROM


Opportunity WHERE AccountId IN :accountIds];

if (!relatedOpps.isEmpty()) {
throw new MyCustomException('Cannot delete Account
with related Opportunities.');
}
}
LinkedIn

Scenario: You need to enforce a validation rule


that prevents the creation of a Task record if its
Due Date is in the past.

Question: How can you enforce this validation using


triggers?

Answer: We can use a before insert trigger on the


Task object to check if the Due Date is in the past. If
it is, we can prevent the creation of the Task by
throwing an exception. Here's a sample trigger:

trigger PreventTaskCreation on Task (before insert)


{ for (Task tsk : Trigger.new) {
if (tsk.ActivityDate < Date.today()) {
throw new MyCustomException('Due Date
cannot be in the past.');
}
}
}
LinkedIn

Scenario: You need to automatically assign a


newly created Lead to a specific User based on
the Lead's Country field.

Question: How would you achieve this using


triggers?

Answer: We can use an after insert trigger on the


Lead object to assign the Lead to a specific User
based on the Lead's Country field. Here's an
example trigger:

trigger AssignLeadToUser on Lead (after insert)


{ Map<String, Id> countryToUserIdMap = new
Map<String, Id>{
'USA' => '005XXXXXXXXXXXX', // User Id for USA
'UK' => '005XXXXXXXXXXXX' // User Id for UK
// Add more mappings as needed
};
LinkedIn

List<Lead> leadsToUpdate = new List<Lead>(); for

(Lead ld : Trigger.new) {
if (countryToUserIdMap.containsKey(ld.Country))
{
ld.OwnerId =
countryToUserIdMap.get(ld.Country);
leadsToUpdate.add(ld);
}
}

update leadsToUpdate;
}
LinkedIn

Can you provide an example of how to prevent


record deletion using a trigger?

trigger PreventAccountDeletion on Account (before


delete) {
for (Account acc : Trigger.old)
{ if (acc.Type == 'Important') {
acc.addError('You cannot delete an important
account.');
}
}
}
LinkedIn

How do you write a trigger to update related


records?

You can use triggers along with SOQL queries to


update related records. Here's an example:

trigger UpdateRelatedContacts on Account (after


insert, after update) {
List<Contact> contactsToUpdate = new
List<Contact>();
for (Account acc : Trigger.new)
{ contactsToUpdate.addAll([SELECT Id,
AccountId FROM Contact WHERE AccountId =
:acc.Id]);
}
// Perform your logic to update
contactsToUpdate
update contactsToUpdate;
}
How do you handle trigger operations on different
objects within the same trigger? Provide an
example.

trigger CrossObjectTrigger on Contact (after insert,


after update) {
if (Trigger.isAfter) {
if (Trigger.isInsert || Trigger.isUpdate)
{ List<Account> relatedAccounts = [SELECT Id,
Name FROM Account WHERE Id IN (SELECT
AccountId FROM Contact WHERE Id IN
:Trigger.newMap.keySet())];

// Perform operations on related Account


records
for (Account acc : relatedAccounts)
{ acc.Description = 'Updated from Contact
Trigger';
}

update relatedAccounts;
}
}
}
LinkedIn

How do you handle trigger operations on child records


when a parent record is updated? Provide an example.

trigger ChildRecordTrigger on Account (after update)


{ if (Trigger.isAfter && Trigger.isUpdate) {
Set<Id> accountIds = new Set<Id>();

for (Account acc : Trigger.new)


{ accountIds.add(acc.Id);
}

List<Contact> relatedContacts = [SELECT Id, Name


FROM Contact WHERE AccountId IN :accountIds];

// Perform operations on related Contact records


for (Contact con : relatedContacts) {
con.Description = 'Chandan is Updated from Account
Trigger';
}

update relatedContacts;
}
}
LinkedIn

How do you write a trigger to enforce


validation rules before record insertion?
Provide an example.

Answer:

trigger ValidateRecordTrigger on
Custom_Object c (before insert) {
for (Custom_Object c obj : Trigger.new)
{ if (obj.Field c == null) {
obj.addError('Field c is required.');
}
// Add more validation rules as needed
}
}
LinkedIn

How do you handle trigger operations


asynchronously? Provide an example.

@future
public static void
handleTriggerAsync(List<Account>
newAccounts, Map<Id, Account> oldMap) {
// Perform asynchronous logic here
// Example: Callouts, heavy computations,
etc.
}

trigger AsyncTrigger on Account (after insert)


{

TriggerHandler.handleTriggerAsync(Trigger.n
ew, Trigger.oldMap);
}

You might also like