Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- trigger SelectPriceBook on Opportunity ( before insert, before update ) {
- map<String,Id> mapPriceBooks = new map<String,Id>();
- for( PriceBook2 sPricebook : [SELECT Id, Name FROM PriceBook2] ) {
- mapPriceBooks.put( sPricebook.Name, sPriceBook.Id );
- }
- for( Opportunity opp : Trigger.new ) {
- // Change Price Book
- // On create
- if( trigger.isInsert ) {
- // New York
- if( opp.Campus__c == 'NYC' )
- opp.Pricebook2Id = mapPriceBooks.get( PB_NYC ); // returns the correct Pricebook's ID
- // Atlanta
- if( opp.Campus__c == 'ATL' )
- opp.Pricebook2Id = mapPriceBooks.get( PB_ATL ); // returns the correct Pricebook's ID
- }
- else if ( trigger.isUpdate ) { // On delete
- // Get old value
- Opportunity beforeUpdate = Trigger.oldMap.get( opp.Id );
- // Check old value
- if(opp.Campus__c != beforeUpdate.Campus__c) {
- // New York
- if( opp.Campus__c == 'NYC' )
- opp.Pricebook2Id = mapPriceBooks.get( PB_NYC ); // returns the correct Pricebook's ID
- // Atlanta
- if( opp.Campus__c == 'ATL' )
- opp.Pricebook2Id = mapPriceBooks.get( PB_ATL ); // returns the correct Pricebook's ID
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement