+ 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>();
                    for(Pricebook2 prcIter : prcbooklists){
                  for(RecordType rectyp : recordtypId){
                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)
                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>();
                    for(Pricebook2 prcIter : prcbooklist){
                for(Opportunity OppIdsItr : Trigger.new){
                for(Account AccItr : [SELECT id,Region__c FROM Account WHERE Id IN:accountIds]){
                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.