Here are the triggers for beginners to practice : ( In-Progress ..)
01/20/24
Scenario 1: Trigger on Account to show an error message when Phone is empty during record creation
trigger AccPhoneFieldEmpty on Account (before insert)
{
if(trigger.isBefore && trigger.isInsert)
{
if(! trigger.new.isEmpty()) //list of Account SObject
{
for(Account acc : trigger.new)
{
if(acc.Phone == null)
{
acc.Phone.addError ('Phone field can't be empty');
}
}
}
}
}
01/21/24
Scenario 2: : Copy the Billing Address to the Shipping Address during Insert/Update on Account
trigger accBillingAddress on Account (before insert, before update)
{
if(trigger.isBefore && (trigger.isInsert || trigger.isUpdate) )
{
if(! trigger.new.isempty())
{
for(Account acc : trigger.new)
{
if(acc.BillingStreet != null || acc.BillingCity != null || acc.BillingState != null || acc.BillingCountry != null)
{
acc.ShippingStreet = acc.BillingStreet;
acc.ShippingCity = acc.BillingCity;
acc.ShippingState = acc.BillingState;
acc.ShippingCountry = acc.BillingCountry;
}
}
}
}
}
01/22/24
Scenario 3 : All Contacts associated with an Account has same Phone number as Account
trigger updateChildPhone on Account (after Update)
{
Map<Id,Account> accMap = new Map<Id,Account>();
if(trigger.isAfter && trigger.isUpdate)
{
if(!trigger.new.isEmpty())
{
for(Account accObj : trigger.new)
{
if(accObj.phone != trigger.oldMap.get(accObj.Id).Phone)
{
accMap.put(accObj.id,accObj);
}
}
}
}
if(!accMap.isEmpty())
{
List<Contact> conList = [Select id,Phone,AccountId from Contact where AccountId IN : accMap.keySet()];
List<Contact> listToUpdateContacts = new List<Contact>();
if(!conList.isEmpty())
{
for(Contact conObj : conList)
{
conObj.phone = accMap.get(conObj.AccountId).phone;
listToUpdateContacts.add(conObj);
}
}
if(!listToUpdateContacts.isEmpty())
{
try
{
update listToUpdateContacts;
}
catch (Exception ex)
{
System.debug('Error while updating records' +ex.getMessage());
}
}
}
}
01/23/24
Scenario 4 : When Contact's Description is updated, update the description on the Account
trigger updateParentRecord on Contact (after update)
{
Set<Id> parentAccIds = new Set<Id>();
if(trigger.isAfter && trigger.isUpdate)
{
if(!trigger.new.isEmpty()) {
for(Contact conObj : trigger.new)
{
if(conObj.AccountId != null & conObj.Description != trigger.oldMap.get(conObj.Id).Description)
{
parentAccIds.add(conObj.AccountId);
}
}
}
}
if(!parentAccIds.isEmpty())
{
Map<Id,Account> mapToUpdate = new Map<Id,Account>();
Map<Id,Account> parentAccMap = new Map<Id,Account>([Select Id,Description from Account WHERE Id IN : parentAccIds]);
if(!trigger.new.isEmpty())
{
for(Contact con : trigger.new)
{
if(parentAccMap.containsKey(con.AccountId))
{
Account acc = parentAccMap.get(con.AccountId);
acc.Description = con.Description;
mapToUpdate.put(acc.id,acc);
}
}
}
if(!mapToUpdate.isEmpty())
{
try
{
update mapToUpdate.values();
}
catch (Exception ex)
{
System.debug('Error while updating records' +ex.getMessage());
}
}
}
}
01/24/24
Scenario 5 : Number of Contacts on Account
trigger countContacts on Contact (after insert, after update, after Delete, after undelete)
{
Set<Id> parentAccIds = new Set<Id>();
if( trigger.isAfter && (trigger.isInsert || trigger.isUndelete) )
{
if(!trigger.new.isEmpty())
{
for(Contact conObj : trigger.new)
{
if(conObj.AccountId != null)
{
parentAccIds.add(conObj.AccountId);
}
}
}
}
if(trigger.isAfter && trigger.isUpdate)
{
if(!trigger.new.isEmpty())
{
for(Contact conObj : trigger.new)
{
if(conObj.AccountId != trigger.oldMap.get(conObj.Id).AccountId)
{
if(trigger.oldMap.get(conObj.Id).AccountId != null)
{
parentAccIds.add(trigger.oldMap.get(conObj.Id).AccountId);
}
if(conObj.AccountId != null)
{
parentAccIds.add(conObj.AccountId);
}
}
}
}
}
if(trigger.isAfter && trigger.isDelete)
{
if(!trigger.old.isEmpty())
{
for(Contact conObj :trigger.old)
{
if(conObj.AccountId != null)
{
parentAccIds.add(conObj.AccountId);
}
}
}
}
if(parentAccIds.isEmpty())
{
List<Account> accList = [Select id,Number_Of_Contacts__c,(Select id from Contacts) from Account where id IN : parentAccIds];
List<Account> accountsToUpdate = new List<Account>();
if(!accList.isEmpty())
{
for(Account acc : accList)
{
acc.Number_Of_Contacts__c = acc.Contacts.size();
accountsToUpdate.add(acc);
}
}
if(!accountsToUpdate.isEmpty())
{
try
{
update accountsToUpdate;
}
catch (DmlException e)
{
System.debug('An error has occured : ' +e.getMessage());
}
}
}
}
01/25/24
Scenario 6 : Avoid duplicate names on Account
trigger avoidDuplicateNamesOnAcc on Account (before insert,before update)
{
Set<String> accNames = new Set<String>();
if(trigger.isBefore && (trigger.isInsert || trigger.isUpdate))
{
if(!trigger.new.isEmpty())
{
for(Account acc : trigger.new)
{
accNames.add(acc.Name);
}
}
}
List<Account> accList = [Select id,Name FROM Account WHERE Name IN : accNames];
Map<String,Account> existingAccMap = new Map<String,Account>();
if(!accList.isEmpty())
{
for(Account accObj : accList)
{
existingAccMap.put(accObj.Name,accObj);
}
if(!trigger.new.isEmpty())
{
for(Account existingAcc : trigger.new)
{
if(existingAccMap.containsKey(existingAcc.Name))
{
existingAcc.addError('Account name already exists');
}
}
}
}
}
Scenario 7 : Create a Contact when the create contact checkbox is checked on the Account with First Name is same as Accounts first name and Last name is 'Contact'
trigger createRelatedContacts on Account (after insert, after update) {
list<Contact> listToInsert = new List<Contact>();
if(trigger.isAfter && (trigger.isInsert || trigger.isUpdate))
{
if(trigger.new.isEmpty())
{
for(Account acc :trigger.new)
{
if(acc.Create_Contact__c && (trigger.isInsert || acc.Create_Contact__c != Trigger.oldMap.get(acc.id).Create_Contact__c))
{
Contact con = new Contact();
con.firstName = acc.Name;
con.LastName = 'Contact';
con.AccountId = acc.Id;
con.Phone = acc.Phone;
listToInsert.add(con);
}
}
}
if(!listToInsert.isEmpty())
{
insert listToInsert;
}
}
}
No comments:
Post a Comment