+ Start a Discussion

Add product to opportunity when converting lead

Is there a way to add product to opportunity when converting a lead. I can add products to the leads but salesforce doesn't seam to allow mapping of the custom field to the Opportunity product.




You cannot do this by default.  One of the reasons is the design of the Products and Price Books.  Since every Opportunity has a Price Book, you have to select the Price Book prior to selecting the Products.  


You can do what you want, but it would require that you use Apex Code to do so.  There are two options:


1. Create a trigger on Opportunities.  The trigger would look at a custom field (Product), and if it is, attach a Price Book and the appropriate Product.  You could then map your product from the Lead into this custom field.  The Trigger would look something like:


trigger addPrioductFromLead on Opportunity (after insert) {
    Set<ID> oppProdId = new Set<ID>();                                   //Builds Product Ids to use in query
    List<Opportunity> oppList = new List<Opportunity>();                 //Builds list of Opportunities to update the Price Book ID field
    List<OpportunityLineItem> oliList = new List<OpportunityLineItem>(); //Set used to create Opportunity Products
    Map<ID, ID> pbeMap = new Map<ID, ID>();                              //Map used to match Products to PriceBookEntry records
    for(Opportunity o : Trigger.new) {
        if(o.Product__c != null) {                                       //Only use the Opportunity if it is created with a Product__c value (i.e. - On Convert)
            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 = '01s300000000MxU';                      //Set the Price Book of the Opportunity
    update oppList;                                                      //Update all of the Opportunities with teh Price Book
    for(PriceBookEntry pbe : [select Id, Product2Id from PriceBookEntry where PriceBook2Id = '01s300000000MxU' and Product2Id in :oppProdId]) { 
        pbeMap.put(pbe.Product2Id, pbe.id);                              //Build the map to use to match Procucts to PriceBookEntries
    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);
    if(oliList.size() > 0) {
        insert oliList;                                                  //Create the OpportunityLineItems


2. You can build a button, VF Page, and Apex class to rebuild the Convert function.  This will give you more control, but would be much more involved.


Hope this helps.



Hi  there.  What type of custom field should be created?