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

please help me to write the test class for trigger


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 :
     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;
     if(oppLineItems.size() > 0)
          update oppLineItems;

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: 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.