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

Trigger Practice

The document outlines various trigger levels for Salesforce account and opportunity records, detailing specific actions to be taken upon creation or update of these records. Key functionalities include updating ratings based on industry, copying billing addresses, creating related contacts and opportunities, and updating fields based on specific conditions. The triggers are implemented in Apex code, ensuring automated processes for account management and related entities.

Uploaded by

Tejas Borkar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Trigger Practice

The document outlines various trigger levels for Salesforce account and opportunity records, detailing specific actions to be taken upon creation or update of these records. Key functionalities include updating ratings based on industry, copying billing addresses, creating related contacts and opportunities, and updating fields based on specific conditions. The triggers are implemented in Apex code, ensuring automated processes for account management and related entities.

Uploaded by

Tejas Borkar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Trigger Level 01 :

1.Account Record Creation if Industry field is having value as "


then populate Rating as 'HOT'

public class AccountTriggerHandler


{
Public Static Void UpdateRating(List<Account> newrecord)
{
for(Account accRecord : newrecord)
{
if(accRecord.Industry == 'Energy' || accRecord.Industry
{
accRecord.Rating = 'Hot';
}
}
}
}

================================================================
trigger AccountHandler on Account (before insert)
{
if(Trigger.isInsert && Trigger.IsBefore)
{
AccountTriggerHandler.UpdateRating(Trigger.New);
}
}

2.Opportunity Record Creation if Amount is bewteen 100000 to 500


"Hot Opportunity" in Description fields

Public Static Void UpdateDescription(List<Opportunity> newrecor

Trigger Level 01 : 1
{
for(Opportunity oppRecord : newrecord)
{
if(oppRecord.Amount >= 100000 && OppRecord.Amount <= 5
{
oppRecord.Description = 'Hot Opportunity';
}
}
}
==============================================================

trigger OpportunityTrigger on Opportunity (before insert)


{
If(Trigger.IsBefore && Trigger.IsInsert)
{
OpportunityTriggerHandler.UpdateDescription(Trigger.New);
}
}

3. When Account Record is created and CopyBillingToShipping CHe


then copy Account Billing Address to Shipping

Public Static Void CopyAddress(List<Account> newrecord)


{
for(Account accRecord : newrecord)
{
if(accRecord.CopyBillingToShipping__c == True)
{
accRecord.ShippingCity = accRecord.BillingCity;
accRecord.ShippingCountry = accRecord.BillingCountry;
accRecord.ShippingState = accRecord.BillingState;
accRecord.ShippingStreet = accRecord.BillingStreet;
}
}

Trigger Level 01 : 2
}

===============================================================
AccountTriggerHandler.CopyAddress(Trigger.New);

4. When An Account Record is Created then create related contact

Public Static Void CreateRealatedContact(List<Account> newreco


{
List<Contact> conRecord = New List<Contact>();
for(Account acc : newrecord)
{
Contact con = New Contact();
con.FirstName = 'Related Contact';
con.LastName = 'Checking Related Contact Creation'+ acc.Na
con.AccountId = acc.Id;
conRecord.add(con);
}
if(!conRecord.isEmpty())
{
insert conRecord;
}
}
================================================================
AccountTriggerHandler.CreateRealatedContact(Trigger.New)

5.When Account Record is Created then created a related Opportun

Public Static Void CreatedRelatedOpportunity(List<Account> n


{
List<Opportunity> OppRecord = New List<Opportunity>();
for(Account acc : newrecord)
{

Trigger Level 01 : 3
Opportunity Opp = New Opportunity();

Opp.Name = 'Related Opportunity';


Opp.StageName = 'Need Analysis' + acc.Name;
Opp.CloseDate = System.today();
Opp.AccountId = acc.Id;

OppRecord.add(Opp);
}
if(! OppRecord.isEmpty())
{
Insert OppRecord;
}
}

----------------------------------------------------------------

AccountTriggerHandler.CreatedRelatedOpportunity(Trigger.New)

6.When Case is Created on any Account.put the latest Case Number


No Of Contact field

Public Static Void CaseNumberUpdate(List<Case> newrecord)


{
List<Account> accRecord = New List<Account>();
if(! newrecord.isEmpty())
{
for(Case cse : newrecord)
{
if(cse.AccountId != Null)
{
Account acc = New Account();

acc.Id = cse.AccountId;
acc.Latest_Case_Number__c = cse.CaseNumber;

Trigger Level 01 : 4
accRecord.add(acc);
}
}
}
Update accRecord;
}
================================================================

trigger CaseTrigger on Case (After Insert)


{
if(Trigger.isAfter && Trigger.IsInsert)
{
CaseTriggerHandler.CaseNumberUpdate(Trigger.New);
}
}

7.When an Opportunity is created on any account.put the latest


No_of_Contact fields

Public Static Void UpdateLatestAmount(List<Opportunity> newrec


{
List<Account> accRecord = New List<Account>();
if(! newrecord.isEmpty())
{
for(Opportunity opp : newrecord)
{
if(opp.AccountId != null)
{
Account acc = New Account();
acc.Id = opp.AccountId;
acc.No_of_Contact__c = opp.Amount;
accRecord.add(acc);
}
}
}

Trigger Level 01 : 5
Update accRecord;
}

================================================================

If(Trigger.isAfter && Trigger.isInsert)


{
OpportunityTriggerHandler.UpdateLatestAmount(Trigger.New)
}

8.On Account two Checkbox fields are avaliable


New Contact and New Opportunity

On Account Creation
If New Contact CheckBox is Checked then related contact should b

If New Opportunity checkbox is checked and Active = Yes,then rel

Public Static Void CreatedRelatedOpportunityAndContact(List<Ac


{
if(! newrecord.isEmpty())
{
List<Contact> ConRecord = New List<Contact>();
List<Opportunity> oppRecord = New List<Opportunity>();

for(Account accRecord :newrecord)


{
if(accRecord.New_Contact__c == True)
{
Contact Con = New Contact();
con.FirstName = 'checkbox';
con.LastName = 'To Create related Contact' + accReco
con.AccountId = accRecord.Id;
conRecord.add(con);

Trigger Level 01 : 6
}

If(accRecord.New_opportunity__c == True)
{
Opportunity opp = New Opportunity();

opp.Name = 'opportunity checking';


opp.CloseDate = system.Today();
opp.StageName = 'Prospecting';
opp.AccountId = accRecord.Id;
oppRecord.add(opp);
}

}
Insert ConRecord;
Insert oppRecord;
}

9.When Account Phone fields is Updated then Populated then popul

in description fields =
Phone is Updated!
OldPhoneNumber xxxx & NewPhoneNumber

Public Static Void PhonefieldsUpdate(List<Account> NewRecord,M


{
if(! NewRecord.isEmpty())
{
for(Account accRecord : NewRecord)
{
if(accRecord.Phone != OldRecords.get(accRecord.Id).P
{

Trigger Level 01 : 7
accRecord.Description = 'Phone Updated!' + OldRecor
}
}
}
}
================================================================

If(Trigger.IsBefore && Trigger.IsUpdate)


{
AccountTriggerHandler.PhonefieldsUpdate(Trigger.New,Trigge
}

10.When an Account is Insert or Updated and the Copybillingtoshi


then copy billing Address to Shipping Address

11.Upon Account Creation or Updation if Industry is Media then P

public class AccountTriggerHandler


{
Public Static Void InsertUpdateRating(List<Account> newRecord)
{
if(! newRecord.IsEmpty())
{
for(Account accRecord : newRecord)
{
If(accRecord.Industry != Null && (accRecord.Industry ==
{
accRecord.Rating = 'Hot';
}
}
}
}

Trigger Level 01 : 8
}
================================================================

AccountTriggerHandler.InsertUpdateRating(Trigger.New);

Trigger Level 01 : 9

You might also like