+ Start a Discussion
shoba shobashoba shoba 

How to calculate the holidays for an opportunity in salesforce

My scenario is to calculate the no of holidays for an opportunity. For this i wrote an apex class but its not working. Can anyone help me out to solve this issue. Am  new to salesforce
public class testforholidays
{
public list<opportunity> opp;
public list<holiday> hd;
public set<date> st = new set<date>();
public integer testforholidays(){
opp =[select id,Name,CreatedDate,Holidayslist__c,AccountId,Opportunity_Business_Unit__c,Region__c,
      LeadSource,Lead_Source_Detail__c, StageName,CloseDate,CurrencyIsoCode,Contact_Name__c,Type,
      Closed_Won_Reason__c 
      from opportunity limit 1];
    for(opportunity opt: opp){
    st.add(opt.CreatedDate.date());
    }
hd = [SELECT Name,ActivityDate FROM Holiday];
Integer ise = 0;
for(holiday h : hd){
 if((h.ActivityDate> (system.today())) && (h.ActivityDate<opt.st)  ){
 ise++;
 }
}
opp.Holidayslist__c=ise;
update opp;
return ise;

}

}
Nayana KNayana K
public class CalculateHoildays
{
    public Opportunitity objOpp;
    
    public CalculateHoildays()
    {
    }
        
    public Integer CalculateNumOfHoildays()
    {
        List<opportunity> lstOpp = new List<opportunity>();
        Integer intCount = 0;
        
        // This query will retrieve any one Opportunity Ascending by CreatedDate
        lstOpp =[select id,Name,CreatedDate,Holidayslist__c,AccountId,Opportunity_Business_Unit__c,Region__c,
              LeadSource,Lead_Source_Detail__c, StageName,CloseDate,CurrencyIsoCode,Contact_Name__c,Type,
              Closed_Won_Reason__c 
              from opportunity limit 1];
              
        // if List not empty then extract 0th index 
        if(!lstOpp.isEmpty())
        {
            Date objDate = lstOpp[0].CreatedDate.date();
            // There is no Standard object called Holiday in salesforce! Be sure to append __c
            Integer intCount = [SELECT COUNT() 
                                FROM Holiday
                                WHERE ActivityDate < TODAY 
                                    AND ActivityDate <: objDate
                                ];
            lstOpp[0].Holidayslist__c = intCount;
            update lstOpp[0];
        }
        return intCount;
    }
}
Nayana KNayana K
Sorry, Holiday is a standard object. I forgot to remove that comment. My bad. 

That error is coming because one validation rule has met its criteria. Check the criterias of validation rules on Opportunity. Check whether the opportunity returned by the soql query is meeting any of the criteria using system.debugs.
 
Nayana KNayana K
public class testforholidays
{     
    public void CalculateNumOfHoildays()
    {
        List<opportunity> lstOpp = new List<opportunity>();
		List<opportunity> lstOppToUpdate = new List<opportunity>();
		Map<Date,Integer> mapActivDateToCount = new Map<Date,Integer>();
		Set<Date> setDateOppCreated = new Set<Date>();
              
		for(Opportunity objOpp : [	select id,Name,CreatedDate,Holidayslist__c
									from opportunity 
									where IsClosed =False])
		{
			lstOpp.add(objOpp);
			setDateOppCreated.add(objOpp.CreatedDate.date());
			
		}
		
         for(Holiday objHoilday : [	SELECT Id, ActivityDate
									FROM Holiday
									WHERE ActivityDate < TODAY 
                                    AND ActivityDate <: setDateOppCreated
									])
		{
			Date dt = objHoilday.ActivityDate.date();
			if(mapActivDateToCount.containsKey(dt))
			{
				mapActivDateToCount.put(dt, mapActivDateToCount.get(dt)+1);
			}
			else
			{
				mapActivDateToCount.put(dt, 1);
			}
		}
        
		// I am not getting any way to avoid for inside for .
		for(Opportunity objOpp : lstOpp)
		{
			objOpp.Holidayslist__c[0] = 0;
			for(Date dtActivity : mapActivDateToCount.keySet())
			{
				if(dtActivity < objOpp.CreatedDate.date())
				{
					objOpp.Holidayslist__c[0] +=  mapActivDateToCount.get(dtActivity);
				}
			}
			lstOppToUpdate.add(objOpp);
		}
		
		system.debug('=====lstOppToUpdate===='+lstOppToUpdate);
		
		Database.update(lstOppToUpdate, true); 
    }
}

 
Nayana KNayana K
Are you running this from trigger? I mean you are using this class as a handler for trigger? 

Can you please post whole code along with trigger?