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
sfdclivesfdclive 

test class help





please help me how to write test class for this trigger

 

trigger UpdateProductRecordType on OpportunityLineItem (after insert)
{
    Set<String> oppLineItemIds = new Set<String>();
    
     for(OpportunityLineItem oppLineItem : Trigger.new)
     {
         oppLineItemIds.add(oppLineItem.Id);
     }
     List<OpportunityLineItem> oppLineItems = new List<OpportunityLineItem>();
     
     for(OpportunityLineItem oppLineItem : [select Opportunity.RecordType.Name, record_type_c__c from OpportunityLineItem where Id in :oppLineItemIds])
     {
          oppLineItem.record_type_c__c = oppLineItem.Opportunity.RecordType.Name;
          oppLineItems.add(oppLineItem);
     }
     if(oppLineItems.size() > 0)
     {
          update oppLineItems;
   }

}

 

the below test class is not covering my trigger 

test class:

 

@isTest
Private class UpdateProductRecordType_test
{
static testMethod void testUpdateProductRecordType()
{
RecordType rt = [select id,Name from RecordType where SobjectType='opportunity' and Name='A' Limit 1];
Opportunity o = new Opportunity(Name = 'Test' , StageName ='Closed Won' , CloseDate = Date.today());
insert o;

Product2 p = new Product2(Name = 'productname');
insert p;

Pricebook2 standardPB = [select id from Pricebook2 where isStandard=true];

Pricebook2 pb = new Pricebook2(Name = 'Standard Price Book 2009', Description = 'Price Book 2009 Products', IsActive = true);
insert pb;
Product2 prod = new Product2(Name = 'productname', 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;


OpportunityLineItem ol = new OpportunityLineItem(OpportunityId = o.id , record_type_c__c ='oppotunity b',
PricebookEntryId = standardPrice.id);
insert ol;
}

 

 

 

 

regards

sri

 

 

khushbukhushbu

sorry.

 

I am not having any idea regarding this.

Can u please help me on my query if u have any idea?

I know I am doing stupid thing.

But please help