Advertisement
miCRoScoPiCeaRthLinG

SelectPriceBook Trigger

Oct 15th, 2013
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.41 KB | None | 0 0
  1. trigger SelectPriceBook on Opportunity ( before insert, before update ) {
  2.    
  3.     map<String,Id> mapPriceBooks = new map<String,Id>();
  4.    
  5.     for( PriceBook2 sPricebook : [SELECT Id, Name FROM PriceBook2] ) {
  6.         mapPriceBooks.put( sPricebook.Name, sPriceBook.Id );
  7.     }
  8.    
  9.     for( Opportunity opp : Trigger.new ) {
  10.         // Change Price Book
  11.        
  12.         // On create
  13.         if( trigger.isInsert ) {
  14.            
  15.             // New York
  16.             if( opp.Campus__c == 'NYC' )
  17.                 opp.Pricebook2Id = mapPriceBooks.get( PB_NYC ); // returns the correct Pricebook's ID
  18.  
  19.             // Atlanta
  20.             if( opp.Campus__c == 'ATL' )
  21.                 opp.Pricebook2Id = mapPriceBooks.get( PB_ATL ); // returns the correct Pricebook's ID
  22.            
  23.         }
  24.         else if ( trigger.isUpdate ) {  // On delete
  25.            
  26.             // Get old value
  27.             Opportunity beforeUpdate = Trigger.oldMap.get( opp.Id );
  28.            
  29.             // Check old value
  30.             if(opp.Campus__c != beforeUpdate.Campus__c) {
  31.                
  32.                 // New York
  33.                 if( opp.Campus__c == 'NYC' )
  34.                     opp.Pricebook2Id = mapPriceBooks.get( PB_NYC ); // returns the correct Pricebook's ID
  35.  
  36.                 // Atlanta
  37.                 if( opp.Campus__c == 'ATL' )
  38.                     opp.Pricebook2Id = mapPriceBooks.get( PB_ATL ); // returns the correct Pricebook's ID
  39.                        
  40.                
  41.             }
  42.         }
  43.              
  44.     }
  45.    
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement