+ Start a Discussion
Salesforce Admin 110Salesforce Admin 110 

unit test for trigger

i have only been able to get 39% code coverage on this trigger need some help with the unit test: 
 
trigger newoppty on contact (after insert, after update) {

     
    
    list<opportunity> opplist = new list<opportunity>();
    
    for (contact con : trigger.new) {
    
    

        if( con.Stage__c == 'Lead' && con.contacttype__C =='Seller') {
            opportunity newopp = new opportunity ();
            
            newopp.ownerid = con.allocated_user__c;
            newopp.name = 'Market Appraisal'; 
            newopp.accountid = con.accountid;
            newopp.CurrencyIsoCode = con.currencyisocode;
            newopp.stagename = 'Lead';
            newopp.recordtypeid = '012250000000Jev';
            newopp.contact__c = con.id;
            newopp.closedate = Date.today().addDays(7);
            newopp.PriceBook2Id = '01s250000005Du6';
            
            

            opplist.add(newopp);

}
}


        try {
        insert opplist;
       OpportunityLineItem[] lines = new OpportunityLineItem[0];
PricebookEntry[] entry = [SELECT Id, Name, UnitPrice FROM PricebookEntry WHERE Pricebook2Id = '01s250000005Du6' AND name In ('Market Appraisal')];
for(Opportunity record: opplist) {
    for (PricebookEntry thisentry : entry)
        lines.add(new OpportunityLineItem(PricebookEntryId=thisentry.Id, OpportunityId=record.Id, UnitPrice=thisentry.UnitPrice, Quantity=1));
}
        insert lines;     
} catch (system.dmlexception e) {
system.debug (e);
}

    
    }

 
Best Answer chosen by Salesforce Admin 110
RAM AnisettiRAM Anisetti
Try this one..
@isTest 
public class Testnewoppty {
    static testMethod void testoppt() {
	  contact c=new contact();
	  c.lastname='Anisetti';
	  c.Stage__c = 'Lead';
	  c.contacttype__C ='Seller';
	  
	  insert c;
	
	   
	
	}
	
	}