You need to sign in to do that
Don't have an account?
Andrew marshal 3
Write a trigger whenever a opportunity stage is changed to closed won , change account rating to Hot.
Write a trigger whenever a opportunity stage is changed to closed won , change account rating to Hot.
Here's an example trigger that will update the Account rating to "Hot" whenever an Opportunity stage is changed to "Closed Won".
If it helps please mark this as Best Answer.
Thank you.
All Answers
Here's an example trigger that will update the Account rating to "Hot" whenever an Opportunity stage is changed to "Closed Won".
If it helps please mark this as Best Answer.
Thank you.
Hello Andrew,
Please find the below trigger for the question you asked. This trigger will cover your scenario of after and before as well.
//trigger controller class
trigger AccountRatingUpdate on Opportunity (before insert, after insert, before update, after update) {
if(trigger.isAfter){
if(trigger.isinsert){
OpportunityHandler.accountRatingUpdateWhenInsert(trigger.new);
}
if(trigger.isUpdate){
OpportunityHandler.accountRatingUpdateWhenUpdate(trigger.newMap,trigger.oldMap);
}
}
if(trigger.isbefore){
if(trigger.isinsert){
OpportunityHandler.accountRatingUpdateWhenInsert(trigger.new);
}
if(trigger.isUpdate){
OpportunityHandler.accountRatingUpdateWhenUpdate(trigger.newMap,trigger.oldMap);
}
}
}
----------------------------------------------------------------------------------------------------------------------------------------------
//trigger Controller class
public class OpportunityHandler {
public static void accountRatingUpdateWhenInsert(List<Opportunity> opportunityNewList){
List<Opportunity> opportunityList = [SELECT ID, StageName, AccountId
FROM Opportunity
WHERE StageName = 'Closed Won'
AND ID IN:opportunityNewList];
Set<Id> accId = new Set<Id>();
if(!opportunityList.isEmpty()){
for(Opportunity opp : opportunityList){
accId.add(opp.AccountId);
}
}
List<Account> accList = [SELECT ID, Rating FROM Account WHERE ID IN:accId];
for(Account acc : accList){
acc.Rating = 'Hot';
}
update accList;
}
public static void accountRatingUpdateWhenUpdate(Map<Id,Opportunity> OpportunityNewMap,Map<Id,Opportunity> OpportunityOldMap){
List<Opportunity> oppList = new List<Opportunity>();
for(Opportunity opp : OpportunityNewMap.Values()){
if(opp.StageName != OpportunityOldMap.get(opp.Id).StageName){
oppList.add(opp);
}
}
List<Opportunity> opportunityList = [SELECT ID , StageName, AccountId
FROM Opportunity
WHERE StageName = 'Closed Won'
AND ID IN:oppList];
Set<Id> accId = new Set<Id>();
if(!opportunityList.isEmpty()){
for(Opportunity opp : opportunityList){
accId.add(opp.AccountId);
}
}
List<Account> accList = [SELECT ID, Rating FROM Account WHERE ID IN:accId];
for(Account acc : accList){
acc.Rating = 'Hot';
}
update accList;
}
}
If it helps please mark this as the Best Answer.
Thank you.
If you find this answer helpful. Please mark it as the best answer.
Here's an example trigger whenever a opportunity stage is changed to closed won , change account rating to Hot.
trigger UpdateAccountRating on Opportunity (after update) {
// Create a set of Account IDs to update
Set<Id> accountIdsToUpdate = new Set<Id>();
// Loop through the updated Opportunities
for (Opportunity opp : Trigger.new) {
// Check if the Stage has changed to "Closed Won"
if (opp.StageName == 'Closed Won' && Trigger.oldMap.get(opp.Id).StageName != 'Closed Won') {
// Add the Account ID to the set of Accounts to update
accountIdsToUpdate.add(opp.AccountId);
}
}
// Query for the Accounts to update
List<Account> accountsToUpdate = [SELECT Id, Rating FROM Account WHERE Id IN :accountIdsToUpdate];
// Loop through the Accounts and update the Rating
for (Account acct : accountsToUpdate) {
acct.Rating = 'Hot';
}
// Update the Accounts
update accountsToUpdate;
}
If it helps please mark this as the Best Answer.
Thank you