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
selvakumar Anbazhaganselvakumar Anbazhagan 

Need Apex Trigger for assign pricebook to a particular Opportunity record type

Hi everyone,

Can anyone please help to me create a trigger for the following condition.

I have a Pricebook called  "Bulk Price Book". I need to assign this pricebook for the opportunity only when a particular Opportunity Record Type (Bulk Services) is choosen. I need the coding . Since i don't have much knowledge about triggers and coding.

PriceBook  -  Id,Name('Bulk Price Book')
Opportunity - RecordTypeId,PricebookId

It will be very useful to me in future to buildup my own.

Thanks in advance.


Thanks,
Selva
Best Answer chosen by selvakumar Anbazhagan
sandeep sankhlasandeep sankhla
Hi Selva,

Please refer below trigger example and implement teh same for your need..
 
trigger OpportunityTrigger on Opportunity (before insert) {


Id pbID = null;
Id recordtypId = null;


 pbID = [Select Name, Description From Pricebook2 where Name =: 'Bulk Price Book '].Id;
 
recordtypId =  [Select SobjectType, Name, Id, DeveloperName From RecordType Where SobjectType = : 'Opportunity' And  Name =: 'Bulk Services'].Id;
	
    
   for(Opportunity obj : trigger.new)
   {
   		if(obj.RecordTypeId  == recordtypId)
   		{
   			obj.Pricebook2Id = pbID;
   		}
   }
    
}

Above trigger is on before event so whenever an opportunity is going to create if will check if recrdtype is bulk service then we will assign pricebookid to the id which we already queried..

1. Query the pricebookid where name is 'Bulk Price Book' and get the id of this pricebook..
2. get the recordtype Id of the recordtype where sobjct is opportunity and name is 'Bulk Service'

3. simply iterate the opportunity and then check if recrdtyp eid is same as our bulk service recordtype id then update the pricebook id with the id which we have from step 1..

Please refer above code and implement and let me know if you face nay issue..

P.S. If my answer helps you to solve your problem please mark it as best answer. It will help other to find best answer.

Thanks,
Sandeep
Salesforce Certified Developer 
'

All Answers

sandeep sankhlasandeep sankhla
Hi Selva,

Please refer below trigger example and implement teh same for your need..
 
trigger OpportunityTrigger on Opportunity (before insert) {


Id pbID = null;
Id recordtypId = null;


 pbID = [Select Name, Description From Pricebook2 where Name =: 'Bulk Price Book '].Id;
 
recordtypId =  [Select SobjectType, Name, Id, DeveloperName From RecordType Where SobjectType = : 'Opportunity' And  Name =: 'Bulk Services'].Id;
	
    
   for(Opportunity obj : trigger.new)
   {
   		if(obj.RecordTypeId  == recordtypId)
   		{
   			obj.Pricebook2Id = pbID;
   		}
   }
    
}

Above trigger is on before event so whenever an opportunity is going to create if will check if recrdtype is bulk service then we will assign pricebookid to the id which we already queried..

1. Query the pricebookid where name is 'Bulk Price Book' and get the id of this pricebook..
2. get the recordtype Id of the recordtype where sobjct is opportunity and name is 'Bulk Service'

3. simply iterate the opportunity and then check if recrdtyp eid is same as our bulk service recordtype id then update the pricebook id with the id which we have from step 1..

Please refer above code and implement and let me know if you face nay issue..

P.S. If my answer helps you to solve your problem please mark it as best answer. It will help other to find best answer.

Thanks,
Sandeep
Salesforce Certified Developer 
'
This was selected as the best answer
selvakumar Anbazhaganselvakumar Anbazhagan
Thanks a lot Sandeep. Its working fine. I got my result.

Just checked that created opportunity under Bulk record type, and it was assinged to Bulk Pricebook.
selvakumar Anbazhaganselvakumar Anbazhagan
Hi Sandeep,

I need one more help. Now this trigger is working fine.

Now i need that,if the same opportunity which we have Bulk Recordtype & Bulk Pricebook) changing Record type to some other record type,then it should update the price book based on zip-code.

Actually,for assign pricebook based on zipcode trigger is already running. Can i use that trigger for this.
Can we able to link that trigger with us. I have tried to amend this in our bulk pricebook trigger. But i didn't get the result.

Existing PriceBook Trigger :
 
 if(Trigger.isInsert){
                List<Pricebook2> prcbooklist = [select id,name from pricebook2];
                 Map<String,Id> prcbookMap = new Map<String,Id>();
                 Map<Id,String> abbAccmap = 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);
                    }
                }
                               
                for(Opportunity OppIdsItr : Trigger.new){
                    accountIds.add(OppIdsItr.accountid);
                    
                }
                for(Account AccItr : [SELECT id,Region__c FROM Account WHERE Id IN:accountIds]){
                    abbAccmap.put(AccItr.id,AccItr.Region__c);
                }
            
                for(Opportunity OppItr : Trigger.new){
                    if(prcbookMap.size() > 0){    
                            
                        if (abbAccmap.get(OppItr.accountId) == 'Price Zip1'){
                            OppItr.Pricebook2Id = prcbookMap.get('Price Book - 1');
                        }
                        else if(abbAccmap.get(OppItr.accountId) == 'Price Zip2'){
                            OppItr.Pricebook2Id = prcbookMap.get('Price Book - 2');
                       }                      
                       
 Please help me,if you can do it.
 
 
 Thanks,
 Selva
Narendra A 12Narendra A 12
I am just adding this answer for other people who might be having a similar need.
The requirement is as follows:
When a new Opportunity is created, I want to automatically assign a Price Book based on the Record Type of the Opportunity.

While the question that's posted here is asking for a trigger to do this, we can as well accomplish it using the Process Builder.
Here are the steps:

(1) In Setup, go to Process Builder and click on "New" to create a new Process.
(2) Specify a process name for this new process and for the option "The process starts when" option choose "A record changes". Click Save.
(3) Click the "Add Object" just under start, and select the Opportunity object. For the "Start the process when", choose "Only when a record is created".
(4) Click on "Add Criteria".
(a) For the "Criteria for Executing Actions", select "Conditions are met"
(b) In the "Set Conditions", in the "Find a field" select the Record Type field.
(c) Set the operator as "Equals"
(d) In the Value field enter the name of the Record Type
(e) Set the "Conditions" radio-button to "All the conditions are met"
(f) Click Save

(5) In the process builder workflow diagram, click on "Immediate Actions" that's to the right of the criteria you just added. In the window that opens, do the following:
(a) Select the Action Type as "Update Records"
(b) Specify a name for the action
(c) Click in the Record Type text box, and in the pop-up, select the option "Select the Opportunity record that started your process" and click "Choose".
(d) In the "Criteria for Updating Records" , select the "No criteria—just update the records!" option.
(e) In the "Set new field values for the records you update" , do the following: 
Choose the field as "Price Book ID". Set the type as "ID". In the Value field, you need to enter the Record ID of the Price Book that you want to be associated with this Opportunity. You can find the Record Id of the Price Book by clicking on the Price Book and looking at the URL of the page in the browser. It will be something like https://na5.salesforce.com/13s2G00000I5xyz. The Record Id for this is "13s2G00000I5xyz".
(f) Click Save.

(6) If you have more record types for which you want to set up this logic, you need to repeat steps (4) and (5) above for the additional record types.