function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
cfdudecfdude 

Help needed for Test on Trigger

I'm fairly new to Apex development and am trying to get the hang of how to write these tests for triggers, but am having a tough time with this.  Any help is appreciated.

 

Here is the trigger:

 

trigger updOpportunityFromLeadConvert on Lead (before update) {

    Map<Id,String> leadIndustry     = new Map<Id,String>(); 
    Map<Id,String> leadHowHear      = new Map<Id,String>(); 
    Map<Id,String> leadOther        = new Map<Id,String>(); 
    Map<Id,String> leadDescription  = new Map<Id,String>();
    Map<Id,String> leadSource       = new Map<Id,String>();
    Map<Id,String> iiSolution       = new Map<Id,String>(); 
    
    date myDate = date.today();
    date trialCloseDate = myDate.addDays(30);
    
    for(Lead lead : Trigger.new) {
        if (lead.IsConverted) {
            leadIndustry.put(lead.ConvertedOpportunityId,lead.Industry);
            leadHowHear.put(lead.ConvertedOpportunityId,lead.How_did_you_hear_about_us__c);
            leadOther.put(lead.ConvertedOpportunityId,lead.Other__c);
            leadDescription.put(lead.ConvertedOpportunityId,lead.Description);
            if(lead.LeadSource=='Web Trial'){
                leadSource.put(lead.ConvertedOpportunityId,'In Trial');
            }else{
                leadSource.put(lead.ConvertedOpportunityId,'Targeted Leads');
            }
            if(lead.LeadSource=='Web Trial'){
                iiSolution.put(lead.ConvertedOpportunityId,'i2Dashboard');
            }
        }
    }
    
    List<Opportunity> conOpp = [Select  Id  From Opportunity  WHERE Opportunity.Id IN :leadIndustry.keySet()];
    
    for ( Opportunity o : conOpp) {
        o.Industry_Sector__c = leadIndustry.get(o.Id);
        o.How_did_you_hear_about_us__c = leadHowHear.get(o.Id);
        o.Other__c = leadOther.get(o.Id);
        o.Description = leadDescription.get(o.Id);
        o.StageName = leadSource.get(o.Id);
        o.iiSolution__c = iiSolution.get(o.Id);
        o.Type = 'New (N)';
        o.CloseDate = trialCloseDate;
        
    }
    update conOpp;
}

 

Here is the test I've written so far (with a few errors in the code already that I'm aware of).  I'm just stuck on this.

 

@isTest
private class updOpportunityFromLeadConvertTest {

    static testMethod void testUpdOpportunityFromLeadConvert(){
    	
    	// set 30 days from now
        date myDate = date.today();
    	date trialCloseDate = myDate.addDays(30);
    	
    	// create list to hold leads
    	List<Lead> leads = new List<Lead>{};
    	converts = new List[];
    	
    	// loop 200 times to make 200 leads, add to list
        for(Integer i=0; i < 200; i++){
        	Lead ld = new Lead(
        		Company='TestCo' + i, 
        		Description='Refer/Browser/Ip Info', 
        		Email='user@testco.com', 
        		Industry='Alternative Energy', 
        		LeadSource='Web Trial', 
        		Status='Incomplete Registration', 
        		FirstName='Joe', 
        		LastName='Blow', 
        		Phone='310-555-1212', 
        		Title='Senior Janitor', 
        		How_did_you_hear_about_us__c='i2live event', 
        		Other__c='beetlemania', 
        		Type__c='Lead');
        	leads.add(ld);	
       }
        
        // start test
        test.startTest();
       
        // insert lead        
        insert leads;

        
        for(Lead l : leads){
	        //Convert the lead
	        Database.LeadConvert lc = new database.LeadConvert();
	        lc.setLeadId(l.Id);
	        
	        
	        //Set the converted status
	        LeadStatus convertStatus = [Select Id, MasterLabel from LeadStatus where IsConverted=true limit 1];
			lc.setConvertedStatus(convertStatus.MasterLabel);
	        
	        // Commit the converted lead
	        Database.Leadconvertresult lcr = Database.convertLead(lc); 
	        
	        converts.add(lcr.getOpportunityId());
        }
        
        // stop test
        test.stopTest();
        
        // Query the db for Opportunities converted from Lead
        List<Opportunity> convertOpp = [Select Industry_Sector__c, How_did_you_hear_about_us__c, Other__c,
        									  Description, StageName, iiSolution__c, Type, CloseDate
        								from Opportunity
        								Where Id IN :leads.Id];

        // Assert fields have right values
        for(Opportunity o : convertOpp){
    		System.assertEquals('Alternative Energy', o.Industry_Sector__c); 
    		System.assertEquals('i2live event', o.How_did_you_hear_about_us__c); 
    		System.assertEquals('beetlemania', o.Other__c);
			System.assertEquals('Refer/Browser/Ip Info', o.Description); 
    		System.assertEquals('In Trial', o.StageName);
    		System.assertEquals('i2Dashboard', o.iiSolution__c);
    		System.assertEquals('New (N)', o.Type);
			System.assertEquals(trialCloseDate, o.CloseDate);
    		
        }
        
        
    
    }


}