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
jpbenjpben 

Opportunity Product Trigger

Anyone has a code to share that will allow me to add Opportunity products upon creation of the Opportunity.  I need to add 20 line items upon creation of the record to alleviate some of the manual data entry for our sales reps.  

Thanks in advance. 
Best Answer chosen by jpben
jpbenjpben
Heres the code that I developed.  Hopefully this may help others in the future. The code selects a pricebook based on the Opportunity Record Type.  Then all of the products (total of 18) on the pricebook are added to the Opportunity.  Our sales reps now only have to update quantities and prices for some of the products.  This is a huge time savings. 

trigger Technology_iKnowMedProducts on Opportunity (before insert, after insert) {
  //Make iKnowMed Pricebook the default when Opportunity Record Type is iKnowMed
    Pricebook2 ikmBook = [SELECT Id FROM Pricebook2 WHERE Id = '01sJ00000001AG1'];
    if(Trigger.isBefore) {
        for(Opportunity oppty: Trigger.new) {
            if(oppty.RecordTypeId=='012J00000004wnY'){
            oppty.Pricebook2Id = ikmBook.Id;
        }
        }
    }
    if(Trigger.isAfter) {
    //Add all of the products on the iKnowMed Pricebook
        
      List<OpportunityLineItem> opptyline = new List<OpportunityLineItem>();
        
        for(Opportunity record: Trigger.new) {
            if(record.RecordTypeId=='012J00000004wnY'){     
             
                for(PricebookEntry entry: [SELECT Id, UnitPrice FROM PricebookEntry WHERE Pricebook2Id = :ikmBook.Id]){
                
        OpportunityLineItem lines = new OpportunityLineItem();
                lines.PricebookEntryId=entry.Id;
                lines.Quantity=1;
                lines.OpportunityId=record.Id;
                lines.UnitPrice=entry.UnitPrice;
            
                opptyline.add(lines);
        }
        insert opptyline;
    }
    }
    }     
}

All Answers

RamuRamu (Salesforce Developers) 
The below post might give you some idea

http://salesforcenapex.wordpress.com/2013/05/18/how-to-insert-opportunity-and-opportunity-line-item-from-other-object/
AshlekhAshlekh
Hi,

If you have done so far so please post here so we can provide best solution. Very difficult to provide whole code.
jpbenjpben
Heres the code that I developed.  Hopefully this may help others in the future. The code selects a pricebook based on the Opportunity Record Type.  Then all of the products (total of 18) on the pricebook are added to the Opportunity.  Our sales reps now only have to update quantities and prices for some of the products.  This is a huge time savings. 

trigger Technology_iKnowMedProducts on Opportunity (before insert, after insert) {
  //Make iKnowMed Pricebook the default when Opportunity Record Type is iKnowMed
    Pricebook2 ikmBook = [SELECT Id FROM Pricebook2 WHERE Id = '01sJ00000001AG1'];
    if(Trigger.isBefore) {
        for(Opportunity oppty: Trigger.new) {
            if(oppty.RecordTypeId=='012J00000004wnY'){
            oppty.Pricebook2Id = ikmBook.Id;
        }
        }
    }
    if(Trigger.isAfter) {
    //Add all of the products on the iKnowMed Pricebook
        
      List<OpportunityLineItem> opptyline = new List<OpportunityLineItem>();
        
        for(Opportunity record: Trigger.new) {
            if(record.RecordTypeId=='012J00000004wnY'){     
             
                for(PricebookEntry entry: [SELECT Id, UnitPrice FROM PricebookEntry WHERE Pricebook2Id = :ikmBook.Id]){
                
        OpportunityLineItem lines = new OpportunityLineItem();
                lines.PricebookEntryId=entry.Id;
                lines.Quantity=1;
                lines.OpportunityId=record.Id;
                lines.UnitPrice=entry.UnitPrice;
            
                opptyline.add(lines);
        }
        insert opptyline;
    }
    }
    }     
}
This was selected as the best answer