+ Start a Discussion
bprakashbprakash 

Test code for pricebook

can anyone please tell how the test class for pricebook is defined?

i am some kind of trouble with the test code

 Pricebook2 standardPB = [select name, isactive from Pricebook2 where IsStandard = true limit 1];
          


         // Pricebook2 standardPB = new Pricebook2(Name = 'Standard Price Book 2009', Description = 'Price Book 2009 Products', IsActive = true,isStandard=true);  (SHOWING ERROR HERE error here no lists found)


         // insert standardPB;
        Pricebook2 pb = new Pricebook2(Name = 'Standard Price Book 2009', Description = 'Price Book 2009 Products', IsActive = true);
        insert pb;
        Product2 prod = new Product2(Name = 'Anti-infectives 2007', Family = 'Best Practices', IsActive = true);
        insert prod;

        PricebookEntry standardPrice = new PricebookEntry(Pricebook2Id = standardPB.Id, Product2Id = prod.Id, UnitPrice = 10000, IsActive = true, UseStandardPrice = false);
        insert standardPrice;

        PricebookEntry pbe = new PricebookEntry(Pricebook2Id = pb.Id, Product2Id = prod.Id, UnitPrice = 10000, IsActive = true, UseStandardPrice = false);
        insert pbe;

 

 

Can someone please expalin me how to overcome this.

Please tell me how to make this working?

bob_buzzardbob_buzzard

Can you clarify the issue that you are seeing?  Is it that you can't retrieve the pricebook you expect to, can't create a pricebook or something else?

bprakashbprakash

Its not returning any list with this code as its thowing error in my test code

this the line where iam getting error

 Pricebook2 standardPB = [select name, isactive from Pricebook2 where IsStandard = true limit 1];

it says that it haso rows.

Bob you have any test class for the price book,iam really confused on this,Please provide some test code so that i will try.

Thanks for your helping hand.

Thanks

Kanu

bprakashbprakash

Hi I  read the doc but,still able to get it done plz mention what changes i need to made to the code?

thanks

for you HELP!

craigmhcraigmh

At the top of your test class:

 

@isTest (seeAllData=true)

 

Or else you will essentially be working with an instance that has zero data in your test methods.

bprakashbprakash

Ok,

how the zero data is done,give an idea how to do with this so that i can take care of myself in the future.

 

iam able to do with this as

 

 

@isTest (seeAllData=true)  

iam really thankful to your help,community really helps

bprakashbprakash

@isTest(seeAllData=true)
private class  ContractLineUpdateTest {
    
    @isTest
    private static void  ContractLineUpdateTest() {
    
    List<ContractLineItem> cList = new List<ContractLineItem>();
     Account newAcc = new Account();
        newAcc.name = 'Test_Oppo_Account';
               
       
       //newAcc.Name= 'acc';
       newAcc.Industry = 'Agriculture';
       newAcc.Type = 'Reseller';
       insert newAcc;
       
       
       
        Asset asNew = new Asset();
      asNew.Name = 'new';
      //asNew.MAC__c = ;
      asNew.SerialNumber = 'po-123' ;
      asNew.AccountId = newAcc.Id;
      asNew.Type__c = 'Eval';
     asNew.Eval_Date__c = System.today() + 91;
       insert  asNew;
       
       
       
       
      
      Pricebook2 standardPB = [select name, isactive from Pricebook2 where IsStandard = true limit 1];
       License_Feature__c newLf= new License_Feature__c(Name ='FireEye Support');   
       insert newLf;

         // Pricebook2 standardPB = new Pricebook2(Name = 'Standard Price Book 2009', Description = 'Price Book 2009 Products', IsActive = true,isStandard=true);
         // insert standardPB;
        Pricebook2 pb = new Pricebook2(Name = 'Standard Price Book 2009', Description = 'Price Book 2009 Products', IsActive = true);
        insert pb;
        Product2 prod = new Product2(Name = 'Anti-infectives 2007', Family = 'Best Practices', IsActive = true ,License_Feature__c = newLf.Id);
        insert prod;

        PricebookEntry standardPrice = new PricebookEntry(Pricebook2Id = standardPB.Id, Product2Id = prod.Id, UnitPrice = 10000, IsActive = true, UseStandardPrice = false);
        insert standardPrice;

        PricebookEntry pbe = new PricebookEntry(Pricebook2Id = pb.Id, Product2Id = prod.Id, UnitPrice = 10000, IsActive = true, UseStandardPrice = false);
        insert pbe;
         /*Pricebook2 s = [select id from Pricebook2 where IsStandard = true];
       
       Product2 newP = new Product2();
       newP.Name = 'pro';
      
       insert newP;
       
       PriceBook2 newP2B =  new Pricebook2(Name = 'Standard Pricebook', Description = 'Price Book 2009 Products', IsActive = true);
       
       insert newP2B;
      
       
       
      PriceBookEntry newPb1 = new  PriceBookEntry();
       newPb1.Product2Id = prod.Id;
      newPb1.PriceBook2Id = pbe.Id;
       
      newPb1.UnitPrice = 2200;
       newPb1.UseStandardPrice = true;
       insert newPb1;
       
       
       
       PriceBookEntry newPb = new  PriceBookEntry();
       newPb.Product2Id = prod.Id;
      newPb.PriceBook2Id = pb.Id;
       
       newPb.UnitPrice = 22;
       newPb.UseStandardPrice = false;
       insert newPb;*/
       
        ServiceContract newS = new ServiceContract();
       newS.AccountId = newAcc.Id;
       newS.Name = newAcc.Name;
      newS.PriceBook2Id = pb.Id;
       insert newS;
       
       
       
       
       ContractLineItem newCl = new ContractLineItem();
       newCl.AssetId = asNew.Id;
       newCl.ServiceContractId = newS.Id;
       newCl.PricebookEntryId = Pbe.Id;
        newCl.Quantity = 1;
        newCl.UnitPrice = pbe.UnitPrice ;
        
        newCl.AssetId = asNew.Id;
        newCl.StartDate = date.parse('12/27/2009');
        newCl.EndDate = date.parse('12/27/2012');
       insert newCl;
       cList.add(newCl);
       
        ContractLineItem newCl1 = new ContractLineItem();
       newCl1.AssetId = asNew.Id;
       newCl1.ServiceContractId = newS.Id;
       newCl1.PricebookEntryId = Pbe.Id;
        newCl1.Quantity = 1;
        newCl1.UnitPrice = pbe.UnitPrice ;
        
        newCl1.AssetId = asNew.Id;
        newCl1.StartDate = date.parse('12/27/2009');
        newCl1.EndDate = date.parse('12/27/2012');
       insert newCl1;
       cList.add(newCl1);
       
       //ContractLineItem conList = [Select Id,AssetId,StartDate,EndDate,CreatedDate,PricebookEntry.Product2.License_Feature__c,PricebookEntry.Product2.License_Feature__r.Name,PricebookEntry.Product2.Level__c,Key__c,Status from ContractLineItem where AssetId =: asNew.Id AND Status != 'Expired' AND EndDate!=null AND StartDate != null ];
       if(cList.size()>0){
       
       for(ContractLineItem CL1 : cList){
          asNew.Support_Start_Date__c = CL1.StartDate;
          asNew.Support_End_Date__c= CL1.EndDate;
       
       }
       }
      
    
    
    }
    
    
  }

bprakashbprakash

so iam getting code coverage,

trigger ContractLineUpdate on Asset (before update) {
   Set<Id> asId = new Set<Id>();
  Map<Id, List<ContractLineItem>> conId = new Map<Id,List<ContractLineItem>>();
   for(Asset newAss : trigger.new){
   
     asId.add(newAss.Id);
   // Map<Id,List<ContractLineItem>> conList = new Map<Id,List<ContractLineItem>>([Select Id,AssetId,StartDate,EndDate,CreatedDate,PricebookEntry.Product2.License_Feature__c,PricebookEntry.Product2.License_Feature__r.Name,PricebookEntry.Product2.Level__c,Key__c,Status from ContractLineItem where AssetId in: newAss.Id AND Status != 'Expired' AND EndDate!=null AND StartDate != null]);
    
   }
 // List<Asset> astList = new List<Asset>([select Id,Name from Asset where Id In :asId]);
   
  List<ContractLineItem> conList = new List<ContractLineItem>([Select Id,AssetId,StartDate,EndDate,CreatedDate,PricebookEntry.Product2.License_Feature__c,PricebookEntry.Product2.License_Feature__r.Name,PricebookEntry.Product2.Level__c,Key__c,Status from ContractLineItem where AssetId in: asId AND Status != 'Expired' AND EndDate!=null AND StartDate != null ]);
 
        List<Date> stList = new List<Date>();
        List<Date> eList = new List<Date>();
        List<Date> maxEdate = new List<Date>();
        List<Date> maxSdate = new List<Date>();
        List<Date> urlSdate = new List<Date>();
       List<Date> urlEdate = new List<Date>();  
       List<Date> proSdate = new List<Date>();
       List<Date> proEdate = new List<Date>();    
                 
   for(Asset ast : trigger.new){
    if(ast.Replacement_For_Asset__c == null){
           maxEdate.clear();
           maxSdate.clear();
           stList.clear();
           eList.clear();
           urlEdate.clear();
           urlSdate.clear();
            ContractLineItem sCont = null,tCont = null,mCont=null,mSha=null,url= null,pro= null;
            String sKey= null,sType= null,mKey = null,sha=null,urlKey= null,proKey=null;       
                // List<ContractLineItem> newList = conList.get(ast.Id);
                system.debug('conList.size():'+conList.Size());
                 if(conList.size()>0){
                     for(ContractLineItem con : conList){
                          if(con.PricebookEntry.Product2.License_Feature__r.Name == 'FireEye Support'&& con.AssetId == ast.id ){
                          
                          stList.add(con.StartDate);
                           eList.add(con.EndDate);
                           if(sCont == null){
                            sKey = con.Key__c;
                            sCont = con;
                            
                            
                           
                           }else{
                            if(sCont.EndDate < con.EndDate){
                              sKey = con.Key__c;
                              sCont = con;
                            
                            }
                            else{
                             if(sCont.EndDate == con.EndDate){
                              if(sCont.CreatedDate < con.CreatedDate){
                                 sKey = con.Key__c;
                                 sCont = con;
                              }
                             }
                            }
                           }
                           if(con.Status == 'Active'){
                          if(tCont == null){
                             tCont = con;
                             sType = con.PricebookEntry.Product2.Level__c;
                                     
                          }else{
                             if(tCont.EndDate < con.EndDate){
                             sType = con.PricebookEntry.Product2.Level__c;
                             tCont= con;
                             }else
                               if(tCont.EndDate == con.EndDate){
                                 if(tCont.CreatedDate < con.CreatedDate){
                                 tCont = con;
                                 sType = con.PricebookEntry.Product2.Level__c;
                                   
                                 }
                               }
                          }
                           
                         }  
                     }
                  can some one help the test class for this trigger

bprakashbprakash

how to write a test class for this kind of situation,asset is a parent for contract line items.

iam not able to cover the test code,please i need help of this community

Rajesh SriramuluRajesh Sriramulu

Hi

 

u have to write all the code with in the testmethod only like this u didn't mention testmethod  try this

 

@isTest

private class ContractLineUpdateTest {
    private static testmethod void ContractLineUpdateTest()
    {

 

 

 

}}

 

 

 

SRS

bprakashbprakash

I got but iam not able to get code coverage 75% please take a look at test code.

Thanks

Bp

Rajesh SriramuluRajesh Sriramulu

Hi

 

 

 

Insert the values which are in if condition i.e satisfy the if condition by inserting the values

 

eg : ast.Replacement_For_Asset__c == null

 

u have to insert 'null'

 

PricebookEntry.Product2.License_Feature__r.Name == 'FireEye Support'

 

insert fireeye support into this .

 

then it will cover code

 

 

 

SRS

bprakashbprakash

Hi,

I had covered most of this by your advice,but i need to cover the createddate in the code,can you please give your advice,

Thanks

Bhanu

Rajesh SriramuluRajesh Sriramulu

Hi Prakash

 

 

sorry I can't get u ,

 

that createddate is custom field or private field  I don't know if it custom it should be like field__c and exactly where ur code is not covered I can't able find because  u told that only that createddate is not covered u can  assign the date value and insert if it comes means it's great can u post screen shot.

 

SRS

bprakashbprakash

CreatedDate is on the record,because iam sorting based on the Createddate for the records.

its standard field for every object

Thanks

Bhanu

Suraj Tripathi 47Suraj Tripathi 47
Hi bprakash,

Just add the below line in your class it will resolve your problem.
@isTest (seeAllData=true)  

If you find your Solution then mark this as the best answer.

Thank you!

Regards,

Suraj Tripathi