1

I have the following trigger:

trigger UpdateOpportunity on Opportunity (before insert, before update){    

       Opportunity[] oppQuery = [SELECT ID, RecordType.Name, StageName, Win_Reason__c, Unsolicited_Bid__c, Competitor_s__c FROM Opportunity WHERE ID IN :Trigger.new];  

       for(Opportunity opp : oppQuery){

          if(opp.RecordType.Name == 'SGI Opportunity' && opp.StageName != 'Lead 0%'){

              System.debug('THIS WORKED1');

                 if(opp.Win_Reason__c == 'No Competition/Sole Source'){

                    opp.Competitor_s__c = 'No Competitor/Sole Source'; 

                    System.debug('THIS WORKED2');
        }

                   if(opp.Unsolicited_Bid__c == 'Yes'){

                      opp.Competitor_s__c = 'No Competitor/Sole Source';

                    System.debug('THIS WORKED3');
            } 

         }
    }
}

To explain the trigger, I want it to only run when the RecordName is 'SGI Opportunity' and if StageName doesn't equal 'Lead 0%' If that is the case then if Win Reason = Yes then then opp.Competitor_s__c = 'No Competitor/Sole Source' and/or if Unsolicited Bid = 'Yes' then Competitor_s__c = 'No Competitor/Sole Source'. I've set up my user to make debug logs and I'm seeing my System.debug comments, but it's not automating the selection of the multi select Competitor picklist for me. I'm not certain at why my code isn't working correctly..

Thanks in advance for your time!

  • If you print 'oppQuery' out to the debug log at line 4, is it being populated correctly? – Davin Casey Sep 8 '14 at 19:25
  • Hi Davin, thank you so much for your response, and excuse my ignorance on what you mentioned to do, but what would be the syntax to print my oppQuery for the debug log? – user3708392 Sep 8 '14 at 19:31
1

The problem is that you are updating the records in oppQuery rather than the ones in Trigger.new (which is what you want to do in a before trigger.

Try this:

trigger UpdateOpportunity on Opportunity (before insert, before update)
{
     Map<Id, Opportunity> recordTypeMap = new Map<Id, Opportunity>([SELECT RecordType.Name FROM Opportunity WHERE ID IN :Trigger.new]);  

     for(Opportunity opp : Trigger.new)
     {
          if(recordTypeMap.get(opp.Id).RecordType.Name == 'SGI Opportunity' && opp.StageName != 'Lead 0%')
         {
              if(opp.Win_Reason__c == 'No Competition/Sole Source')
              {
                   opp.Competitor_s__c = 'No Competitor/Sole Source';
               }

               if(opp.Unsolicited_Bid__c == 'Yes')
               {
                   opp.Competitor_s__c = 'No Competitor/Sole Source';
                } 
          }
    }
}
  • Thank you so much Alex! I'm new to SalesForce, APEX, and SOQL queries and the code you provided works perfectly. Cheers mate! – user3708392 Sep 8 '14 at 19:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.