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
Rahul63Rahul63 

Can any one please help as below code is not working for multiple records at a time. Our scenario is to update the Opportunity stage based on Opportunity Building and Quote creation. If those doesn't have then we need to throw the error.

public void beforeUpdate( ) {
    List<Opportunity> oppLst = Trigger.old;
List < Opportunity > newOptyList = Trigger.New;
    List < Quote > QuoteList = [SELECT Id, Name, Opportunity.name  FROM Quote where OpportunityID = : oppLst[0].Id];
    List < Opportunity_Building__c > buildingopportunityList = new List < Opportunity_Building__c > ([SELECT Opportunity__c,Id, Name FROM Opportunity_Building__c where Opportunity__c = : oppLst[0].Id]);
   for(Opportunity oppNew :newOptyList ) {
    for(opportunity oppOld: oppLst){
        if ((buildingopportunityList.size() == 0 )){ 
        if(oppOld.StageName == 'Prospecting' && oppNew.StageName == 'Cost analysis' ) {
            oppNew.StageName.addError(Label.Opp_Stage_CostAnalysis_Error_without_OppBldg);
           }
        
        }
        if ((QuoteList.size() == 0)){
             if(oppOld.StageName == 'Cost analysis' && oppNew.StageName == 'Offer negotiation'){
                oppNew.StageName.addError(Label.Opp_Stage_Offernegotiation_Error_without_Quote);
            }
        }   
Rahul63Rahul63
Above code was working as expected but code needs to remodified to work for multiple records. Can you please help me. Thanks in Advance
Wilfredo Morillo 20Wilfredo Morillo 20
//This shuold help you support multiple opps

public void beforeUpdate( ) {
    
    
    AggregateResult allQuoteList = [select Opportunity__c,count(id) qouteCount from Opportunity_Building__c where Opportunity__c :trigger.old group by opportunity__c];
     AggregateResult allBuildingopportunity = [select Opportunity__c,count(id) buildingCount from Opportunity_Building__c where Opportunity__c :trigger.old group by Opportunity__c];
     
    Map<id,integer> qouteCountMap = new Map<id,integer>();
    Map<id,integer> BuildingCountMap = new Map<id,integer>();
    
    for(AggregateResult ar:allQuoteList){
        qouteCountMap.put((id)ar.get('Opportunity__c'),(Integer)ar.get('qouteCount'));
        
    }
    for(AggregateResult ar:allBuildingopportunity){
        BuildingCountMap.put((id)ar.get('Opportunity__c'),(Integer)ar.get('buildingCount'));      
    }
    
    
  for(Opportunity oppNew :trigger.new ) {
    Opportunity oppOld = trigger.oldmap.get(oppNew.id);
      
        if (BuildingCountMap.get(oppOld.id)== 0 ){ 
        if(oppOld.StageName == 'Prospecting' && oppNew.StageName == 'Cost analysis' ) {
            oppNew.StageName.addError(Label.Opp_Stage_CostAnalysis_Error_without_OppBldg);
           }
        
        }
         if (qouteCountMap.get(oppOld.id)== 0 ){
             if(oppOld.StageName == 'Cost analysis' && oppNew.StageName == 'Offer negotiation'){
                oppNew.StageName.addError(Label.Opp_Stage_Offernegotiation_Error_without_Quote);
            }
        }
  	}
}

Wilfredo Morillo 20Wilfredo Morillo 20
This should be in a trigger not in a class.