• Nate Helterbrand 11
  • NEWBIE
  • 0 Points
  • Member since 2016

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 1
    Replies

Hi Dev community, I have written an apex trigger to automatically convert an opportunity product to an asset dependent on criteria, I have managed to write a test class but I think I am missing something simple to get coverage. As it stands I only get 9% coverage on my trigger. 

Apex Trigger

 

trigger OpportunityAssetonClosedWon on Opportunity (after insert, after update) {
    for(Opportunity o: trigger.new){
        if(o.isWon==true && o.HasOpportunityLineItem==true){
            String opptyId = o.Id;
            OpportunityLineItem[] OLI = [Select UnitPrice, Quantity, PricebookEntry.Product2Id, Product2.Name,
                                         Product2.Family, PricebookEntry.Product2.Name
                                         From OpportunityLineItem 
                                         WHERE OpportunityId=:opptyId];
            Asset[] ast = new Asset[]{};
            Asset a = new Asset();
            for(OpportunityLineItem ol: OLI){
            if(ol.Product2.Family=='Terminal' || ol.Product2.Family=='Gateway' ){
                a = new Asset();
                a.AccountId = o.AccountId;
                a.Product2Id= ol.PricebookEntry.Product2Id;
                a.Quantity= 1;
                a.Price= ol.UnitPrice;
                a.PurchaseDate=o.CloseDate;
                a.status='Purchased';
                a.Name = ol.Product2.Name;
                ast.add(a);
                ol.Converted_to_Asset__c = true;
            }
            }
                update OLI;
                insert ast;
            }
        }
    }

Apex Test Class
 
@isTest
public class OpportunityAssetonClosedWonTest 
{
	static testMethod void closedOpportunity() 
	{
        Account testAcc = new Account(Name = 'TestAccount');
        insert testAcc;
        
        Pricebook2 p = new Pricebook2(Name = 'Testbook');
        insert p;
        
		Opportunity testOpportunity = new Opportunity(
            StageName = 'Sourcing Demand',
            CloseDate = Date.newInstance(2017,12,31),
            AccountId = testAcc.Id,
            Name = 'Test Opportunity Triggers',
            Pricebook2Id=Test.getStandardPricebookId()
        );
        insert testOpportunity;

		Pricebook2 pb22 = new Pricebook2(Name='testDIE');
		insert pb22;
		
        Product2 pro1 = new Product2(Name='BXAD', isActive=true ,Family = 'Terminal');
		insert pro1;
		Product2 pro2 = new Product2(Name='BXCD', isActive=true ,Family = 'Gateway');
		insert pro2;
        Product2 pro3 = new Product2(Name='BXBD', isActive=true ,Family = 'Card Not Present');
        insert pro3;
        
		PricebookEntry pbe1 =new PricebookEntry(UnitPrice=10000,Product2Id=pro1.Id,Pricebook2Id=Test.getStandardPricebookId(),
											 isActive=true,UseStandardPrice = false);
		insert pbe1;
        PricebookEntry pbe2 =new PricebookEntry(UnitPrice=5000,Product2Id=pro2.Id,Pricebook2Id=Test.getStandardPricebookId(),
											 isActive=true,UseStandardPrice = false);
		insert pbe2;
        PricebookEntry pbe3 =new PricebookEntry(UnitPrice=1000,Product2Id=pro3.Id,Pricebook2Id=Test.getStandardPricebookId(),
											 isActive=true,UseStandardPrice = false);
		insert pbe3;

		OpportunityLineItem OPplineitem1 = new OpportunityLineItem (Quantity=1, OpportunityId=testOpportunity.Id,UnitPrice=0.01,PriceBookEntryId=pbe1.Id);
		insert OPplineitem1;   
        OpportunityLineItem OPplineitem2 = new OpportunityLineItem (Quantity=1, OpportunityId=testOpportunity.Id,UnitPrice=0.01,PriceBookEntryId=pbe2.Id);
		insert OPplineitem2;    
        OpportunityLineItem OPplineitem3 = new OpportunityLineItem (Quantity=1, OpportunityId=testOpportunity.Id,UnitPrice=0.01,PriceBookEntryId=pbe3.Id);
		insert OPplineitem3;    
			
        
        
        Test.startTest();
        
			testOpportunity.StageName = 'Closed/Won';
			update testOpportunity;
        
        Test.stopTest(); 
        
		System.assertEquals(testOpportunity.StageName, 'Closed/Won');
	}
}