SFDC NoteBook - Trigger Scenario Questions
SFDC NoteBook - Trigger Scenario Questions
SFDC NoteBook - Trigger Scenario Questions
More
Create Blog
Sign In
SFDC NoteBook
Followers (6)
Follow
for(account a : trigger.old){
if(a.city__c == 'Hyderabad'){
opp.city__c = 'Mumbai';
Followers
oppor.add(opp);
}
Total Pageviews
}
}update oppor; 3,690
}
Pages
Home
Trigger Scenario 2 :
30 Salesforce Developer Interview
Apex trigger to update account rating when the opportunity stage equals closed won Questions & Answ...
30 Salesforce Admin Interview Questions
SFDC scenario based interview Questions
trigger Opportunitystageclosedwon_updatesaccountrating on Opportunity (before insert) {
Trigger Scenario Questions
list<account> accounts = new list<account>();
Salesforce Interview Questions : Part 1
for(account acc : [select name, rating from account]){ Salesforce Interview Questions : Part 2
for(opportunity opp : trigger.new){ Salesforce Interview Questions : Part 3
Salesforce Interview Questions : Part 4
if(opp.stagename == 'closed won'){
acc.Rating = 'hot';
Search This Blog
accounts.add(acc);
Search
}
}
About Me
}update accounts;
force
}
View my complete profile
Trigger Scenario 3 :
Blog Archive
Prevent an account from deleting with trigger, if it has 2 or more contacts.
▼
2018
(2)
▼
▼
October
(2)
▼
for(account a : trigger.old){
acctid.add(a.id);
}
map<id,account> accounts = new map<id,account>([select id, name, (select lastname,
firstname from contacts) from account where id in : acctid]);
for(account acc : trigger.old){
if(accounts.get(acc.Id).contacts.size()>=2){
sfdcnotebook.blogspot.com/p/trigger-scenario-questions.html 1/10
9/17/21, 10:29 AM SFDC NoteBook: Trigger Scenario Questions
Trigger Scenario 4 :
When ever new account is created with annual revenue more than five lakhs then add wilson as
account team member.
Trigger Scenario 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 Scenario 6 :
When ever a case is created with origin as email then set status as new and Priority as Normal
Trigger Scenario 7 :
When ever lead is created with leadSource as Web then give rating as cold otherwise hot
trigger leadexample on Lead (before insert) {
for(lead ld : trigger.new){
if(ld.leadsource == 'web'){
ld.Rating = 'cold';
sfdcnotebook.blogspot.com/p/trigger-scenario-questions.html 2/10
9/17/21, 10:29 AM SFDC NoteBook: Trigger Scenario Questions
}
else{
ld.Rating = 'hot';
}
}
}
Trigger Scenario 8 :
whenever a account phone is modified/updated then contact records with phone
fields(otherphone with oldvalue and homephone with newvalue) associated with this account
record gets updated as same as account phone field.
trigger accountupdatedexample on Account (before update) {
map<id,account> accold=trigger.oldmap;
map<id,account> accnew=trigger.newmap;
set<id> keys=accold.keySet();
list<id> myid=new list<id>();
for(ID K : keys){
account oldvalues = accold.get(K);
account newvalues = accnew.get(K);
if(oldvalues.phone!=newvalues.phone){
myid.add(K);
}
}
for(contact con : [select lastname, phone from contact where accountid in : myid]){
account a = accold.get(con.accountid);
account b = accnew.get(con.accountid);
con.otherphone = a.phone;
con.homephone = b.phone;
}
}
Trigger Scenario 9 :
When ever opportunity stagename is modifed to closed won then set closedate as today and type
as new Customer.
trigger opporupdate on Opportunity (before update){
Map<Id,Opportunity> oppold=Trigger.oldMap;
Map<Id,Opportunity> oppnew=Trigger.newMap;
Set<Id> keys =oppold.keySet();
for(Id rid :keys){
Opportunity oldopt=oppold.get(rid);
Opportunity newOpt=oppnew.get(rid);
if(oldopt.stagename!='Closed won' && newOpt.stagename=='Closed won'){
newOpt.closeDate=System.today();
newOpt.type='New Customer';
}
}
}
Trigger Scenario 10 :
when a new opportunity is created with amount more than 50 lakhs add the wilson as
opportunityteamMember.
trigger optamountteammember on Opportunity (before insert) {
list<OpportunityTeamMember> teams = new list<OpportunityTeamMember>();
user u = [select id from user where alias='schar'];
for(opportunity opt : trigger.new){
if(opt.amount > 5000000){
OpportunityTeamMember atm = new OpportunityTeamMember();
atm.OpportunityId = opt.id;
sfdcnotebook.blogspot.com/p/trigger-scenario-questions.html 3/10
9/17/21, 10:29 AM SFDC NoteBook: Trigger Scenario Questions
atm.UserId = u.id;
atm.TeamMemberRole = 'Opportunity Manager';
atm.OpportunityAccessLevel = 'All';
teams.add(atm);
}
}insert teams;
}
Trigger Scenario 11 :
whenever the stagename is modified to closedwon then set the closedate as today
apex class:
public class optytriggerhandler {
public void udpate(map<id,opportunity> oldmap, map<id,opportunity> newmap){
for(id keys : oldmap.keySet()){
opportunity oppold = oldmap.get(keys);
opportunity oppnew = newmap.get(keys);
if(oppold.Stagename!= 'closed won' && oppnew.Stagename == 'closed won'){
oppnew.CloseDate = system.today();
}
}
}
}
trigger class:
trigger OpportunityUpdate on Opportunity (before update) {
optytriggerhandler.udpate(trigger.oldmap, trigger.newmap);
}
Trigger Scenario 12 :
when a new lead is created with leadsource as web then make the other user as the owner of the
record.
trigger leadownerassignment on Lead (before insert) {
for(lead ld : trigger.new){
if(ld.leadsource == 'web'){
user u = [select id from user where alias = 'swethag'];
ld.ownerid = u.id;
}
}
}
Trigger Scenario 13 :
when a new contact is created for a existing account then set contact otherphone as account
phone.
trigger contactaccountrelation on Contact (before insert) {
Trigger Scenario 14 :
Create “Top X Designation” custom object which is the related list to Opportunity (Look up
Relationship). In the Top X Designation object, create the fields Type (Picklist), Document
Attached (Checkbox) Create one field Handoff Attached with pick list type with values are Yes,
No on Opportunity Object. Logic :- If Type (Top X Designation) = “Contract Flow
sfdcnotebook.blogspot.com/p/trigger-scenario-questions.html 4/10
9/17/21, 10:29 AM SFDC NoteBook: Trigger Scenario Questions
Down/Handoff”, and “Document Attached” = True then “Handoff Attached” = True, otherwise
false.
trigger updateHandoffattachedupdateHandoffattached on Top_X_Designation__c (after insert,
after update, after delete) {
map<id,Top_X_Designation__c> maptop = new map<id,Top_X_Designation__c>();
set<id> oppid = new set<id>();
map<id,Top_X_Designation__c> maptopfalse = new map<id,Top_X_Designation__c>();
set<id> oppidfalse = new set<id>();
list<opportunity> opplist = new list<opportunity>();
list<opportunity> opptoupdate = new list<opportunity>();
opportunity opp = new opportunity();
if(trigger.isafter){
if(trigger.isinsert || trigger.isupdate){
for(Top_X_Designation__c top : trigger.new){
if(top.Document_Attached__c == true && ((top.Type__c=='Contract Flow Down') ||
(top.Type__c=='Handoff'))){
maptop.put(top.OppLookUp__c, top);
oppid.add(top.OppLookUp__c);
}else{
maptopfalse.put(top.OppLookUp__c, top);
oppidfalse.add(top.OppLookUp__c);
}
}
opplist = [select stagename, handoff_attached__c from opportunity where ID in : oppid
and ID in : oppidfalse];
for(opportunity opp : opplist){
if(maptop.containsKey(opp.Id)){
opp.Handoff_Attached__c = 'yes';
opptoupdate.add(opp);
}
if(maptopfalse.containsKey(opp.id)){
opp.Handoff_Attached__c ='no';
opptoupdate.add(opp);
}
}
if(opptoupdate.size()>0){
update opptoupdate;
}
}
}
}
Trigger Scenario 15 :
The following Trigger will fires when we try to create the account with same name i.e. Preventing
the users to create Duplicate Accounts
trigger AccountDuplicateTrigger on Account (before insert, before update) {
map<string, account> accountmap = new map<string, account>();
sfdcnotebook.blogspot.com/p/trigger-scenario-questions.html 5/10
9/17/21, 10:29 AM SFDC NoteBook: Trigger Scenario Questions
for(account account : [select id, name from account where name IN : accountmap.keyset()]){
account accountnewmap = accountmap.get(account.name);
accountnewmap.Name.adderror('account with this name already exixts');
}
}
Trigger Scenario 16 :
Trigger to count the total number of contact records associated
with that Account
trigger Accountcountcontacts on Contact (after insert, after update, after delete, after undelete) {
set<id> acctids = new set<id>();
sfdcnotebook.blogspot.com/p/trigger-scenario-questions.html 6/10
9/17/21, 10:29 AM SFDC NoteBook: Trigger Scenario Questions
Trigger Scenario 18 :
1. Set the OWD on the opportunity as private 2. Remove modify all permission on wilson's profile
3. whenever the opportunity stagename is modified to closed won and amount is more than 10
lacs share the record with wilson
Apex Class:
Trigger Class:
trigger afterupdateopptrigg on Opportunity (after update) {
afterupdateopptrigg.afterupdate(trigger.old, trigger.new);
}
Trigger Scenario 19 :
When a new Account record is inserted verify the industry field value, if industry field value is
Education then assign the owner as karthic
Trigger :
TestClass :
@isTest
private class AccountOwnerTest {
@isTest
static void testme(){
User u=[select id from User where username='[email protected]'];
sfdcnotebook.blogspot.com/p/trigger-scenario-questions.html 7/10
9/17/21, 10:29 AM SFDC NoteBook: Trigger Scenario Questions
Trigger Scenario 20 :
When ever we are trying to delete the account record which has
a contacts for it .,Then deletion has to fail
Trigger :
trigger accountDelete on Account (before Delete) {
List<Account> accsList=[select id,name,(select id from Contacts) from Account where id
in:Trigger.Old];
for(Account a:accsList){
if(a.contacts.size()>0)
a.addError('We cannot delete this account');
}
}
TestClass :
@isTest
private class accountDeleteTest {
@isTest
static void testMe(){
Account a1=new Account(Name='Test');
insert a1;
Contact c1=new Contact(LastName='test',accountId=a1.id);
insert c1;
Account a2=new Account(name='Demo');
insert a2;
Integer myCount=[select count() from Account];
try{
delete a1;
}catch(Exception e){
System.debug(e);
}
Integer count=[select count() from Account];
System.assertEquals(count,mycount);
try{
delete a2;
}catch(Exception e){
System.debug(e);
}
Integer size=[select count() from Account];
System.assertEquals(size,mycount-1);
}
}
Trigger Scenario 21 :
When ever account record is deleted successfully send the email
sfdcnotebook.blogspot.com/p/trigger-scenario-questions.html 8/10
9/17/21, 10:29 AM SFDC NoteBook: Trigger Scenario Questions
Trigger Scenario 22 :
1.Create two objects creditcard and Application . 2.Create lookup field on Application choosing
Creditcard as parent 3.When we delete any creditcard record all corresponding application
records also be deleted
trigger creditcardDeletion on CreditCard__c (before delete) {
List<Id> cards=new List<Id>();
for(CreditCard__c c:Trigger.Old){
if(c.card_Type__c=='Platinum'){
cards.add(c.id);
}
}
List<Application__c> apps=[select id from Application__c where CreditCard__c in:cards];
delete apps;
}
1 comment:
Comment as:
tiwariaditya46@ Sign out
Publish Preview
Notify me
Home
Subscribe to:
Posts (Atom)
sfdcnotebook.blogspot.com/p/trigger-scenario-questions.html 9/10
9/17/21, 10:29 AM SFDC NoteBook: Trigger Scenario Questions
sfdcnotebook.blogspot.com/p/trigger-scenario-questions.html 10/10