Trigger Asked In Interview
1. 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)
{
for(Account a:Trigger.new)
{
List<Account> acc=[Select id from Account where Name=:a.Name and Rating=:a.Rating
];
if(acc.size()>0)
{
acc.Name.addError('You Cannot Create the Duplicate Account');
}
}
}
2. The following trigger adds the Opportunity Owner into the sales team automatically
whenever the Opportunity is created.
trigger OppTeam on Opportunity (after insert) {
List<OpportunityShare> sharesToCreate = new List<OpportunityShare>();
List<OpportunityTeamMember> oppteam = new List<OpportunityTeamMember
> ();
OpportunityShare oshare = new OpportunityShare();
oshare.OpportunityAccessLevel = 'Edit';
oshare.OpportunityId = trigger.new[0].Id;
oshare.UserOrGroupId = trigger.new[0].createdbyid;
sharesToCreate.add(oshare);
OpportunityTeamMember ot = new OpportunityTeamMember();
ot.OpportunityId = trigger.new[0].Id;
ot.UserId = trigger.new[0].OwnerId;
ot.TeamMemberRole = 'Account Manager';
oppteam.add(ot);
if(Oppteam!=null && Oppteam.size()>0)
{
insert Oppteam;
}
if(sharesToCreate!=null && sharesToCreate.size()>0)
{
list<Database.SaveResult> sr = Database.insert(sharesToCreate,false);
}
}
3.The following trigger will prevent the users from deleting the Accounts. This is because
System Administrator has all the permissions, we cannot change the permissions.
trigger AccountDelete on Account (before delete) {
for(Account Acc:trigger.old)
{
acc.adderror('You Cannot Delete the Account Record');
}
}
4 Create Custom field called “Number of Locations” on the Account Object (Data
Type=Number)
https://www.amazon.in/gp/product/B07DJHV6S7/ref=as_li_tl?ie=UTF8&camp=3638&creative=24630&creativeASIN=B07DJHV6S7&linkCode=as2&tag=shopper058-21&linkId=350b196c5b43942ffdd312cce36ef52f
The following trigger 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
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.Number_of_Locations__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;
}
for Question 1, You have written SQQl inside for loop. This is not good practice.
ReplyDeleteTry instead:
Check using containsKey condition
trigger DemoTrigger on Account(before insert,before update) {
List accList=new List([select id,name from Account]);
map duplicateMap=new map();
//get old records
for(Account acc:accList){
duplicateMap.put(acc.Name,acc);
}
//compare old records with records in trigger.new
for(Account acc:Trigger.new){
if(duplicateMap.containsKey(acc.Name)){
acc.adderror('Name already exists');
}
}
}
http:www.sfdcamplified.com