Search Here

Trigger Scenario Based Questions in Salesforce

 Trigger Scenario 1 :

When ever a case is created with origin as email then set status as new and Priority as Medium.

Object : Case
Trigger: Before Insert

Trigger Code: CaseOrigin.apxt

trigger CaseOrigin on Case (before insert) {
for(case c : trigger.new){
if(c.origin == 'Email'){
c.status = 'New';
c.priority = 'Medium';
}
}
}

Output :

–> Case is created with Origin as “Email” :

Status : Working
Priority : High
Case Origin : Email

–> Before Insert :

As per the requirement, we are performing an operation on the trigger when the user save the new case that means we need to use as before insert trigger.

Status : New
Priority : Medium
Case Origin : Email

Trigger Scenario 2 :
When ever Lead is created with LeadSource as Web then give rating as cold otherwise hot.

Object : Lead
Trigger: Before Insert

Trigger Code: LeadScenario.apxt

trigger LeadScenario on Lead (before insert) {
for(lead ld : trigger.new){
if(ld.leadsource == 'Web')
{
ld.Rating = 'Cold';
}
else{
ld.Rating = 'Hot';
}
}
}

Output:

–> Lead is created with LeadSource as Web :

–> Before Insert :

As per the requirement, we are performing an operation on the trigger when the user save the new lead that means we need to use as before insert trigger.

Here the user has selected the lead source as ‘web’, so the rating will be ‘cold’

Trigger Scenario 3 :
Whenever New Account Record is created then needs to create associated Contact Record automatically.

Object : Account
Trigger: After Insert

Description : When ever new Account record is successfully created, then create the corresponding contact record for the account with:

•account name as contact lastname
•account phone as contact phone

Trigger Code: AccountAfter.apxt

trigger AccountAfter on Account (after insert) {
List<contact> cons=new List<contact>();
for(Account acc: Trigger.New){
Contact c=new Contact();
c.accountid=acc.id;
c.lastname=acc.name;
c.phone=acc.phone;
cons.add(c)
}
insert cons;
}

Test Class:

@isTest
private class AccountAfterHandler {
@isTest
static void testme(){
Integer count=[select count() from Account];
Integer size=[select count() from Contact];
Account acc=new Account(Name='LearnFrenzy',phone='022-845454');
try{
insert acc;
}
catch(Exception e){
System.debug(e);
}
Integer newCount=[select count() from Account];
Integer newsize=[select count() from Contact];
Contact c=[select lastname,phone from Contact where accountid=:acc.id];
System.assertEquals(c.lastname,acc.name);
System.assertEquals(c.phone,acc.phone);
}
}

Trigger Scenario 4 :
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.

Object : Account
Trigger: After Insert

Trigger Code: CreateAccountContact.apxt

trigger CreateAccountContact on Account (after insert) {
list<contact> contacts = 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;
contacts.add(con);
}
}
insert contacts;
}

Output:

–> New Account is created with Industry as Banking :

–> After Insert :

As per the requirement, we are performing an operation on the trigger when the user save the new account that means we need to use as after insert trigger.

AFTER triggers are usually used when information needs to be updated in a separate table/object due to a change. They run after changes have been made to the database (not necessarily committed).

Trigger Scenario 5 :
Creates the number of contacts which are equal to the number which we will enter in the Number of Locations field on the Account Object.

Create Custom field called “Number of Locations” on the Account Object (Data Type=Number)

Object : Account
Trigger: After Insert

Trigger Code: ContactsCreation.apxt

trigger ContactsCreation on Account (after insert) {
list<contact> listContact = new list<contact>();
map<id,decimal> mapAcc = new map<id,decimal>();
for(Account acc:trigger.new){
mapAcc.put(acc.id,acc.NumberofLocations__c);
}
if(mapAcc.size()>0 && mapAcc!=null){
for(Id accId:mapAcc.keyset()){
for(integer i=0;i<mapAcc.get(accId);i++){
contact newContact=new contact();
newContact.accountid=accId;
newContact.lastname='contact'+i;
listContact.add(newContact);
}
}
}
if(listContact.size()>0 && listContact!=null)
insert listContact;
}

Output:

–> Enter in the Number of Locations field on the Account Object. :

Here the user has created a new account ‘LearnFrenzy’ and the Number of Locations is 4.

–> After Insert :

As per the requirement, we are performing an operation on the trigger when the user creates the number of contacts which are equal to the number which we will enter in the Number of Locations field on the Account Object. That means we need to use as after insert trigger.

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.