• San Diego Craig
  • NEWBIE
  • 0 Points
  • Member since 2011

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 0
    Replies
  • On the Lead object, I created a custom field called “Product_1__c”, which is a Lookup field to the Product object
  • I have successfully created a trigger that moves the Product from the Lead to the Opportunity during the Convert process

 

Problem:  In my Apex test class, how do I programmatically add the custom field “Product_1__c” to the Lead, and then invoke the Convert process?

 

Apex Unit Test code:

@isTest

private class testTrgLeadConvert {
    static testMethod void myUnitTest() {       

test.startTest();               

 

// Create the Lead object...

Lead lead = new Lead();       

lead.LastName = 'Test LastName';       

lead.Company = 'Test Company';       

insert lead;               

 

// make an update....

lead.Status = 'Qualified Lead';       

lead.Number_of_Reps__c = 10;       

lead.Industry = 'Banking';       

update lead;               

 

// test to ensure update worked...

Lead updatedLead = [SELECT Id, LastName, Company, Status, Industry FROM Lead Where Id = :lead.Id];       

System.assertEquals('Banking', updatedLead.Industry);               

 

// convert the Lead...

Database.Leadconvert leadConvert = new Database.Leadconvert();       

leadConvert.setLeadId(updatedLead.Id);       

leadConvert.setConvertedStatus('Qualified Lead');       

leadConvert.setDoNotCreateOpportunity(false);       

leadConvert.setSendNotificationEmail(false);               

Database.Leadconvertresult lcr = Database.convertLead(leadConvert);       

System.debug('leadConvert' + lcr.isSuccess());

 

Opportunity o = [SELECT Id, OwnerId FROM Opportunity WHERE Id=:lcr.getOpportunityId()];    

System.debug('opportunity ID:  ' + o.Id);        test.stopTest();   

}

}

 

Trigger:

trigger trgLeadConvert on Lead (after insert, after update) {
// products & opportunities   

set<Id> products = new Set<Id>();   

set<id> opportunities = new set<id>();             

 

// product to pricebook entries   

map<id,pricebookentry> pricebookentries = new map<id,pricebookentry>();             

 

// line items to add   

list<opportunitylineitem> lineitems = new list<opportunitylineitem>();             

 

// standard pricebook   

pricebook2 standardpricebook = [select id from pricebook2 where isstandard = true];
   

// get all of the Lead object's product and opportunity id values    

for(lead l : trigger.new) {       

products.add(l.Product_1__c);       

products.add(l.Product_2__c);       

products.add(l.Product_3__c);       

products.add(l.Product_4__c);       

opportunities.add(l.convertedopportunityid);   

}
   

// map product id to pricebook entry...  join the Lead's Product w/ the Pricebook Entry   

for(pricebookentry pbe:[select id, unitprice, product2id                            

  from pricebookentry                            

  where pricebook2.isstandard = true                              

     and product2id in :products]) {       

pricebookentries.put(pbe.product2id, pbe);   

}
   

// set pricebook entry value for opportunities   

for(opportunity[] opps:[select id from opportunity where id in :opportunities]) {       

for(opportunity opp:opps) {           

opp.pricebook2id = standardpricebook.id;       

}       

update opps;   

}
   

// create line items   

for(lead l:trigger.new) {       

if(l.convertedopportunityid == null || l.Product_1__c == null) continue;       

lineitems.add(new opportunitylineitem(quantity=1,totalprice=pricebookentries.get(l.Product_1__c).unitprice,opportunityid=l.convertedopportunityid,pricebookentryid=pricebookentries.get(l.Product_1__c).id));               if(l.Product_2__c == null) continue;       

lineitems.add(new opportunitylineitem(quantity=1,totalprice=pricebookentries.get(l.Product_2__c).unitprice,opportunityid=l.convertedopportunityid,pricebookentryid=pricebookentries.get(l.Product_2__c).id));
       if(l.Product_3__c == null) continue;       

lineitems.add(new opportunitylineitem(quantity=1,totalprice=pricebookentries.get(l.Product_3__c).unitprice,opportunityid=l.convertedopportunityid,pricebookentryid=pricebookentries.get(l.Product_3__c).id));
       if(l.Product_4__c == null) continue;       

lineitems.add(new opportunitylineitem(quantity=1,totalprice=pricebookentries.get(l.Product_4__c).unitprice,opportunityid=l.convertedopportunityid,pricebookentryid=pricebookentries.get(l.Product_4__c).id));}     

// commit line items to the database   

insert lineitems;    

 

}