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
Felipe FernandesFelipe Fernandes 

Test class

Hello!

 

I need help to write a test class.

How can I test this trigger? I tried to deploy the trigger but I have to do a test...

 

 

trigger productComboUpdate on OpportunityLineItem (after insert) {
    //get the Product Numbers and other fields from the OpportunityLineItem and build a list of OpportunityLineItems to update
    List<OpportunityLineItem> oliList = new List<OpportunityLineItem>();
    for(OpportunityLineItem oli : [Select Id, PricebookEntry.Product2.Number1__c, PricebookEntry.Product2.Number2__c from OpportunityLineItem where Id in : trigger.new]) {
        oli.ProductCombo__c = oli.PricebookEntry.Product2.Number1__c + oli.PricebookEntry.Product2.Number2__c; //Update the OpportunityLineItem
        oliList.add(oli);
    }
    update oliList; //Update the OpportunityLineItems with the ProductCombo__c field
}

 

 

Can someone help me with this?

Thanks!

 

kyle.tkyle.t

Looking at your trigger, I think you may have over complicated things. rather than updating the Opportunity Line Item after instert you can do it before you insert and not have to update a record  you just inserted.  Also, you don't need the SOQL query since all the information you need is passed in the trigger.new

 

 

trigger productComboUpdate on OpportunityLineItem (before insert) {    
    for(OpportunityLineItem oli : trigger.new) {
        //Update the OpportunityLineItem
oli.ProductCombo__c =
oli.PricebookEntry.Product2.Number1__c +
oli.PricebookEntry.Product2.Number2__c; } }

 

With that being said, here is the basic structure you will need for a test method

 

@isTest
private class testMyOpportunityLineItemTrigger {
  
	public static testMethod void testOLITrigger(){
		Opportunity opp = new Opportunity();
		opp.name = 'test opp 1';
		insert opp;

		OpportunityLineItem oli = new OpportunityLineItem();
		oli.OpportunityId = opp.id;
		//fill in necessary details to insert an opportunity line item
		insert oli;
		
//make sure the value you get for ProductCombo__c is what you expect System.assertequals(<the number you expect to get>,oli.ProductCombo__c); } }

 

 

 

 

Pradeep_NavatarPradeep_Navatar

Write a test class that runs for the unit test before the deployment.

 

Below is a code for sample test class :

 

                @isTest

                private class TestOpportunityLineItem

                {

                                static testMethod void OppoLineItemMethod()

                                {

                                                List<OpportunityLineItem> oliList = new List<OpportunityLineItem>();

                                                // create a OpportunityLineItem with proper required field

                                                // add the add the created record in to the oliList -> oliList.add(rec);

                                                // insert the list -> insert oliList;

                                                // update few records those are in the list -> oli.ProductCombo__c = oli.PricebookEntry.Product2.Number1__c + oli.PricebookEntry.Product2.Number2__c;

                                                // update the oliList;

                                }

                }

                and then run this test class, it will run successfully if there is no error.