+ Start a Discussion
Raghav TRaghav T 

How to create a trigger to count the open opportunity which not close won or close lost or cancelled?

Best Answer chosen by Raghav T
CharuDuttCharuDutt
Hii Raghav
Try  Below Trigger
trigger SumAmountOpp on Opportunity (After Insert,After Update,After Delete) {
 List<Account> accList=new List<Account>();

    Set<Id> setAccIds = new Set<Id>();
    if(Trigger.isInsert){
         if(trigger.isAfter){
        for(Opportunity con : Trigger.new){
            if(con.AccountId != null && && Con.StageName != 'Closed Won' && con.StageName != 'Closed Lost' && con.StageName != 'Cancelled'){
            setAccIds.add(con.AccountId);
            	}
			}
		}
    } 
    system.debug('setAccIds ==> '+setAccIds);
    if(Trigger.isUpdate){
         if(trigger.isAfter){
        for(Opportunity con : Trigger.new){ 
            if(con.AccountId!=Trigger.oldMap.get(con.Id).AccountId && Con.StageName != 'Closed Won' && con.StageName != 'Closed Lost' && con.StageName != 'Cancelled'){
               	setAccIds.add(con.AccountId);
                setAccIds.add(Trigger.oldMap.get(con.Id).AccountId);
            	}
            
			}        
        }
    }
    if(Trigger.isDelete){
        if(trigger.isAfter){
        for(Opportunity con : Trigger.old) { 
            if(con.AccountId != null && con.StageName != 'Closed Won' && con.StageName != 'Closed Lost' && con.StageName != 'Cancelled'){
            setAccIds.add(con.AccountId);
            	}
        	}
        }
    }    
    for(Account acc :[Select id,Total_opp__c ,(Select id,Name,Amount from Opportunities where StageName != 'Closed Won' AND StageName != 'Closed Lost' AND StageName != 'Cancelled') from Account where Id in : setAccIds]){
			
        acc.Total_opp__c = acc.Opportunities.size();
        acclist.add(acc);
        
    }
    if(acclist.size()>0){
        update accList;     
    }
    
}
Please Mark It As Best Answer If It Helps
Thank You! 

All Answers

CharuDuttCharuDutt
Hii Raghav
Try  Below Trigger
trigger SumAmountOpp on Opportunity (After Insert,After Update,After Delete) {
 List<Account> accList=new List<Account>();

    Set<Id> setAccIds = new Set<Id>();
    if(Trigger.isInsert){
         if(trigger.isAfter){
        for(Opportunity con : Trigger.new){
            if(con.AccountId != null && && Con.StageName != 'Closed Won' && con.StageName != 'Closed Lost' && con.StageName != 'Cancelled'){
            setAccIds.add(con.AccountId);
            	}
			}
		}
    } 
    system.debug('setAccIds ==> '+setAccIds);
    if(Trigger.isUpdate){
         if(trigger.isAfter){
        for(Opportunity con : Trigger.new){ 
            if(con.AccountId!=Trigger.oldMap.get(con.Id).AccountId && Con.StageName != 'Closed Won' && con.StageName != 'Closed Lost' && con.StageName != 'Cancelled'){
               	setAccIds.add(con.AccountId);
                setAccIds.add(Trigger.oldMap.get(con.Id).AccountId);
            	}
            
			}        
        }
    }
    if(Trigger.isDelete){
        if(trigger.isAfter){
        for(Opportunity con : Trigger.old) { 
            if(con.AccountId != null && con.StageName != 'Closed Won' && con.StageName != 'Closed Lost' && con.StageName != 'Cancelled'){
            setAccIds.add(con.AccountId);
            	}
        	}
        }
    }    
    for(Account acc :[Select id,Total_opp__c ,(Select id,Name,Amount from Opportunities where StageName != 'Closed Won' AND StageName != 'Closed Lost' AND StageName != 'Cancelled') from Account where Id in : setAccIds]){
			
        acc.Total_opp__c = acc.Opportunities.size();
        acclist.add(acc);
        
    }
    if(acclist.size()>0){
        update accList;     
    }
    
}
Please Mark It As Best Answer If It Helps
Thank You! 
This was selected as the best answer
CharuDuttCharuDutt
Please Close your Query By Marking  It As Best Answer If It Helps So It Also Helps Others In Future