Trigger

You might also like

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 2

//Oppstage= closed won then update account status to active and related contact

status to active

//Oppportunity - After Update

//Set<Id> accIds

//Check If Stage Filed is updated or not

//we will update account, Contact

trigger updateRelatedAccContact on Opportunity(After update){

If(Trigger.isAFter){
If(Trigger.isUpdate){

OpportuntyTriggerHandler.updateRelatedAccCont(Trigger.New,
Trigger.OldMap);
}
}
}

Class OpportuntiyTriggerHandler{

public static void updateRelatedAccCont(List<Opportunity> oppList, Map<Id,


Opprtunity> oldOppMap){

Set<Id> accIds=new Set<Id>();

for(Opportunity opp: oppList){


if(opp.StageName!='' && oldOppMap.get(opp.id).StageName!
=opp.StageName)
{
if(opp.AccountId!=null){
accIds.add(opp.AccountId);
}
}
}

if(!accIds.isEmpty())
{ //getexisting accounts
List<Account> relatedAccs=[Select Id, status From Account Where
Id=:accIds];
List<Contact> relatedCons=[Select Constatus From Account Where
AccountId=:accIds];

///

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


for(Account acc : relatedAccs){
acc.status = 'Active';
accListToUpdate.add(acc);
}

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


for(Contact con : relatedCons){
con.Constatus = 'Active';
accListToUpdate.add(con);
}

Update accListToUpdate;
update conListToUpdate;

}
}

You might also like