shoba 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(){
Closed_Won_Reason__c
from opportunity limit 1];
for(opportunity opt: opp){
}
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 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
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 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 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])
{

}

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);
}
}