You need to sign in to do that
Don't have an account?
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(){ 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; } }
{
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;
}
}
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.
Can you please post whole code along with trigger?