Coding Genius (Trigger)

The following practice questions are for Salesforce developers who are new to salesforce and wish to solve some basic programming questions. Write a trigger

1. On Account create a default contact every time an account is created. What is Trigger

Click here to learn more

trigger Accounttir on Account (after insert) {

list<contact> con = new list<contact>();

for(Account acc : trigger.new){

contact c = new contact();

c.LastName = acc.Name;

c.AccountID = acc.ID;

con.add(c);

}

insert con;

}

2. Bulkify the above trigger to manage multiple record insertion. Trigger Bulkify

Click here to learn more

trigger Accounttir on Account (after insert) {

list<contact> con = new list<contact>();

for(Account acc : trigger.new){

contact c = new contact();

c.LastName = acc.Name;

c.AccountID = acc.ID;

con.add(c);

}

insert con;

}

3. On Opportunity to not let the user insert Opportunities with pass closed Date with following Message “Please enter a future Closed Date”. This can also be implemented by a validation rule but we want a Trigger to be written

Click here to learn more

trigger opportunitydate on Opportunity (before insert) {

for(opportunity opp : trigger.new) {

if(opp.Closedate<=Date.Today()){

opp.adderror(‘please eneter a future closed date’);

}

}

}

4. On Product to setup default Pricebook entry in the “Standard Pricebook” as 1$. Product Schema Diagram

Click here to learn more

trigger Productupdate on Product2 (after insert) {

list<PricebookEntry> pblist = new list<PricebookEntry>();

Pricebook2 pb=[Select id FROM Pricebook2 WHERE IsStandard=true];

for(Product2 pro:trigger.new){

PriceBookEntry pbe=new PriceBookEntry();

pbe.pricebook2id = pb.id;

pbe.Unitprice=1;

pbe.Product2Id=pro.id;

pblist.add(pbe);

} insert pblist;

}

5. On Contact to update the Account Name field with contact Last Name, concatenated in the end. Example If “Tata” Account gets a contact “Ratan” created then Account Name field will be updated as “TataRatan”.

Click here to learn more

trigger updateAccname on Contact (after insert,before delete) {

List<Account> accountlist=new List<Account>();

if(trigger.isInsert){

for(Contact con:[select Accountid,Account.Name,LastName from Contact WHERE Id

IN:trigger.new]){

if(con.Account.Name!=null){

con.Account.Name+=con.LastName;

accountlist.add(con.Account);

}

}

}else if(trigger.isDelete){

for(Contact con : [select AccountId, Account.Name, LastName from Contact WHERE Id IN:

trigger.old]){

system.debug(‘** contact –‘+con + ‘ *** Account –‘ + con.AccountId + ‘ *** Account Name — ‘+

con.Account.Name);

con.Account.Name = con.Account.Name.replace(con.LastName,”);

accountlist.add(con.Account);

}

}

system.debug(‘*** ‘ +accountlist);

update accountlist;

}

6. Update the above trigger to delete the Last Name from the Account Company field when a Contact is deleted without deleting the other Last name. Example If “TataRatanNori” Account already have “Ratan” and “Nori” as its contacts and then contact “Ratan” gets deleted then Account Name field will be updated as “TataNori”.

Click here to learn more

trigger updateAccname on Contact (after insert,before delete) {

List<Account> accountlist=new List<Account>();

if(trigger.isInsert){

for(Contact con:[select Accountid,Account.Name,LastName from Contact WHERE Id

IN:trigger.new]){

if(con.Account.Name!=null){

con.Account.Name+=con.LastName;

accountlist.add(con.Account);

}

}

}else if(trigger.isDelete){

for(Contact con : [select AccountId, Account.Name, LastName from Contact WHERE Id IN:

trigger.old]){

system.debug(‘** contact –‘+con + ‘ *** Account –‘ + con.AccountId + ‘ *** Account Name — ‘+

con.Account.Name);

con.Account.Name = con.Account.Name.replace(con.LastName,”);

accountlist.add(con.Account);

}

}

system.debug(‘*** ‘ +accountlist);

update accountlist;

}

7. On Opportunity to alert the user when an Opportunity is being closed with no Opportunity line items in it.

Click here to learn more

trigger Opportunityalert on Opportunity (after update) {

for(Opportunity opp : [Select Id,StageName, (Select Id FROM OpportunityLineItems) FROM

Opportunity WHERE Id IN :Trigger.new])

{

if(opp.StageName == ‘Closed Won’ || opp.StageName == ‘CLosed Lost’ &&

opp.OpportunityLineItems.size() == 0 ){

trigger.newmap.get(opp.id).adderror(‘There is no opportunity Line Item ‘);

}

}

}

 

Author: AJ

Share This Post On

2 Comments

  1. Dear Sir,

    Great sir, There is No Other words…….

    Post a Reply

Submit a Comment

Your email address will not be published. Required fields are marked *

× How can I help you?