Search Here

Scenario 4: When an Account’s status changes to ‘Inactive’, all related Opportunities should be marked as ‘Closed Lost’. How would you accomplish this?

 This would be an ‘after update’ trigger on the Account object. The trigger checks if the status has changed to ‘Inactive’ and then updates the status of all related Opportunities to ‘Closed Lost’.


trigger CloseRelatedOpportunities on Account (after update) {

List<Opportunity> oppsToUpdate = new List<Opportunity>();

for(Account acc : Trigger.new){

if(acc.Status__c == ‘Inactive’ && (Trigger.oldMap.get(acc.Id).Status__c != ‘Inactive’)){

oppsToUpdate.addAll([SELECT Id FROM Opportunity WHERE AccountId = :acc.Id AND StageName != ‘Closed Lost’]);

}

}

for(Opportunity opp : oppsToUpdate){

opp.StageName = ‘Closed Lost’;

}

if(!oppsToUpdate.isEmpty()){

update oppsToUpdate;

}

}

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.