+ Start a Discussion
Selvakumar Anbazhagan 7Selvakumar Anbazhagan 7 

Trigger for Pricebook Selection Dynamically based on Zipcode

Hi Everyone,
 
I would like to get any idea or help for my new task.

I have a requirement that Pricebook should get select based on the zipcode to the account. Is it way to approach through trigger or any functionality available in SF?

I have a field "Pricebookname" in “Zipcode” object. But this is not related to Pricebook object. Can We have make lookup relationship with Zipcode object? Through that how i can assign pricebook to the zipcode.
For ex, Zipcode 00001,00005,00025,00123 having pricebook name as Region-D and Zipcode 00002,04583,07894 having Region-C then these accounts should choose correct pricebook for their zipcodes.

As a part, in some cases i also need to choose some other pricebook rather than to particular pricebook. 

For example, Zipcode 12345 actual pricebook is Region-A but it should choose Region-Others.
 
 
It could be done through any functionality or Trigger? 
 
 
Thanks in advance.
 
Best Answer chosen by Selvakumar Anbazhagan 7
Carlos Campillo GallegoCarlos Campillo Gallego
Hi again Selva,

I've built it with process builder so you can see how easy it is (plus you don't have to write an apex test class for it ;)  ) :
User-added image
Open the image in a new tab to see it bigger :)
Regarding your code, please avoid using queries inside loops or you will reach SOQL limits without noticing it...

Regards

All Answers

Carlos Campillo GallegoCarlos Campillo Gallego
Hi Selvakumar,

Yes, you can create a lookup field in your Zipcode Object related to Price Book. This leaves you some questions, how are you going to fill this field, manually or automatically? if automatically, how? you will have to define some business logic for this. Is there any Zipcode which could have more than one Price Book? by creating a lookup field directly on your Zipcode object, you are forcing it to have just one Price Book (unless you create more lookup fields or change your data model).

Hope this helps you, regards.
Selvakumar Anbazhagan 7Selvakumar Anbazhagan 7
Hi Carlos,

Thanks for your response.
 
I can use direct lookup with pricebook object. And each zipcode can have only one pricebook. In some case I need to choose different pricebook that can deal later.
 
Here my scenario is that, when I am creating a new opportunity to the account it should automatically choose its own pricebook which belongs to the zipcode.Details are given below :
 
Zipcode table have a field Pricebook Name. And also Zipcode can directly linked with the Account.
 
Zipcode Object:
 
Zip : 12345
PriceBook_Name__c : Region – A
 
Account :
 
Name : AcctName123
Zip : 12345 (Lookup with Zipcode object)
 
When am Creating a new opportunity for this account “AcctName123”, Opportunity pricebook should automatically choose Pricebook as “Region – A”.

I believe this is possible only with trigger. How I can achieve this?
 
Any help on this it would be appreciate.
 
Thanks,
Selva
 
Carlos Campillo GallegoCarlos Campillo Gallego
Hi Selva,

Since you have the pricebook already related on your Opportunity's Account, you just have to take the value of that Account field and populate it on your opportunity. That is quite easy to do with just Process Builder an update records action. But you still have to populate Price Book field value on your Accounts.

Regards
Selvakumar Anbazhagan 7Selvakumar Anbazhagan 7
Hi,

I have tried something using Accounts object itself. 

I have referred pricebook to the "Zipcode" object using lookup. And also i have lookup relationship with Accounts and Zipcode. Then i have created a formula field "PriceBook__c" in Accounts as Zip_Code__r.PriceBookName__c

Through that i created a trigger as follows. But something i did missed in the logic. 

Trigger Sample: 

trigger Pricebookupdate on Opportunity (Before insert, Before update, After insert) 
    {
    if(trigger.isBefore){
        if(trigger.isInsert || trigger.isUpdate){
        for(Opportunity OppIdsItr : Trigger.new){
                    accountIds.add(OppIdsItr.accountid);
                }
                for(Account AccItr : [SELECT id,PriceBook__c FROM Account WHERE Id IN:accountIds]){
                    abbAccmap.put(AccItr.id,AccItr.PriceBook__c);
                }
            
                for(Opportunity OppItr : Trigger.new){
                    if(prcbookMap.size() > 0){    
                    
                        if(abbAccmap.get(OppItr.accountId) == AccItr.PriceBook__c){
                            OppItr.Pricebook2Id = prcbookMap.get('AccItr.PriceBook__c');
                        }
                        else if {
                        }
                                        
                      }
                                      }
                
            }
        }
    }


Any help on this to resolve this trigger?


Thanks!
Carlos Campillo GallegoCarlos Campillo Gallego
Hi again Selva,

I've built it with process builder so you can see how easy it is (plus you don't have to write an apex test class for it ;)  ) :
User-added image
Open the image in a new tab to see it bigger :)
Regarding your code, please avoid using queries inside loops or you will reach SOQL limits without noticing it...

Regards
This was selected as the best answer
Selvakumar Anbazhagan 7Selvakumar Anbazhagan 7
Hi Carlos,

I have tried it and it is working fine. Thanks a lot.

But in some case, i need to change Pricebook for particular record types even it assigns automaticallly. Can we add these into process builder or else condition?

Thanks,
Selva