function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
ckellieckellie 

Trigger Not Bulkified

I have tried to bulkify this trigger many times, but have failed as when I mass update my opportunities, most of my opportunity probability fields have been updated with the wrong values. How do I solve this problem and bulkify the trigger?

 

Trigger:

 

trigger ForecastOverride on Opportunity (Before Update) {

    Set<Id> bIds = new Set<Id>();
    user u;
    for(Opportunity op : trigger.new){
    System.debug('**** 0 op id : '+op.ForecastCategoryName);
    bids.add(op.id);
   }     
    
    if(StaticClass.doNotExecute ==true)
{
    system.debug('Inserting'+StaticClass.doNotExecute);
    for(Opportunity o:trigger.new)
    {
     bIds.add(o.id);
    Decimal m = 10;
    Decimal l = 0;
    Decimal t = 25;
    Decimal bc = 75;
    Decimal c = 90;
    Decimal p = 50;
    Decimal w = 100;
    
    if(trigger.new[0].Opp_Owner__c != trigger.new[0].Current_User__c){

    if (trigger.new[0].Forecast_Category_Override__c != trigger.old[0].Forecast_Category_Override__c)
     {
         o.ForecastCategoryName = o.Forecast_Category_Override__c;
        
        if (trigger.new[0].Forecast_Category_Override__c == 'Best Case') {
        o.Probability = bc;
        }
        if (trigger.new[0].Forecast_Category_Override__c == 'Commit') {
        o.Probability = c;
        }
        if (trigger.new[0].Forecast_Category_Override__c == 'Omitted') {
        o.Probability = m;
        }
        if (trigger.new[0].Forecast_Category_Override__c == 'Pipeline') {
        o.Probability = p;
        }  
          }   
   
    if (trigger.new[0].StageName == 'Closed Won')
     {
        o.probability = w;
        o.ForecastCategoryName = 'Closed';
        o.Forecast_Category_Override__c = 'Closed';
        
        }   
    if (trigger.new[0].StageName == 'Prospecting')
     {
        o.probability = m;
        o.ForecastCategoryName = 'Omitted';
        o.Forecast_Category_Override__c = 'Omitted';
        
        }
    if (trigger.new[0].StageName == 'Qualifying')
     {
        o.probability = m;
        o.ForecastCategoryName = 'Omitted';
        o.Forecast_Category_Override__c = 'Omitted';

       } 
    if (trigger.new[0].StageName == 'Proposal')
     {
        o.probability = t;
        o.ForecastCategoryName = 'Pipeline';
        o.Forecast_Category_Override__c = 'Pipeline';

       } 
    if (trigger.new[0].StageName == 'Proposal Delivered' && trigger.new[0].ForecastCategoryname == 'Pipeline')
     {
        o.probability = p;
        o.ForecastCategoryName = 'Pipeline';
        o.Forecast_Category_Override__c = 'Pipeline';

       } 
    if (trigger.new[0].StageName == 'Proposal Delivered' && trigger.new[0].ForecastCategoryname == 'Best Case')
     {
        o.probability = bc;
        o.ForecastCategoryName = 'Best Case';
        o.Forecast_Category_Override__c = 'Best Case';

       }
    if (trigger.new[0].StageName == 'Proposal Delivered' && trigger.new[0].ForecastCategoryname == 'Omitted')
     {
        o.probability = m;
        o.ForecastCategoryName = 'Omitted';
        o.Forecast_Category_Override__c = 'Omitted';

       }        
   if (trigger.new[0].StageName == 'Negotiation' && trigger.new[0].ForecastCategoryname == 'Best Case')
     {
        o.probability = bc;
        o.ForecastCategoryName = 'Best Case';
        o.Forecast_Category_Override__c = 'Best Case';

       }  
  if (trigger.new[0].StageName == 'Negotiation' && trigger.new[0].ForecastCategoryname == 'Commit')
     {
        o.probability = c;
        o.ForecastCategoryName = 'Commit';
        o.Forecast_Category_Override__c = 'Commit';

       }  
  if (trigger.new[0].StageName == 'Negotiation' && trigger.new[0].ForecastCategoryname == 'Omitted')
     {
        o.probability = m;
        o.ForecastCategoryName = 'Omitted';
        o.Forecast_Category_Override__c = 'Omitted';

       }   
   if (trigger.new[0].StageName == 'Closed Lost')
     {
        o.probability = l;
        o.ForecastCategoryName = 'Omitted';
        o.Forecast_Category_Override__c = 'Omitted';

       }
   }
  }

}
}
Best Answer chosen by Admin (Salesforce Developers) 
Damien_Damien_
for(Opportunity o:trigger.new)
{
    //I'll assume this is your first time coding.  When you are looping over an item in a for loop, its format is: for (ObjectName variableName: collectionName)
    //Basically you are looping through the entire collection and assigning each item in the collection to a variable to do work on
    if(o.Opp_Owner__c != UserInfo.getUserId()){

 

All Answers

Damien_Damien_

You are looping through all of the Opportunities.... but you specifically keep accessing the first one.

 

for(Opportunity o:trigger.new)
{
    //You are looping through... but instead accessing the first one instead of current.
    if(trigger.new[0].Opp_Owner__c != trigger.new[0].Current_User__c){

 

ckellieckellie

How do I change this line of code while still the evaluating whether the current user is the opportunity owner? I am thinking I might need to use a for loop but am not sure.

 

Thank you

Damien_Damien_
for(Opportunity o:trigger.new)
{
    //I'll assume this is your first time coding.  When you are looping over an item in a for loop, its format is: for (ObjectName variableName: collectionName)
    //Basically you are looping through the entire collection and assigning each item in the collection to a variable to do work on
    if(o.Opp_Owner__c != UserInfo.getUserId()){

 

This was selected as the best answer
ckellieckellie

Thank you very much for your assistance.