• Blake Miller 11
  • NEWBIE
  • 15 Points
  • Member since 2017

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 6
    Replies
This is my test class: 

@IsTest(SeeAllData=true) 
public with sharing class TestInsertDefaultLineItems {
    static testMethod void validateTrigger() {
       Account acc= new Account(Name = 'testAcc', Description='testdesc');
       insert acc;

        //Case record in Test method. 
        Contact conObj = new Contact();
        conObj.lastname = 'testcon';
        conObj.AccountId = acc.id;
        insert conObj;
 
        Opportunity opp1 = new Opportunity (AccountId = acc.Id, Name = 'testOpp', CloseDate = System.today());
            insert opp1;
        
       /* PricebookEntry priceBookEntryNew = new PricebookEntry ();
        Product2 product = new Product2 ();
        PriceBook2 pb2 = new PriceBook2 (Name='Standard priceBook', Description='test');
        insert pb2;      
       List pricebookList = [SELECT Id FROM PriceBook2 WHERE IsStandard = true ];*/
        
     PricebookEntry priceBookEntryNew = new PricebookEntry();
        Product2 product = new Product2(); 
        PriceBook2 pb2 = new PriceBook2 (Name='Standard priceBook',Description = 'test');
        insert pb2;
        List <PriceBook2> pricebookList = [SELECT Id FROM PriceBook2 WHERE IsStandard = true];
        PriceBook2 pricebooktest = new PriceBook2 ();
        if (pricebookList !=null && pricebookList.size()>0)
            pricebooktest = pricebookList.get(0);
        product.name = 'Test';
        insert product;
        
        priceBookEntryNew.Product2Id = product.Id;
        priceBookEntryNew.PriceBook2Id = pricebooktest.Id;
        priceBookEntryNew.UnitPrice = 20.00;
        priceBookEntryNew.UseStandardPrice = false;
        priceBookEntryNew.isactive = true; 
        insert priceBookEntryNew;

        OpportunityLineItem oli = new OpportunityLineItem
            (OpportunityId = opp1.Id,
             PricebookEntryId = priceBookEntryNew.Id,
             Quantity = 1,
             UnitPrice = priceBookEntryNew.UnitPrice,
             
             ServiceDate = System.today()
            );
        insert oli;
    }
}
This is my test class: 

@IsTest(SeeAllData=true) 
public with sharing class TestInsertDefaultLineItems {
    static testMethod void validateTrigger() {
       Account acc= new Account(Name = 'testAcc', Description='testdesc');
       insert acc;

        //Case record in Test method. 
        Contact conObj = new Contact();
        conObj.lastname = 'testcon';
        conObj.AccountId = acc.id;
        insert conObj;
 
        Opportunity opp1 = new Opportunity (AccountId = acc.Id, Name = 'testOpp', CloseDate = System.today());
            insert opp1;
        
       /* PricebookEntry priceBookEntryNew = new PricebookEntry ();
        Product2 product = new Product2 ();
        PriceBook2 pb2 = new PriceBook2 (Name='Standard priceBook', Description='test');
        insert pb2;      
       List pricebookList = [SELECT Id FROM PriceBook2 WHERE IsStandard = true ];*/
        
     PricebookEntry priceBookEntryNew = new PricebookEntry();
        Product2 product = new Product2(); 
        PriceBook2 pb2 = new PriceBook2 (Name='Standard priceBook',Description = 'test');
        insert pb2;
        List <PriceBook2> pricebookList = [SELECT Id FROM PriceBook2 WHERE IsStandard = true];
        PriceBook2 pricebooktest = new PriceBook2 ();
        if (pricebookList !=null && pricebookList.size()>0)
            pricebooktest = pricebookList.get(0);
        product.name = 'Test';
        insert product;
        
        priceBookEntryNew.Product2Id = product.Id;
        priceBookEntryNew.PriceBook2Id = pricebooktest.Id;
        priceBookEntryNew.UnitPrice = 20.00;
        priceBookEntryNew.UseStandardPrice = false;
        priceBookEntryNew.isactive = true; 
        insert priceBookEntryNew;

        OpportunityLineItem oli = new OpportunityLineItem
            (OpportunityId = opp1.Id,
             PricebookEntryId = priceBookEntryNew.Id,
             Quantity = 1,
             UnitPrice = priceBookEntryNew.UnitPrice,
             
             ServiceDate = System.today()
            );
        insert oli;
    }
}