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
AlgypugAlgypug 

Problem in writing test class

Hi..,

 

 

 

I have a problem in writing test class.

Actually i wrote a trigger , to calculate roll up summary in lookup relationships.I achieved that one.

BOM__c is parent object and QuoteLineItem is child object.

 

While writing test class , we need to create quote line item record for testing.

Quotelineitem is linked with PriceBOOkEntry object and Product Object.

So its giving errors while  testing like some values are missing.

I am posting my trigger and test class.

Can we create pricebookentry and product records manually....?

Can anyone help me plz.....?

 

Trigger :

-------------------------------------------

 

trigger RollUpTotalBasicPriceOnBOM on QuoteLineItem (after delete, after insert, after update) {

       Set<id> bomids = new Set<id>();
       decimal total;
     List<BOM__c> BOMtoUpdate = new List<BOM__c>();
       List<QuoteLineItem> quoteitem = new List<QuoteLineItem>();
      if(!trigger.isDelete){
        for (QuoteLineItem item : Trigger.new)
                  bomIds.add(item.BOM__c);
     }
    if (Trigger.isDelete) {
               for (QuoteLineItem item : Trigger.old)
                    bomids.add(item.BOM__c);
      }
     
      for(AggregateResult aggr : [select BOM__c bomId,sum(Basic_Price__c) bomTotal from QuoteLineItem where BOM__c in:bomids group by BOM__C ]){
        BOMToUpdate.add(new BOM__c(id = String.valueOf(aggr.get('bomId')),Total_Basic_Price__c = Integer.valueOf(aggr.get('bomTotal'))) );
      }
      update BOMToUpdate;
      System.debug('BOMToUpdate:'+BOMToUpdate);
}   

 

Test class ;-

 


@isTest
private class Test_RollUpTotalBasicPriceOnBOM {

    static testMethod void myUnitTest() {
        // TO DO: implement unit test
      Account acc = new Account(Name = 'testing',Credit_age__c = 100.00 ,VAT_Number__c = '1243',Credit_Period_Days__c = 20 , Credit_Terms__c = 'Open',ECC_No__c = 'ABC 1234',CurrencyIsoCode = 'INR',State__c = 'Andhra Pradesh',Customer_Category__c = 'Systems Integrator',Source__c = 'Cold Call',Source_Detail__c ='nothing',Type__c = 'Customer',Sector__c ='Iron & Steel',Product_Type__c ='Standard',Competition__c = 'Glastronix',Annualised_Quantity_Per_Month__c = 2000.00,Temperature__c = 'Warm',Address_Line_1__c = 'Hindupur',City__c = 'Bangalore',Pin_Code__c = '515201',Region__c = 'North');
      insert acc;  
      Opportunity  o = new Opportunity( name = 'opportunity',CloseDate = System.today()+10,StageName = 'Approach',ForecastCategoryName = 'Pipeline',AccountId = acc.id);
      insert o;
      Quote q = new Quote(BillingName = 'sample',Validity__c = 'After 20 days',Status = 'Accepted',Quotation_Type__c = 'Per Product',Packing__c = 1000.0,OpportunityId = o.id,Name = 'quote', Freight__c = 'To Pay - Customer to Pay Transporter',Excise_Duty__c = 12.0 , Education_Cess_on_ED__c = 2.0,Discount__c = 10.0,Cess_Sec_Higher_on_ED__c = 1.0 );
      insert q;
      Pricebook2 p = new Pricebook2 (Description = 'Sampleone' , Name  = 'Sample' );
      insert p;
      Product2  pro = new Product2 (Account__c = acc.id , Family = 'Scrap' , Name = 'Steel' , Colour__c = 'RAL 7035 - Light Grey' , Finish__c = 'Structured' , Approx_Weight_Kg__c = 8780);
      insert pro;
      //PricebookEntry price  = new PricebookEntry (UseStandardPrice = false, CurrencyIsoCode = 'INR' , Pricebook2Id = p.id , Product2Id  = pro.id , UnitPrice  = 100 ) ;
      //insert price;
      Employee_Info__c e = new Employee_Info__c(Type__c = 'Staff' , Status__c = 'Trainee' , Name = 'Mahesh' , Date_of_Joining__c = System.today() , Date_of_Birth__c = System.today() - 30 , Fucntional_Area__c = 'Developer' );
      insert e;
      BOM__c b = new BOM__c (BOM_Status__c = 'Pending' , Designer__c = e.id , Product__c = pro.id , Product_Quantity__c = 100);
      insert b;
      QuoteLineItem quote = new QuoteLineItem  (BOM__c = b.id , Rate_Kg__c = 10,Rate_Basis__c = 'Per Kg' , QuoteId = q.id , Quantity = 20 , unitprice = 20);
      insert quote;    
         }
}
Rahul SharmaRahul Sharma

Hi Algypug,

 

Pricebook2 cannot be created in test class, so you need to query existing records from database and use them in query.

And if you have version 24 for your test class then as per latest release you dont get existing data by default, therefore you need to make it true by making @isTest(seeAllData=true)