function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
MaggieSumitMaggieSumit 

I want add One more Logic Like If Opportunity Stage = Closed Lost then Account Status should Be Lost. In the below Code. How to I Implement ​

Currently Logic is If a Account Contains two Opportunity if 1.Opportunity Stage is "Closed Won" and If I am Updating 2 Opportunity Stage = "Closed Lost" then it will Update Account Status = Customer. 

I want add One more Logic Like If Opportunity Stage = Closed Lost then Account Status should Be Lost. In the below Code. How to I Implement 

public static void AfterListUpdateHandler(List<Opportunity> newOpps){
        Set<Id> accIds = new Set<Id>();
        Set<Account> acclists = new Set<Account>();
            for(Opportunity opp : newOpps){
                if(opp.StageName == STAGE_CLOSED_LOST){
                    accIds.add(opp.AccountId);
                }             
            }
        System.debug('OppListStage1 ::>'+accIds);
        Opportunity [] OppListStage  = [SELECT Id,StageName,AccountId FROM Opportunity WHERE AccountId IN: accIds AND StageName =: STAGE_CLOSED_WON];
        System.debug('OppListStage2 ::>'+OppListStage);
        
            if(OppListStage.size()>0){
                for(Opportunity op : OppListStage){
                    Account acc = new Account();
                    acc.Id = op.AccountId;
                    acc.Account_Status__c = STATUS_CHURNED_CUST;
                    acclists.add(acc);              
                }
                if(checkRecursive.runOnce()){
                    update new List<Account>(acclists);
                    System.debug('opp1 ::>acclists'+acclists);
                }
                 
            }    
        }
Best Answer chosen by MaggieSumit
Alex EzhilarasanAlex Ezhilarasan
I think your requirement is if there is no other Opportunity with STAGE_CLOSED_WON for the Account, you want to set the Account_Status to LOST. If so, this is the code
 
public static void AfterListUpdateHandler(List<Opportunity> newOpps){
	
	Set<Id> accIds = new Set<Id>();
	Set<Account> acclists = new Set<Account>();
	List<Account> updateStatusAcctList = new List<Account>();

    for(Opportunity opp : newOpps){
        if(opp.StageName == STAGE_CLOSED_LOST){
            accIds.add(opp.AccountId);
        }             
    }
	System.debug('OppListStage1 ::>'+accIds);
	Opportunity [] OppListStage  = [SELECT Id,StageName,AccountId FROM Opportunity WHERE AccountId IN: accIds AND StageName =: STAGE_CLOSED_WON];
	System.debug('OppListStage2 ::>'+OppListStage);

    if(OppListStage.size()>0){
        for(Opportunity op : OppListStage){
            Account acc = new Account();
            acc.Id = op.AccountId;
            acc.Account_Status__c = STATUS_CHURNED_CUST;
            acclists.add(acc);  
            if(accIds.contains(op.AccountId)) accIds.remove(op.AccountId);            
        }
        if(checkRecursive.runOnce()){
            update new List<Account>(acclists);
            System.debug('opp1 ::>acclists'+acclists);
        }
    }
    if( accIds.size() > 0 ){
    	for(Id acctId : accIds){
    		updateStatusAcctList.add(new Account( Id = op.AccountId, Account_Status__c = LOST));
    	}

    	//Update accout status with Lost(New logic)
    	update updateStatusAcctList
    } 
}


 

All Answers

Alex EzhilarasanAlex Ezhilarasan
I think your requirement is if there is no other Opportunity with STAGE_CLOSED_WON for the Account, you want to set the Account_Status to LOST. If so, this is the code
 
public static void AfterListUpdateHandler(List<Opportunity> newOpps){
	
	Set<Id> accIds = new Set<Id>();
	Set<Account> acclists = new Set<Account>();
	List<Account> updateStatusAcctList = new List<Account>();

    for(Opportunity opp : newOpps){
        if(opp.StageName == STAGE_CLOSED_LOST){
            accIds.add(opp.AccountId);
        }             
    }
	System.debug('OppListStage1 ::>'+accIds);
	Opportunity [] OppListStage  = [SELECT Id,StageName,AccountId FROM Opportunity WHERE AccountId IN: accIds AND StageName =: STAGE_CLOSED_WON];
	System.debug('OppListStage2 ::>'+OppListStage);

    if(OppListStage.size()>0){
        for(Opportunity op : OppListStage){
            Account acc = new Account();
            acc.Id = op.AccountId;
            acc.Account_Status__c = STATUS_CHURNED_CUST;
            acclists.add(acc);  
            if(accIds.contains(op.AccountId)) accIds.remove(op.AccountId);            
        }
        if(checkRecursive.runOnce()){
            update new List<Account>(acclists);
            System.debug('opp1 ::>acclists'+acclists);
        }
    }
    if( accIds.size() > 0 ){
    	for(Id acctId : accIds){
    		updateStatusAcctList.add(new Account( Id = op.AccountId, Account_Status__c = LOST));
    	}

    	//Update accout status with Lost(New logic)
    	update updateStatusAcctList
    } 
}


 
This was selected as the best answer
MaggieSumitMaggieSumit
Thanks :)