Important trigger scenario for practice and interview point.
- Abhilash Banpurkar
- Jan 27, 2023
- 3 min read
Updated: Feb 5, 2023
Prevent deletion of active account records
trigger PreveventDeletion on Account (before delete) {
for(Account acc:trigger.old) {
if(acc.Active__c=='yes')
//check if account active or not.
{ acc.addError('You can not delete active account records');
}
}
}
Create a trigger on lead object to make sure the lead address should be mandatory.
trigger LeadAddressMandatory on Lead (before insert) {
for(Lead ld:trigger.new) {
if((ld.City==null)||(ld.Street == null)||(ld.State==null)) {
ld.addError('Complete address is mandatory');
}
}
}
Prevent deletion of contact when that contact is realted to account.
trigger preventCOntactDeletion on Contact (before delete) {
//Step 1: create a set to store account ids set accIds=new set();
//Step 2: collect all the accountIds in the set created in step 1
for(Contact acc:trigger.old){
accIds.add(acc.accountId);
}
//Step 3: create a map to store account id as key and contact records as value
Map accMap=new Map();
//Step 4: Collect relative data in the map created in step 3
for(Account acc:[select id,name from account where id in:accIds]){
accMap.put(acc.id, acc);
}
//Step 5:Iterate main logic in the for loop
for(Contact con:trigger.old){
if(accMap.containsKey(con.AccountId)){
if(accMap.get(con.AccountId)!=null){
con.addError('Contact cannot deleted');
}
}
}
}
Create a trigger on lead object to auto populate the annual revenue based on the industry name as below
Banking-909000,
finance-89000,
insurance-77000,
energy-90000
trigger leadAnnualRevenue on Lead (before insert){
for(Lead ld:trigger.new){
if(ld.Industry=='banking'){
ld.AnnualRevenue=909000;
}
else if(ld.Industry=='finance')
{
ld.AnnualRevenue=89000;
}
else if(ld.Industry=='insurance')
{
ld.AnnualRevenue=70000;
}
else if(ld.Industry=='energy')
{
ld.AnnualRevenue=90000;
}
}
}
Avoid creation of duplicate account name.
trigger AvoidDuplicateAccount on Account (before insert){
//Step1: Create a list to store accNames
List accNames=new List();
//Step 2: Collect all the account Name in the list created in step 1 for(Account acc:trigger.new){
accNames.add(acc.Name);
}
//step 3: Create a list to store duplicate account name
List dupName=new List();
//Step 4: collect all the existing account name in the list created in step3
for(Account ac:[select id,name from account where name in:accNames]){
dupName.add(ac.Name);
}
//Step 4: Iterate main logic in the for loop and use list created in step 3 for relative comparision
for(account acc:trigger.new){
if(dupName.contains(acc.name){
acc.addError('This account Name already exit in the account');
}
}
}
Created related contact with same name as account name and same phone as account phone.
trigger createRelatedContact on Account (After insert) {
// create a list to store related
contact List conList=new List();
//Iterate through trigger.new in the for loop
for(Account acc:trigger.new){
Contact cont=new Contact();
//Create the instance of contact cont.LastName=acc.name;
cont.Phone=acc.phone;
cont.AccountId=acc.Id;
conList.add(cont);
}
insert conList;
}
Write a trigger on account when an account billingCity is updated, update all its contacts MailingCity with account billingCity.
trigger accUpdatedBillingCity on Account (After update){
set accIds=new set();
for(Account acc:trigger.new){
accIds.add(acc.id);
}
Map accMap=new Map();
for(Account acc:[select id,name,billingCity from account where id in:accIds]){
accMap.put(acc.id, acc);
}
List conList=new List();
for(Contact con:[select id,accountId,mailingCity from contact where accountId in:accMap.keySet()]){
if(accMap.containsKey(con.AccountId)){
if(accMap.get(con.AccountId).billingCity!=trigger.oldMap.get(con.AccountId).billingCity){
con.MailingCity=accMap.get(con.AccountId).billingCity;
conList.add(con);
}
}
}
if(!conList.isEmpty()){
update conList;
}
}
Write A Trigger on account to make sure that account industry and account website are required.
trigger accIndWeb on Account (before insert){
for(account acc:trigger.new){
if((acc.Industry==null)||(acc.Website==null)){
acc.addError('Account website and industry is mandatory');
}
}
}
Write A Trigger Whenever opportunity stage is 'Closed Won' then set 'CloseDate' as 'TodayDate' and Type='New Customer'.
Trigger OpportunityStage on Opportunity (before insert,before update) {
for(Opportunity op:trigger.new){
if(op.stageName=='Closed won'){
op.closeDate =System.today(); op.Type='New Customer';
}
}
}
Prevent contact from deleting if contact is active.
trigger PreventContactNotDeleted on Contact (before delete){
for(Contact con:trigger.old){
if(con.Active__c=='Yes'){
con.addError('You can not delete active contact record');
}
}
}
Write A Trigger on Lead if rating is 'Hot' update lead status as 'Closed -converted'.
trigger LeadStatusUpdated on Lead(before insert,before update){
for(Lead ld:trigger.new){
if(ld.Rating=='Hot'){
ld.status='Closed-Converted';
}
}
}
When Contact type is platinum on Contact object then modified the Platinum contact status as 'Platinum Contact is modified !' on Account.
Trigger ContactStatusUpdated on Contact ( After Update) {
Set accIds=new Set();
for(Contact con: trigger.new){
if(con.AccountId !=null && con.type__c == 'Platinum'){
accIds.add(con.AccountId);
}
}
if(accIds.size() > 0){
List accList=[select id,name,platinum_contact_status__c from Account where id in :accIds];
for(Account acc:accList){
acc.Platinum_Contact_Status__c= ' Platinum Contact modified ! ';
}
if(!accList.isEmpty()){
update accList;
}
}
}
When lead is created or updated then check if the email of lead is already there in existing on contacts.If email already exist then throw error.
Trigger LeadEmailTrigger on Lead(before insert,before update){
// create a list to store contact email List emailList=new List();
//fetch the existing contact email from the database
List contList=[select id,email from contact];
for(Contact con : contList){
emailList.add(con.email);
}
for(Lead ld : trigger.new) {
if((ld.email != null) && (trigger.isInsert || (ld.email!=trigger.oldmap.get(ld.id).email)){
If(emailList.contains(ld.email)){
Ld.addError(‘This email already exist on contact’);
}
}
}
}
Commentaires