You need to sign in to do that
Don't have an account?
Wisdom to attach product and price to opportunity on conversion from lead
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;
}
}
Hi,
I understand that when a lead is created - you will be taking the value of Product in a field on Lead which you wish to add as opportunity line item once the lead is converted and an opportunity is created.
Going with the standard lead conversion approach - it will not be possible to create a opportunityline item AT the time of lead conversion but what we can do is the following:
1)Create a product field on Opportunity that can save the value of the Product field on Lead
2)Map this custom field to the lead field so the value gets saved at lead conversion
3)Create a trigger on OPPORTUNITY and create a lineitem with the product name macthing that of saved on the Product custom field on opportunity which we created on step1
Hope this helps!
All Answers
Hi,
I understand that when a lead is created - you will be taking the value of Product in a field on Lead which you wish to add as opportunity line item once the lead is converted and an opportunity is created.
Going with the standard lead conversion approach - it will not be possible to create a opportunityline item AT the time of lead conversion but what we can do is the following:
1)Create a product field on Opportunity that can save the value of the Product field on Lead
2)Map this custom field to the lead field so the value gets saved at lead conversion
3)Create a trigger on OPPORTUNITY and create a lineitem with the product name macthing that of saved on the Product custom field on opportunity which we created on step1
Hope this helps!
Thanks Sonam - that makes sense