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 

please help me to write the test class for trigger

hi,

here is my trigger, please give me a test class

 

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, RecordType__c from OpportunityLineItem where Id in :oppLineItemIds])
     {
          oppLineItem.RecordType__c = oppLineItem.Opportunity.RecordType.Name;
          oppLineItems.add(oppLineItem);
     }
     if(oppLineItems.size() > 0)
     {
          update oppLineItems;
     }
}
k_bentsenk_bentsen

It's probably more beneficial to you if you write your own test class so that you are familiar with how to do so in the future. Here's the Salesforce guide on creating test classes: http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_qs_test.htm The example is very simple, but it should give you an idea of how to structure your test classes.

 

In the body of your test method, you could do:

1. Create dummy Opportunity and insert it. Be sure to define the record type.

2. Create dummy OpportunityLineItem, with the parent Id of above opportunity, and insert it.