You are on page 1of 5

Trigger Assignment

1. When an account is inserted or updated,check whether any opportunity


is linked to it or not,if not then create one whose name is ‘First
Opportunity -<Account Name>’.

Trigger accountOpportunity Trigger on Account(after,update,after,insert)

If(trigger.isinsert || trigger.isupdate)

List<Account> acclist = Trigger.new;

List<id> l1 =new list<id>();

For(Account acc: acclist)

L1.add(acc.id);

List<Account> l2 =[select Name, (Select Name CloseDate, StageName from opportunities) from Account where ID
IN: L1];

For Account acc1: l2)

For(Account acc2:acclist

If(acc1.id==acc2.id

{if(acc1.opportunities.size() < 1)

Opportunity opp = new opportunity

(Name = ‘FirstOpportunity – ‘ + acc1.Name, ClosedDate = Date.NewInstance(2022,08,24), StageName =


‘Qualification’, Account ID = acc1.id);

Insert opp;

}
}

2.
When an opportunity is inserted check for its duplicity on the basis of its
name and the account to which it is related to i.e. If it has same name and it’s
linked to same Account then append “Duplicate Opportunity” in the name.

Trigger OpportunityTrigger on Opportunity(before insert)

List<opportunity> l1 = trigger.new;

List<opportunity> l2 = [select Name, StageName, CloseDate, Account ID from Opportunity];

For(Opportunity opp1: l1)

For(Opportunity opp2: l2)

If(Opp1.Name ==Opp2.Name && Opp1.AccountID == Opp2.AccountID)

OPp1.Name = Opp1.Name + ‘Duplicate Opportunity’;

4. Prevent deletion of account if it has any open opportunity related to it,


along with an error message.

Trigger accountOpportunityTrigger on Account(before Delete)

List<Account> acclist = Trigger.Old;

List<ID> ListID = new list<ID>();


For (Account acc: acclist)

ListID.add(acc.id);

For(Account acc: acclist)

ListID.add(acc.ID);

For (Account acc1: listacc)

For (Account acc2: acclist)

if(acc1.id==acc2.id)

If(acc1.opportunities.size()>0)

For(Opportunity opp:acc1.opprtunities)

If(Opp.stageName!=’Closed Won’)

Acc2.adderror(‘Account cannot be deleted as there are unclosed Opportunities)

5. Insert Account and opportunities data through CSV (Min 200 records).
Whenever any opportunity gets inserted we have to check that if the amount
is greater than 10000$ or not, if yes, then update the related account record
by setting “Rating” as Hot and “Customer Priority” as High.
Note: First import the Account records and then import opportunities relating
it with accounts. It is mandatory to relate each opportunity to an account.
Trigger insertOpportunityTrigger on opportunity(after insert)

List<Opportunity> newOpp = trigger.new;

List<ID> listID = new list<ID>();

For(Opportunity opp: newOpp)

List ID.add(opp.AccountID);

List<Account> acc =

[select Name, Rating,CustomerPriority__c,(select Name, StageName, Amount, CloseDate from Opportunities)


from Account where ID IN:listID];

For(Opportunity opp:newopp)

listID.add(opp.AccountID);

List<Account> acc =[Select Name, Rating, CustomerPriority__c(select Name, StageName, Amount, CloseDate
from Opportunities) from Account where ID IN:listID];

For(Account acc1: acc)

For(Opportunity obj: acc1.opportunities)

If(obj.Amount > 10000)

Acc1.Rating = ‘Hot’;

Acc1.CustomerPriority__c=’High’;

Update acc1;

9. Write a trigger to stop an user from creating an opportunity if he has more


than 2 open
opportunities.
Trigger insertOpportunityTrigger on Opportunity(before insert)

List<opportunity> newlist = trigger.new;


List<opportunity> oldlist = [select Name,CloseDate,StageName from Opportunity];

Integer i=0

For(opportunity newopp:newlist)

For(opportunity list:oldlist)

If(list.StageName != ‘Closed Won’)

I=i+1;

6. Whenever a lead gets saved with the source as “Phone Inquiry” then
automatically convert
it.

Trigger leadTrigger on Lead(before insert)

List<lead> leadlist = trigger.new;

For(leadl1:leadlist)

If(l1.leadsource == ‘Phone Inquiry’)

L1.status=’Closed – Converted’;

If (i>=2)

Newopp.adderror(‘New opportunity cannot be created as there are already two open opportunities pending’);

You might also like