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

Trigger

The document describes various triggers that are used to automatically update records or create related records when certain conditions are met. The triggers cover scenarios like creating contacts when accounts are created, setting fields on cases based on origin, preventing duplicate accounts, updating leads, creating opportunities if an account rating changes, and more.

Uploaded by

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

Trigger

The document describes various triggers that are used to automatically update records or create related records when certain conditions are met. The triggers cover scenarios like creating contacts when accounts are created, setting fields on cases based on origin, preventing duplicate accounts, updating leads, creating opportunities if an account rating changes, and more.

Uploaded by

siddesh shinde
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

1) If an Account is created then it should create the contact under account with

account name.

Trigger AccountCreation on Account(after Insert){


List<Contact> contactList = new List<Contact>();
for(Acccount accobj : trigger.new){
Contact cont = new Contact();
cont.LastName = accobj.Name;
cont.AccountId = accobj.Id;
contactList.add(cont);
}
if(contactList!=null && contactList.size>0){
Insert contactList;}

2) If Rating is hot then type should be selected before saving to database

Trigger AccountRat on Account(before insert){


for(Account accobj : trigger.new){
if(Rating=='Hot' && type==null){
accobj.addError('Type is mandatory');
}
}
}

3) When ever lead is created with LeadSource as Web then give rating as cold
otherwise hot.

Trigger LeadConv on Lead(before insert){


for(Lead led : trigger.new){
if(led.Leadsource == 'web'){
led.rating = 'cold';
}
else{
led.rating = 'hot';
}
}
}

4) Whenever a case is created with origin as email then set status as new and
priority as medium
Trigger Casecret on Case(before insert){
for(Case cret : trigger.new){
if(cret.Case Origin == 'Email'){
cret.Status='New';
cret.Priority='Medium';
}
}
}

5)When ever the Account is created with Industry as Banking then create a contact
for account, Contact Lastname as Account name and contact phone as account phone.

Trigger AccountCreat on Account(after Insert){


List<Contact> cont = new List<Contact>;
for(Account acc : Trigger.new){
if(acc.Industry=='Banking'){
Contact con = new Contact;
con.LastName=acc.Name;
con.Phone=acc.Phone;
con.AccountId=acc.Id;
cont.add(con);
}
}
if(cont!=null && cont.size>0){
insert(cont);
}
}

6)The following Trigger will fires when we try to create the account with the same
name i.e. Preventing the users to create Duplicate Accounts

Trigger AccountSame on Account(before insert){


for(Account acc : trigger.new){
List<Account> acct = "Select id from Account where Name=:acc.Name and
Rating=:acc.Rating';
if(acct.size()>0){
acct.Name.addError('You Cannot create Duplicate Account');
}
}
}

7)The following trigger describes when the leads are inserted into the database it
would add Doctor prefixed for all lead names. This is applicable for both inserting
and updating the lead records.

Trigger LeadUpdate on Lead(before Update, before insert){


for(Lead led : trigger.new){
led.Firstname = 'Dr.' + led.FirstName;
}
}

8) create an opportunity when Account "Rating" is changed to "Hot"

whenever u have to check the old and new value we use TriggerHandler
trigger accountUpdate on Account(after Update){
if(trigger.isUpdate && trigger.isAfter){
Account.TriggerHandler(createOpportunity);
}}

class

public static void createOpportunity(map<id,Account> actNewmap,map<id,Account>


actOldmap){

List<Oppportunity> opp = new List<Opportunity>();


for(Account acc : actNewmap.values()){
if(actoldmap.get(acc.id).Rating != actNewmap.get(acc.id).Rating &&
actNewmap.get(acc.id).Rating == 'Hot'){
Opportunity oppo = new Opportunity();
oppo.AccountId = acc.Id;
oppo.Name = acc.Name;
opp.add(oppo);
}
if(opp != null && opp.size>0){
insert opp;}
}

9) prevent account deletion when there is primary contact under account can't
understand but use trigger handler

trigger Accountpr on Accont(before delete){


for(Account acc : trigger.old){
if(acc.IsPrimary == true){

10)Throw an error whenever the user try to delete the contact which is not
associated to account

trigger Accountdel on Account(before delete){


for(Account acc : trigger.old){
if(Contact.AccountId == null){
Contact.addError("Associated Contact is not Available");
}
}
}

11)Create a field called "Contact Relationship" checkbox on the contact object and
create the object called "Contact Relationship" which is related list to the
Contacts (Lookup Relationship).
Now build a logic when we create contact by checking Contact Relationship checkbox,
then contact Relationship will be created automatically for that contact.

trigger contactRela on contact(after insert){


List<contact Relationship> conr = new List<contact Relationship>();
for(contact co :trigger.new){
if(co.contact Relationship == true){
contact Relationship cor = new contact Relationship();
cor.Name = co.LastName;
cor.Contact__c = co.id;
conr.add(co);}
}
insert(conr);}

12)Suppose we received a business requirement that we need to create an Invoice


Record when Customer's 'Customer Status' field changes to Active from Inactive.

trigger Customerst on Customer(after update){


List<InvoiceRecord> Ivc = new <InvoiceRecord>();
for(Customer cus : trigger.new){
if(Customer Status__c == 'Active'){
InvoiceRecord__c Iv = new InvoiceRecord__c();
Iv.Status__c ='pending';
Ivc.add(Iv);}
}
Insert(Ivc);}

13)The following trigger updates the field called “Hello” by the value
“World”whenever we are creating an account or updating an account record. Create
the field called “Hello” on the Account Object (Data Type = Text)

trigger FieldUpdate on account(before insert, before update){


for(Account acc : trigger.new){
if(acc.Hello__c != 'world'){
acc.Hello__c = 'world';}}}

You might also like