function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
selva kumar 14selva kumar 14 

Trigger issue regarding pricebook

Hi Everyone,

I have a  issue in my trigger functionality.

My Requirement is given below.

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?

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
Best Answer chosen by selva kumar 14
Pankaj_GanwaniPankaj_Ganwani
Hi,

Please use below mentioned code. Since you are performing updation on the same object so you should use before insert and before update instead.

trigger BulkPricebookOpp on Opportunity (before insert,before update)
{
          if(Trigger.isBefore && (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>();
                
                if(!prcbooklists.isEmpty())
                {
                    for(Pricebook2 prcIter : prcbooklists)
                    {
                        prcbookMaps.put(prcIter.name,prcIter.id);
                    }
                }
                
                              
              for(RecordType rectyp : recordtypId)
              {
                    recMap.put(rectyp.Id,rectyp.name);
              }       
                 
                            
                for(Opportunity OppItrs : Trigger.new)
                {
                    if(prcbookMaps.size() > 0) 
                    {                                
                        if(Trigger.isInsert)
                        {
                            if (recMap.get(OppItrs.RecordTypeId) == 'k) Bulk Services')
                            {
                                OppItrs.Pricebook2Id = prcbookMaps.get('Bulk Price Book');
                            }
                        }
                        else if(Trigger.isUpdate)
                        {
                             if(OppItrs.recordtypId!=Trigger.oldMap.get(OppItrs.Id).RecordTypeId)
                             {
                                if(recMap.get(OppItrs.RecordTypeId) == 'k) Bulk Services')
                                    OppItrs.Pricebook2Id = prcbookMaps.get('Bulk Price Book');
                                else
                                    OppItrs.Pricebook2Id = prcbookMaps.get('Non Bulk Pricebook');
                             }
                        }
                    }    
                }
        }
}

All Answers

Pankaj_GanwaniPankaj_Ganwani
Hi,

Please use below mentioned code. Since you are performing updation on the same object so you should use before insert and before update instead.

trigger BulkPricebookOpp on Opportunity (before insert,before update)
{
          if(Trigger.isBefore && (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>();
                
                if(!prcbooklists.isEmpty())
                {
                    for(Pricebook2 prcIter : prcbooklists)
                    {
                        prcbookMaps.put(prcIter.name,prcIter.id);
                    }
                }
                
                              
              for(RecordType rectyp : recordtypId)
              {
                    recMap.put(rectyp.Id,rectyp.name);
              }       
                 
                            
                for(Opportunity OppItrs : Trigger.new)
                {
                    if(prcbookMaps.size() > 0) 
                    {                                
                        if(Trigger.isInsert)
                        {
                            if (recMap.get(OppItrs.RecordTypeId) == 'k) Bulk Services')
                            {
                                OppItrs.Pricebook2Id = prcbookMaps.get('Bulk Price Book');
                            }
                        }
                        else if(Trigger.isUpdate)
                        {
                             if(OppItrs.recordtypId!=Trigger.oldMap.get(OppItrs.Id).RecordTypeId)
                             {
                                if(recMap.get(OppItrs.RecordTypeId) == 'k) Bulk Services')
                                    OppItrs.Pricebook2Id = prcbookMaps.get('Bulk Price Book');
                                else
                                    OppItrs.Pricebook2Id = prcbookMaps.get('Non Bulk Pricebook');
                             }
                        }
                    }    
                }
        }
}
This was selected as the best answer
selva kumar 14selva kumar 14
Hi  Pankaj,

Thanks a lot. It is working fine.

Regards,
Selva
selva kumar 14selva kumar 14
Hi Pankaj,

I faced one more issue in this.

In my sandbox,this trigger is working fine my above functionality issue.

When i deployed in production, am facing the same issue.

Is it happen? Since it is working in sandbox but not in production.

Do you know the reason? please help on this.

Thanks in advance.
Pankaj_GanwaniPankaj_Ganwani
Hi,

Can you please make sure all the essential record types and price books exist in Production?
selva kumar 14selva kumar 14
Yes. Recently only i have refresh my sandbox. Suppose, if i have issues in record types/price book in production other functionality also should return error.But only thing when i change bulk pricebook to some other pricebook it doesn't change.

But it is working propely in sandbox.
Pankaj_GanwaniPankaj_Ganwani
Are you sure you have deployed the same code as I gave you or you did any modifications in there before deployment? Please check once more in Production.
selva kumar 14selva kumar 14
Yes i executed the same logic in production.

My trigger is given below.

trigger BulkPricebookOpp on Opportunity (Before insert, Before update) {
if(Trigger.isBefore && (Trigger.isInsert || Trigger.isUpdate))
            {
                 List<Pricebook2> prcbooklist = [select id,name from pricebook2];
                 Map<String,Id> prcbookMap = new Map<String,Id>();
                 Map<Id,String> abbAccmap = new Map<Id,String>();
                 List<RecordType> recordtypId =  [Select Id,Name From RecordType Where SobjectType = 'Opportunity'];
                 Map<Id,String> recMap = new Map<Id,String>();
                 Set<Opportunity> OppItrortunityUpdateIds = new Set<Opportunity>();
                 Set<Id> accountIds = new Set<Id>();
                 Set<Id> RectypIds = new Set<Id>();
                              
                if(!prcbooklist.isEmpty())
                {
                    for(Pricebook2 prcIter : prcbooklist)
                    {
                        prcbookMap.put(prcIter.name,prcIter.id);
                    }
                }
                System.debug('==prcmap'+prcbookMap);
                
                for(RecordType rectyp : recordtypId)
                {
                    recMap.put(rectyp.Id,rectyp.name);
                }
                
                for(Opportunity OppIdsItr : Trigger.new)
                {
                    accountIds.add(OppIdsItr.accountid);
                    
                }
                for(Account AccItr : [SELECT id,ABB_Region__c FROM Account WHERE Id IN:accountIds])
                {
                    abbAccmap.put(AccItr.id,AccItr.ABB_Region__c);
                }
            
                for(Opportunity OppItr : Trigger.new)
                {
                    if(prcbookMap.size() > 0)
                    {
                         
                        if (recMap.get(OppItr.RecordTypeId) == 'k) Bulk Services')
                        {
                            OppItr.Pricebook2Id = prcbookMap.get('Bulk Price Book');
                        }
                        else if(Trigger.isUpdate)
                        {
                          if(OppItr.RecordTypeId!=Trigger.oldMap.get(OppItr.Id).RecordTypeId)
                            {
                            if(recMap.get(OppItr.RecordTypeId) == 'k) Bulk Services')
                            OppItr.Pricebook2Id = prcbookMap.get('Bulk Price Book');
                            else if(abbAccmap.get(OppItr.accountId) == 'A - D2')

                            {
                             OppItr.Pricebook2Id = prcbookMap.get('A Price Book - D2');
                            }
                            else if(abbAccmap.get(OppItr.accountId) == 'W-D2'){
                            OppItr.Pricebook2Id=prcbookMap.get('W Price Book - D2');
                            }
                            else if(abbAccmap.get(OppItr.accountId) == 'W-D3'){                             
                            OppItr.Pricebook2Id=prcbookMap.get('W Price Book - D3');
                            }
                                                   
                      
                          }
                      System.debug('====assign'+OppItr.Pricebook2Id);
                
                        }
              }
              }

            }
}

Can u look into it.
selva kumar 14selva kumar 14
Hi,

Now it is working as expected. Just i have created an other opportunity and followed as it is given result.

Thanks so much for your effort.