+ Start a Discussion
selvakumar Anbazhaganselvakumar Anbazhagan 

Trigger Functionality Issue in Assign Pricebook

Hi Everyone,

I have a trigger that have the following functionality.

If a  specific recordtype ("Bulk Record Type" ) opportunity is created,then it should select “k) Bulk  PriceBook".

After Save,If i change Bulk to some other record type,then  it should be changed to particular pricebook(Non Bulk Pricebook) .
As well as if other Recordtype to Bulk Record type it should change to "Bulk Pricebook" .
So i have created trigger regarding this requirement.

It is working for that,
1)  Bulk Record Type Choosen --> Bulk Pricebook Assigned
2) Other Record Type to Bulk Change -->Bulk Pricebook Assigned

My only issue with this is,
If i Change Bulk Record type to Other Record Type Pricebook doesn't change.It remains in same Bulk Pricebook.

Is it cases related to where use insert,update in a if else statements? I have tried to debugging. I couldn't make a way.

Please any one help me in my coding to find out.

My trigger is given below.

trigger BulkPricebookOpp on Opportunity (before insert,before update)
{
          if(trigger.isInsert || trigger.isUpdate) {
                 List<Pricebook2> prcbooklists = [select id,name from pricebook2];
                 Map<String,Id> prcbookMaps = new Map<String,Id>();
                 List<RecordType> recordtypId =  [Select Id,Name From RecordType Where SobjectType = 'Opportunity'];
                 Map<Id,String> recMap = new Map<Id,String>();
                  Set<Opportunity> OppItrortunityUpdatesIds = new Set<Opportunity>();
                                               
                if(!prcbooklists.isEmpty()){
                    for(Pricebook2 prcIter : prcbooklists){
                        prcbookMaps.put(prcIter.name,prcIter.id);
                    }
                }
                System.debug('==prcmap'+prcbookMaps);
                
                              
                  for(RecordType rectyp : recordtypId){
                        recMap.put(rectyp.Id,rectyp.name);
                    }       
                 
                            
                for(Opportunity OppItrs : Trigger.new){
                    if(prcbookMaps.size() > 0) {    
                            
                        if (recMap.get(OppItrs.RecordTypeId) == 'k) Bulk Services'){
                            OppItrs.Pricebook2Id = prcbookMaps.get('Bulk Price Book');
                        }
                    }    
        
        else if(Trigger.isAfter)
        {
        if(Trigger.isUpdate)
        {
                List<Pricebook2> prcbooklist = [select id,name from pricebook2];
                 Map<String,Id> prcbookMap = new Map<String,Id>();
                 Map<Id,String> Accmap = new Map<Id,String>();
                 Set<Opportunity> OppItrortunityUpdateIds = new Set<Opportunity>();
                 Set<Id> accountIds = new Set<Id>();
                              
                if(!prcbooklist.isEmpty()){
                    for(Pricebook2 prcIter : prcbooklist){
                        prcbookMap.put(prcIter.name,prcIter.id);
                    }
                }
                System.debug('==prcmap'+prcbookMap);
                
                for(Opportunity OppIdsItr : Trigger.new){
                    accountIds.add(OppIdsItr.accountid);
                    
                }
                for(Account AccItr : [SELECT id,Region__c FROM Account WHERE Id IN:accountIds]){
                    Accmap.put(AccItr.id,AccItr.Region__c);
                }
            
                for(Opportunity OppItr : Trigger.new){
                    if(prcbookMap.size() > 0){    
                            
                        if (Accmap.get(OppItr.accountId) == 'Service 3'){
                            OppItr.Pricebook2Id = prcbookMap.get('Non Bulk Price Book - 3');
                        }
                        else if(Accmap.get(OppItr.accountId) == ' Service 2'){
                            OppItr.Pricebook2Id = prcbookMap.get('Non Bulk Price Book - 2');
                       }
                     }
                 }
             }
        }
    }
  }
}


Thanks in advance.

Regards,
Selva