• YoniBer
  • NEWBIE
  • 0 Points
  • Member since 2013

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 1
    Replies

I'm new to Apex and have been mulling over the best way to do this:

 

I have a lookup field to the product object on the lead object.  When entering a lead the user selects the product they are interested in.

 

When the lead is qualified and converted, I want to not only create an opportunity (straightforward Salesforce) but I want to match the value in the lookup field on the lead object to the product object and insert this as the product (opportunity line item?) associated with the opportunity. I also want to be able to recognize the pricebook that this product belongs and insert the price into the 'Sales price' field.

 

Should I use the lead object in this? Should I create the trigger on the opportunity object? Suggestions will be greatly appreciated.

 

I'm trying to start by adapting this code I found.

 

trigger addProductFromLead on Opportunity (after insert) {

 

//Build Product Ids to use in query
Set<ID> oppProdId = new Set<ID>();

//Build list of Opportunities to update the Price Book ID field
List<Opportunity> oppList = new List<Opportunity>();

//Set used to create Opportunity Products
List<OpportunityLineItem> oliList = new List<OpportunityLineItem>();

//Map used to match Products to PriceBookEntry records
Map<ID, ID> pbeMap = new Map<ID, ID>();


for(Opportunity o : Trigger.new) {

//Only use the Opportunity if it is created with a Product__c value (i.e. - On Convert) if(o.AnticipatedCourse__c != null) {

oppProdId.add(otemp.Product__c);
//Add the product id to the set
Opportunity otemp = o.clone();
//Create a duplicate Opportunity to use in an update
otemp.PriceBook2Id = '01sd0000000Q1YD'; // I'd like to be able to dynamically recognize the correct price book
//Set the Price Book of the Opportunity
oppList.add(otemp); //seems to be a problem with declaring this variable
}
}
//Update all of the Opportunities with the Price Book
update oppList;
for(PriceBookEntry pbe : [select Id, Product2Id from PriceBookEntry where PriceBook2Id =
'01sd0000000Q1YD' and Product2Id in :oppProdId]) {
//Build the map to use to match Procucts to PriceBookEntries
pbeMap.put(pbe.Product2Id, pbe.id);
}
for(Opportunity opp : oppList) {
//Build the OpportunityLineItems for each Opportunity (sets the quantity to 1 and the
price to 100)
OpportunityLineItem oli = new OpportunityLineItem(OpportunityId = opp.id,
PriceBookEntryId = pbeMap.get(opp.Product__c), Quantity = 1, UnitPrice = 100);
oliList.add(oli);
}
if(oliList.size() > 0) {
//Create the OpportunityLineItems
insert oliList;
}

}

I'm new to Apex and have been mulling over the best way to do this:

 

I have a lookup field to the product object on the lead object.  When entering a lead the user selects the product they are interested in.

 

When the lead is qualified and converted, I want to not only create an opportunity (straightforward Salesforce) but I want to match the value in the lookup field on the lead object to the product object and insert this as the product (opportunity line item?) associated with the opportunity. I also want to be able to recognize the pricebook that this product belongs and insert the price into the 'Sales price' field.

 

Should I use the lead object in this? Should I create the trigger on the opportunity object? Suggestions will be greatly appreciated.

 

I'm trying to start by adapting this code I found.

 

trigger addProductFromLead on Opportunity (after insert) {

 

//Build Product Ids to use in query
Set<ID> oppProdId = new Set<ID>();

//Build list of Opportunities to update the Price Book ID field
List<Opportunity> oppList = new List<Opportunity>();

//Set used to create Opportunity Products
List<OpportunityLineItem> oliList = new List<OpportunityLineItem>();

//Map used to match Products to PriceBookEntry records
Map<ID, ID> pbeMap = new Map<ID, ID>();


for(Opportunity o : Trigger.new) {

//Only use the Opportunity if it is created with a Product__c value (i.e. - On Convert) if(o.AnticipatedCourse__c != null) {

oppProdId.add(otemp.Product__c);
//Add the product id to the set
Opportunity otemp = o.clone();
//Create a duplicate Opportunity to use in an update
otemp.PriceBook2Id = '01sd0000000Q1YD'; // I'd like to be able to dynamically recognize the correct price book
//Set the Price Book of the Opportunity
oppList.add(otemp); //seems to be a problem with declaring this variable
}
}
//Update all of the Opportunities with the Price Book
update oppList;
for(PriceBookEntry pbe : [select Id, Product2Id from PriceBookEntry where PriceBook2Id =
'01sd0000000Q1YD' and Product2Id in :oppProdId]) {
//Build the map to use to match Procucts to PriceBookEntries
pbeMap.put(pbe.Product2Id, pbe.id);
}
for(Opportunity opp : oppList) {
//Build the OpportunityLineItems for each Opportunity (sets the quantity to 1 and the
price to 100)
OpportunityLineItem oli = new OpportunityLineItem(OpportunityId = opp.id,
PriceBookEntryId = pbeMap.get(opp.Product__c), Quantity = 1, UnitPrice = 100);
oliList.add(oli);
}
if(oliList.size() > 0) {
//Create the OpportunityLineItems
insert oliList;
}

}