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
matt.bylermatt.byler 

Writing test code for a trigger

I have the following trigger I need to deploy to production but have only been able to get 66% test coverage. 

 

trigger createworkorder on Opportunity (after update)
{
if(trigger.new[0].StageName=='closed won'&&(trigger.old[0].StageName<>'closed won'))
{
Work_Order__c obj=new                               Work_Order__c(Opportunity__c=trigger.new[0].id,Stage__c='New',Account__c=trigger.new[0].AccountId);

insert obj;
List<Case> ll=new List<Case>();
for(OpportunityLineItem la : [SELECT PricebookEntry.Product2.Name from OpportunityLineItem where opportunityId =: Trigger.new])
{
String ss= String.ValueOf(la.PricebookEntry.Product2.Name);
case c1=new case(Status='new',Subject='Fulfillment for'+' '+ss,Work_Order__c=obj.id);
ll.add(c1);
}
insert ll;
}
}

 

Can someone help me with this test code?

Best Answer chosen by Admin (Salesforce Developers) 
matt.bylermatt.byler

Final Code:

 

@isTest(SeeAllData=True)
public class testcreateworkorder{
public static testMethod void testcreateworkorder()
{
//Create Account
Account a= new Account(Name = 'Test Account');
insert a;

PriceBook2 objPriceBook = [select Id from Pricebook2 where isStandard=true limit 1];

Product2 objPro1 = new Product2(Name = 'Abc Software',IsActive = true);
Insert objPro1;

PricebookEntry objPriceBookEntry = new PricebookEntry(Pricebook2Id =objPriceBook.Id,Product2Id = objPro1.Id,UnitPrice = 100,IsActive = true,UseStandardPrice=false);
insert objPriceBookEntry;

Opportunity objOpp1= new Opportunity();
objOpp1.Name = 'Testing12';
objOpp1.AccountId = a.Id;
objOpp1.StageName = 'Prospecting';
objOpp1.CloseDate=date.newinstance(2013, 2, 17);
insert objopp1;
 
OpportunityLineItem qli = new OpportunityLineItem(Opportunityid=objopp1.Id,pricebookentryid=objPriceBookEntry.id,TotalPrice=100,Quantity=2);
insert qli;


// update the opportunity in order to test trigger

objOpp1.StageName='Closed Won';
update objopp1;

system.assertEquals(1, [select count() from work_order__c where opportunity__c = :objopp1.id LIMIT 1]);
}
}

 

100% Coverage Thanks so much guys!!! Appreciate it.

All Answers

matt.bylermatt.byler

All the code is covered except for this section:

 

String ss= String.ValueOf(la.PricebookEntry.Product2.Name);
case c1=new case(Status='new',Subject='Fulfillment for'+' '+ss,Work_Order__c=obj.id);
ll.add(c1);

carlocarlo

We need to see your test code.

matt.bylermatt.byler

@isTest(SeeAllData=true)
public class testcreateworkorder{
public static testMethod void testcreateworkorder()
{
 // create opportunity
 
Opportunity testopp=new Opportunity(name='Test Opp',StageName='Prospecting',CloseDate=date.newinstance(2013, 2, 17));
insert testopp;

//Query opp

Opportunity testopp2= [SELECT Id, Name FROM Opportunity
                                WHERE Name='Test Opp' LIMIT 1];
System.assert(testopp2 != null);

// update the opportunity in order to test trigger

testopp.StageName='Closed Won';
update testopp;

system.assertEquals(1, [select count() from work_order__c where opportunity__c = :testopp.id LIMIT 1]);
}
}

carlocarlo

So in your trigger the section of code which is untested is surrounded by this for loop:

 

for(OpportunityLineItem la : [SELECT PricebookEntry.Product2.Name from OpportunityLineItem where opportunityId =: Trigger.new])
{
String ss= String.ValueOf(la.PricebookEntry.Product2.Name);
case c1=new case(Status='new',Subject='Fulfillment for'+' '+ss,Work_Order__c=obj.id);
ll.add(c1);
}

 

There are no OpportunityLineItem objects which have an opportunityId matching the testopp you are updating.

matt.bylermatt.byler

Can you tell me how I can insert products for that opportunity?

 

carlocarlo

Total guess here : 

 

 

@isTest(SeeAllData=true)

public class testcreateworkorder{

public static testMethod void testcreateworkorder()

{

 // create opportunity

 

Opportunity testopp=new Opportunity(name='Test Opp',StageName='Prospecting',CloseDate=date.newinstance(2013, 2, 17));

insert testopp;

 

//Query opp

 

Opportunity testopp2= [SELECT Id, Name FROM Opportunity

                                WHERE Name='Test Opp' LIMIT 1];

System.assert(testopp2 != null);

 

// Look here is my guess work

// I am anxious there is no reference to PricebookEntry.Product2.Name


OpportunityLineItem poo = new OpportunityLineItem(opportunityId = testopp2.Id);

insert poo;

 

// update the opportunity in order to test trigger

 

testopp.StageName='Closed Won';

update testopp;

 

system.assertEquals(1, [select count() from work_order__c where opportunity__c = :testopp.id LIMIT 1]);

}

}

New_DeveloperNew_Developer

Try this

 

 

Account a = new Account(Name = 'Test Account');
insert a;

PriceBook2 objPriceBook = [select Id from Pricebook2 where isStandard=true limit 1];

Product2 objPro1 = new Product2(Name = 'Abc Software',IsActive = true);
Insert objPro1;


PricebookEntry objPriceBookEntry = new PricebookEntry(Pricebook2Id =objPriceBook.Id,Product2Id = objPro1.Id,UnitPrice = 100,IsActive = true,UseStandardPrice=false);
insert objPriceBookEntry;

Opportunity objOpp1= new Opportunity();
objOpp1.Name = 'Testing12';
objOpp1.AccountId = a.Id;
objOpp1.StageName = 'Prospecting';
insert objopp1;

 

OpportunityLineItem qli = newOpportunityLineItem(OpportunityId=objopp1.Id,
pricebookentryid=objPriceBookEntry.id, unitprice=1.5, quantity=2);

matt.bylermatt.byler

Final Code:

 

@isTest(SeeAllData=True)
public class testcreateworkorder{
public static testMethod void testcreateworkorder()
{
//Create Account
Account a= new Account(Name = 'Test Account');
insert a;

PriceBook2 objPriceBook = [select Id from Pricebook2 where isStandard=true limit 1];

Product2 objPro1 = new Product2(Name = 'Abc Software',IsActive = true);
Insert objPro1;

PricebookEntry objPriceBookEntry = new PricebookEntry(Pricebook2Id =objPriceBook.Id,Product2Id = objPro1.Id,UnitPrice = 100,IsActive = true,UseStandardPrice=false);
insert objPriceBookEntry;

Opportunity objOpp1= new Opportunity();
objOpp1.Name = 'Testing12';
objOpp1.AccountId = a.Id;
objOpp1.StageName = 'Prospecting';
objOpp1.CloseDate=date.newinstance(2013, 2, 17);
insert objopp1;
 
OpportunityLineItem qli = new OpportunityLineItem(Opportunityid=objopp1.Id,pricebookentryid=objPriceBookEntry.id,TotalPrice=100,Quantity=2);
insert qli;


// update the opportunity in order to test trigger

objOpp1.StageName='Closed Won';
update objopp1;

system.assertEquals(1, [select count() from work_order__c where opportunity__c = :objopp1.id LIMIT 1]);
}
}

 

100% Coverage Thanks so much guys!!! Appreciate it.

This was selected as the best answer