0% found this document useful (0 votes)
0 views

Trigger Beginner Level Scenario

The document outlines several triggers in Salesforce that automate various tasks related to Contacts, Opportunities, Accounts, and Leads. Key functionalities include auto-populating fields, creating follow-up tasks, preventing changes to account names, logging deleted records, assigning leads based on source, blocking deletions of closed opportunities, counting contacts under accounts, and copying phone numbers from accounts to related contacts. Each trigger is defined with specific criteria and actions to enhance CRM efficiency and data integrity.

Uploaded by

Sushant
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

Trigger Beginner Level Scenario

The document outlines several triggers in Salesforce that automate various tasks related to Contacts, Opportunities, Accounts, and Leads. Key functionalities include auto-populating fields, creating follow-up tasks, preventing changes to account names, logging deleted records, assigning leads based on source, blocking deletions of closed opportunities, counting contacts under accounts, and copying phone numbers from accounts to related contacts. Each trigger is defined with specific criteria and actions to enhance CRM efficiency and data integrity.

Uploaded by

Sushant
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Trigger Simple Scenarios

Part 1
Trigger 1:
Auto-populate the Description field on the Contact record with a
welcome message when a new contact is created.
trigger contactTrigger on Contact (before insert) {
for(Contact con : Trigger.New)
{
con.Description='Welcome '+con.FirstName+' '+con.LastName +' To our CRM';
}
}

OutPut:

______________________________________________________________________________
Trigger 2:
After creating an Opportunity, create a related Task to follow up in 3 days.
trigger opportunityTrigger on Opportunity (after insert) {
List<Task> TaskList=new List<Task>();
for(opportunity opp:Trigger.New)
{
task Rec=new task();
Rec.WhatId=opp.Id;
Rec.OwnerId=opp.OwnerId;
Rec.ActivityDate=Date.Today().adddays(3);
Rec.Status='New';
Rec.Subject='Follow up the Task';
TaskList.add(Rec);
}
if(!TaskList.isEmpty())
{
insert TaskList;
}}

OutPut:
Trigger 3:
Prevent changing the Account Name if the Account Type is "Customer -
Direct".
trigger AccountTrigger on Account (after update) {
for(Account acc:Trigger.New)
{
Account OldAcc=Trigger.OldMap.get(acc.Id);
if(OldAcc.Name!=acc.Name && acc.Type=='Customer - Direct')
{
acc.Name.addError('Account Name can not be changed for Direct Customers');
}
}
}

OutPut:
Trigger 4:
When a Case is deleted, log the deleted Case ID into a custom object called
"Deleted_Record_Log__c".
trigger CaseTrigger on Case (after delete) {
List<Deleted_Record_Log__c> DRLList=new List<Deleted_Record_Log__c>();
for(case caseRec:Trigger.old)
{
Deleted_Record_Log__c DRL=new Deleted_Record_Log__c();
DRL.Record_Id__c=caseRec.Id;
DRL.Object_Name__c='Case';
DRL.Deleted_On__c=Date.Today();
DRLList.add(DRL);
}
if(!DRLList.isEmpty())
{
insert DRLList;
}}

OutPut:
Trigger 5:
If a new Lead has LeadSource = 'Web', assign it to a specific user (e.g., Web Lead
Manager).
trigger LeadTrigger on Lead (before insert) {
User WebLeadManager=[select id from User where Profile.Name='WebLead' Limit 1];
for(Lead Rec:Trigger.New)
{
if(Rec.LeadSource=='Web')
{
Rec.OwnerId=WebLeadManager.id;
}
}
}

OutPut:

WebLeadManager
Trigger 7:
Block users from deleting Opportunities if StageName = 'Closed Won' or Closed
Lost.
trigger opportunityTrigger on Opportunity (before delete) {
for(opportunity opp:Trigger.Old)
{
if(opp.StageName=='Closed Won'||opp.StageName=='Closed Lost')
{
opp.addError('You cant delete closed opportunities');
}
}
}

OutPut:
Trigger 8:
On inserting a new Contact,deleting a contact and undelete a contact, count
how many contacts exist under the related Account and update a custom field
on Account: Total_Contacts__c.
trigger contactTrigger on Contact (after insert,after Delete,after undelete) {
if(Trigger.Isinsert || Trigger.IsUndelete || Trigger.isdelete)
{
contactCount();
}
public void contactCount()
{
set<Id> accountIds=new set<Id>();
for(contact con:Trigger.New)
{
accountIds.add(con.AccountId);
}
List<Account> AccList=[select id,(select id from contacts) from Account where
Id=:accountIds];
for(Account acc:AccList)
{
acc. Total_Contacts__c =acc.contacts.size();
}
update AccList;
}
}
OutPut:
Trigger 9:
When a new Account is created, copy Phone to the related contact.

trigger AccountTrigger on Account (after insert,after update) {


List<contact> conList=[select phone from contact where AccountId=:Trigger.New];
for(contact con:conList)
{
con.Phone=Trigger.New[0].phone;
}
update conList;
}
OutPut:

You might also like