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
Guru@SfCloudGuru@SfCloud 

URGENT :Test Code Coverage for OpportunityLineitems

Hi,

I am trying to get the 100% coede coverage for my class but i strucked at 53% ,can anyone suggest me how to get the 100 % code coverage. if any one help would be greatly appriciated.

My Class wich is not get Covered(Bold lines are not get covered)

trigger CreatinOpportunities on Opportunity (after update) {

    Set<Id>  OppId=New Set<Id>();

    for(Opportunity Opp:trigger.new)
    {
        if(Opp.Sync_to_Sage__c=='Sent to Sage')
           OppId.add(Opp.id);
    }
      
   if(Oppid.size()>0)
   {
       List<Asset> AssetLst=new List<Asset>();
      
       System.debug('helllllllllllooooo'+Oppid.size());
      
       for(Schema.OpportunityLineItem OppLine:[Select id,Name,Quantity,Opportunity.Accountid,Description from OpportunityLineItem where Opportunityid IN:oppid] )
       {
           System.debug('OppLine.Size()***********'+OppLine);
      
           for(Integer i=0;i<OppLine.Quantity;i++)
           {
                        asset a = new asset();
                        a.name=OppLine.name;
                        a.Enquiry__c = OppLine.OpportunityId;         
                        a.Accountid= OppLine.Opportunity.AccountId;
                        a.Description = OppLine.Description;
                        //a.Product2Id = OLI.ProductCode;// There is no product code field on the Asset object
                        //a.Supplier__C = OLI.Make_Model__c;//Field is not writable
                        //a.Equipmentcost = //no field is available with this name  in asset                     
                        AssetLst.add(a);  
           }
       }

     insert AssetLst;  
   }

My Test Class:

@isTest
Public class CreatinOpportunities_Test{
    Public static testMethod void CreatinOpportunities_class_Test(){
   
  
       
        Account Acc = new Account();
        Acc.Name = 'TestAccount';
        Insert Acc;
       
        Contact Con = new Contact();
        Con.AccountId = Acc.Id;
        Con.LastName = 'ContactLast';
        Insert Con;
      
       
        Opportunity Opp =  new Opportunity();
        Opp.Account = Acc;
        Opp.Account_on_hold__c = true;
        Opp.Name = 'TestOpportunity';
        Opp.StageName = 'Prospecting';
        Opp.Sync_to_Sage__c = 'Sent to Sage';
        Opp.CloseDate = System.Today().adddays(10);
       
        Insert Opp;
       
       
       
       
        Product2 Prod = new Product2();
        Prod.Name = 'TestProduct';
        Prod.CurrencyIsoCode = 'EUR';
        //Prod.UseStandardPrice=false;       
       
        Insert Prod;
       
      
        Opportunity Opp1 =  new Opportunity();
        Opp1.Account = Acc;
        Opp1.Account_on_hold__c = true;
        Opp1.Name = 'TestOpportunity';
        Opp1.StageName = 'Prospecting';
        Opp1.Sync_to_Sage__c = 'Ready to sync';
        Opp1.CloseDate = System.Today().adddays(30);
      
       
        Insert Opp1;
       
        Opp1.Sync_to_Sage__c = 'Sent to Sage';
       
        Update Opp1;
       
        List<Opportunity> OppLst = new  List<Opportunity >{Opp,Opp1};
       
       
        Asset Ass = new Asset();
        Ass.Name = 'TestAss';
        Ass.AccountId = Acc.Id;
        Ass.CurrencyIsoCode = 'EUR';
        Ass.Enquiry__c = Opp1.Id;      
       
        Insert Ass;
          
        Pricebook2 price = new Pricebook2(Name = 'TestPrice',IsActive = true);
      
        //PriceBook2 pb2Standard = [select Id from Pricebook2 where  Name =:'Standard Price Book' AND isActive = true Limit 1];
       
       
    
        PricebookEntry prEntry = new PricebookEntry(Pricebook2Id = price.Id,UnitPrice = 10.00,Product2Id = Prod.Id,IsActive = true);
     
       
        OpportunityLineItem OppLi = new OpportunityLineItem(OpportunityId = Opp.Id,Quantity = 1,TotalPrice = 200,PricebookEntryId = PrEntry.Id);
       
        OpportunityLineItem OppLi1 = new OpportunityLineItem(OpportunityId = Opp1.Id,Quantity = 2,TotalPrice = 100,PricebookEntryId = PrEntry.Id);
       
        OpportunityLineItem OppLi2 = new OpportunityLineItem(OpportunityId = Opp1.Id,Quantity = 3,TotalPrice = 150,PricebookEntryId = PrEntry.Id);
       
  
        List<OpportunityLineItem > OppLst1 = new  List<OpportunityLineItem >{OppLi,OppLi1,OppLi2};
       
      
       
        //System.debug('OppLst1**************'+OppLst1.Size());
     
   
    }

}


Vinit_KumarVinit_Kumar
The way I am looking at your code the below query will not return any row in your test class :-

Select id,Name,Quantity,Opportunity.Accountid,Description from OpportunityLineItem where Opportunityid IN:oppid

You need to associate OpportunityLineItem with Opportunty before updating your opportunty(which would fire your trigger).

Move this line of code to the last :-

Opp1.Sync_to_Sage__c = 'Sent to Sage';
      
 Update Opp1;

And also,you have not inserted your OpportunityLineItem record so before that you need to  add :

So,your test class should be :-

@isTest
Public class CreatinOpportunities_Test{
    Public static testMethod void CreatinOpportunities_class_Test(){
   
  
       
        Account Acc = new Account();
        Acc.Name = 'TestAccount';
        Insert Acc;
       
        Contact Con = new Contact();
        Con.AccountId = Acc.Id;
        Con.LastName = 'ContactLast';
        Insert Con;
      
       
        Opportunity Opp =  new Opportunity();
        Opp.Account = Acc;
        Opp.Account_on_hold__c = true;
        Opp.Name = 'TestOpportunity';
        Opp.StageName = 'Prospecting';
        Opp.Sync_to_Sage__c = 'Sent to Sage';
        Opp.CloseDate = System.Today().adddays(10);
       
        Insert Opp;
       
       
       
       
        Product2 Prod = new Product2();
        Prod.Name = 'TestProduct';
        Prod.CurrencyIsoCode = 'EUR';
        //Prod.UseStandardPrice=false;       
       
        Insert Prod;
       
      
        Opportunity Opp1 =  new Opportunity();
        Opp1.Account = Acc;
        Opp1.Account_on_hold__c = true;
        Opp1.Name = 'TestOpportunity';
        Opp1.StageName = 'Prospecting';
        Opp1.Sync_to_Sage__c = 'Ready to sync';
        Opp1.CloseDate = System.Today().adddays(30);
      
       
        Insert Opp1;
       
      //  Opp1.Sync_to_Sage__c = 'Sent to Sage';
       
      //  Update Opp1;
       
        List<Opportunity> OppLst = new  List<Opportunity >{Opp,Opp1};
       
       
        Asset Ass = new Asset();
        Ass.Name = 'TestAss';
        Ass.AccountId = Acc.Id;
        Ass.CurrencyIsoCode = 'EUR';
        Ass.Enquiry__c = Opp1.Id;      
       
        Insert Ass;
          
        Pricebook2 price = new Pricebook2(Name = 'TestPrice',IsActive = true);
      
        //PriceBook2 pb2Standard = [select Id from Pricebook2 where  Name =:'Standard Price Book' AND isActive = true Limit 1];
       
       
    
        PricebookEntry prEntry = new PricebookEntry(Pricebook2Id = price.Id,UnitPrice = 10.00,Product2Id = Prod.Id,IsActive = true);
     
       
        OpportunityLineItem OppLi = new OpportunityLineItem(OpportunityId = Opp.Id,Quantity = 1,TotalPrice = 200,PricebookEntryId = PrEntry.Id);
       
        OpportunityLineItem OppLi1 = new OpportunityLineItem(OpportunityId = Opp1.Id,Quantity = 2,TotalPrice = 100,PricebookEntryId = PrEntry.Id);
       
        OpportunityLineItem OppLi2 = new OpportunityLineItem(OpportunityId = Opp1.Id,Quantity = 3,TotalPrice = 150,PricebookEntryId = PrEntry.Id);
       
  
        List<OpportunityLineItem > OppLst1 = new  List<OpportunityLineItem >{OppLi,OppLi1,OppLi2};
       
        insert OppLst1;
       
	    Opp1.Sync_to_Sage__c = 'Sent to Sage';
       
        Update Opp1;
       
        //System.debug('OppLst1**************'+OppLst1.Size());
     
   
    }

}



If this helps,please mark it as best answer to help others :)

Guru@SfCloudGuru@SfCloud
Hi Vinit , Great thanks for your reply but right now i am facing with the new issue which is called as System.DmlException: Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, field integrity exception: PricebookEntryId, unknown (versions 3.0 and higher must specify pricebook entry id, others must specify product id): [PricebookEntryId, unknown].

Can You please suggest me how to overcome this.

Thanks in advance
Gurunath
Vinit_KumarVinit_Kumar
Ok you have not inserted your Pricebook2 record also.Unless you have the id genertaed you cant reference it.

put this line 

insert price;

after 

Pricebook2 price = new Pricebook2(Name = 'TestPrice',IsActive = true);

that should resolve it.