You need to sign in to do that
Don't have an account?

code bulkify
Please suggest me on how to bulkify this code- i get too many soql errors frequently.
trigger NewUpdateMonthValueOpp1 on Opportunity (before update) {
Integer FiscalYearStartMonth = [select FiscalYearStartMonth from Organization where id=:Userinfo.getOrganizationId()].FiscalYearStartMonth;
Double Amount4=0;
Double Amount6=0;
Double Amount7=0;
Double Amount8=0;
for(Opportunity op:Trigger.New)
{
List<Revenue_Schedule__c> revenueList = new List<Revenue_Schedule__c>();
revenueList =[select id,Total_Value_by_Forecast__c,Date__c from Revenue_Schedule__c
where Date__c = THIS_FISCAL_YEAR and Opportunity_Name__c=:op.id ];
List<Revenue_Schedule__c> revenueListFY = new List<Revenue_Schedule__c>();
revenueListFY =[select id,Total_Value_by_Forecast__c,Date__c from Revenue_Schedule__c
where Date__c = NEXT_FISCAL_YEAR and Opportunity_Name__c=:op.id ];
List<Revenue_Schedule__c> revenueListFULL = new List<Revenue_Schedule__c>();
revenueListFULL =[select id,Total_Value_by_Forecast__c,Date__c from Revenue_Schedule__c
where Opportunity_Name__c=:op.id ];
for(Integer k=0;k<revenueList.size();k++)
{
if(revenueList[k].Total_Value_by_Forecast__c!=null)
{
Amount4=Amount4+revenueList[k].Total_Value_by_Forecast__c;
}
}
for(Integer l=0;l<revenueListFULL.size();l++)
{
if(revenueListFULL[l].Total_Value_by_Forecast__c!=null)
{
Amount7=Amount7+revenueListFULL[l].Total_Value_by_Forecast__c;
}
Amount8=Amount7+op.Setup_Fee__c;
}
for(Integer m=0;m<revenueListFY .size();m++)
{
if(revenueListFY [m].Total_Value_by_Forecast__c!=null)
{
Amount6=Amount6+revenueListFY [m].Total_Value_by_Forecast__c;
}
}
op.Current_Year_Fee_Value1__c=Amount4;
op.Next_FY_Fee_Value__c=Amount6;
op.Amount=Amount8;
}
}
trigger NewUpdateMonthValueOpp1 on Opportunity (before update) {
Integer FiscalYearStartMonth = [select FiscalYearStartMonth from Organization where id=:Userinfo.getOrganizationId()].FiscalYearStartMonth;
Double Amount4=0;
Double Amount6=0;
Double Amount7=0;
Double Amount8=0;
for(Opportunity op:Trigger.New)
{
List<Revenue_Schedule__c> revenueList = new List<Revenue_Schedule__c>();
revenueList =[select id,Total_Value_by_Forecast__c,Date__c from Revenue_Schedule__c
where Date__c = THIS_FISCAL_YEAR and Opportunity_Name__c=:op.id ];
List<Revenue_Schedule__c> revenueListFY = new List<Revenue_Schedule__c>();
revenueListFY =[select id,Total_Value_by_Forecast__c,Date__c from Revenue_Schedule__c
where Date__c = NEXT_FISCAL_YEAR and Opportunity_Name__c=:op.id ];
List<Revenue_Schedule__c> revenueListFULL = new List<Revenue_Schedule__c>();
revenueListFULL =[select id,Total_Value_by_Forecast__c,Date__c from Revenue_Schedule__c
where Opportunity_Name__c=:op.id ];
for(Integer k=0;k<revenueList.size();k++)
{
if(revenueList[k].Total_Value_by_Forecast__c!=null)
{
Amount4=Amount4+revenueList[k].Total_Value_by_Forecast__c;
}
}
for(Integer l=0;l<revenueListFULL.size();l++)
{
if(revenueListFULL[l].Total_Value_by_Forecast__c!=null)
{
Amount7=Amount7+revenueListFULL[l].Total_Value_by_Forecast__c;
}
Amount8=Amount7+op.Setup_Fee__c;
}
for(Integer m=0;m<revenueListFY .size();m++)
{
if(revenueListFY [m].Total_Value_by_Forecast__c!=null)
{
Amount6=Amount6+revenueListFY [m].Total_Value_by_Forecast__c;
}
}
op.Current_Year_Fee_Value1__c=Amount4;
op.Next_FY_Fee_Value__c=Amount6;
op.Amount=Amount8;
}
}
Try this code.
trigger NewUpdateMonthValueOpp1 on Opportunity (before update) {
map<String,List<Revenue_Schedule__c>> revenueListMap = new map<String,List<Revenue_Schedule__c>>();
map<String,List<Revenue_Schedule__c>> revenueListFULLMap = new map<String,List<Revenue_Schedule__c>>();
map<String,List<Revenue_Schedule__c>> revenueListFYMap = new map<String,List<Revenue_Schedule__c>>();
for(Revenue_Schedule__c rs : [select select id,Total_Value_by_Forecast__c,Date__c from Revenue_Schedule__c where Opportunity_Name__c In :Trigger.New]){
if(rs.Date__c = NEXT_FISCAL_YEAR){
if(!revenueListFYMap.containsKey(rs.Opportunity_Name__c))
revenueListFYMap.put(rs.Opportunity_Name__c,new list<Revenue_Schedule__c>());
revenueListFYMap.get(rs.Opportunity_Name__c).add(rs);
}
if(rs.Date__c = THIS_FISCAL_YEAR){
if(!revenueListMap.containsKey(rs.Opportunity_Name__c))
revenueListMap.put(rs.Opportunity_Name__c,new list<Revenue_Schedule__c>());
revenueListMap.get(rs.Opportunity_Name__c).add(rs);
}
if(!revenueListFULLMap.containsKey(rs.Opportunity_Name__c))
revenueListFULLMap.put(rs.Opportunity_Name__c,new list<Revenue_Schedule__c>());
revenueListFULLMap.get(rs.Opportunity_Name__c).add(rs);
}
Integer FiscalYearStartMonth = [select FiscalYearStartMonth from Organization where id=:Userinfo.getOrganizationId()].FiscalYearStartMonth;
Double Amount4=0;
Double Amount6=0;
Double Amount7=0;
Double Amount8=0;
for(Opportunity op:Trigger.New)
{
if(revenueListMap.containsKey(op.Id)){
for(Integer k=0;k<revenueListMap[op.Id].size();k++)
{
if(revenueListMap[op.Id][k].Total_Value_by_Forecast__c!=null)
{
Amount4=Amount4+revenueListMap[op.Id][k].Total_Value_by_Forecast__c;
}
}
}
if(revenueListFULLMap.containsKey(op.Id)){
for(Integer l=0;l<revenueListFULLMap.get(op.Id).size();l++)
{
if(revenueListFULLMap.get(op.Id)[l].Total_Value_by_Forecast__c!=null)
{
Amount7=Amount7+revenueListFULLMap.get(op.Id)[l].Total_Value_by_Forecast__c;
}
Amount8=Amount7+op.Setup_Fee__c;
}
}
if(revenueListFYMap.containsKey(op.Id)){
for(Integer m=0;m<revenueListFYMap.get(op.Id) .size();m++)
{
if(revenueListFYMap.get(op.Id)[m].Total_Value_by_Forecast__c!=null)
{
Amount6=Amount6+revenueListFYMap.get(op.Id)[m].Total_Value_by_Forecast__c;
}
}
}
op.Current_Year_Fee_Value1__c=Amount4;
op.Next_FY_Fee_Value__c=Amount6;
op.Amount=Amount8;
}
}
Thanks
Nitin
Here is the complete code, it may help others
trigger AmountContactRole on Opportunity (before update) {
Integer FiscalYearStartMonth = [select FiscalYearStartMonth from Organization where id=:Userinfo.getOrganizationId()].FiscalYearStartMonth;
map<ID, double> mapamout = new map<ID, double>();
map<ID, Integer> mapcontact6 = new map<ID, Integer>();
//this is required for multi-currency org as i am using aggregate function. THe aggregate function will return the currency value in corporate current and
//i then use the conversion rates from currency type table to convert to the opportunity currency.
Map<String, Double> MapCurrency = new Map<string, Double>();
//querry the contact role object for all incoming opp id's and take the count of contacts.
for(AggregateResult cr : [Select count(ContactId) numRecs, OpportunityId OppId From OpportunityContactRole
Where OpportunityId in :trigger.newmap.keyset() group by OpportunityId ])
{
mapcontact6.put((ID)cr.get('OppId'), (Integer)cr.get('numRecs'));
}
//Query the database for incoming opp ids and aggregate the field Total Value by forecast - i am rolling up this value to update in standard ammount field
for(AggregateResult ar : [Select Sum(Total_Value_by_Forecast__c) numRecs, Opportunity_Name__c OppId From Revenue_Schedule__c
Where Opportunity_Name__c in :trigger.newmap.keyset() group by Opportunity_Name__c ])
{
mapamout.put((ID)ar.get('OppId'), (Double)ar.get('numRecs'));
}
//currencytype
for(currencytype c : [SELECT conversionrate, isocode FROM currencytype])
{
MapCurrency.put(c.isocode,c.Conversionrate);
}
for(opportunity opp : trigger.new)
{
string opportunityCurrency = opp.CurrencyIsoCode;
Double conversionRate = MapCurrency.get(opportunityCurrency);
//assign the sum value to amount multiplied by conversion rate
if(mapamout.get(opp.id) != null )
Opp.Amount = mapamout.get(opp.id)*conversionRate;
// assign count of contacts from contact role
if(mapcontact6.get(opp.id) != null)
Opp.Number_of_Contacts_Roles_Assigned__c = mapcontact6.get(opp.id);
}}