You need to sign in to do that
Don't have an account?

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.
Thanks in advance.
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
http://salesforcenapex.wordpress.com/2013/05/18/how-to-insert-opportunity-and-opportunity-line-item-from-other-object/
If you have done so far so please post here so we can provide best solution. Very difficult to provide whole code.
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;
}
}
}
}