You need to sign in to do that
Don't have an account?
Felipe 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!
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
With that being said, here is the basic structure you will need for a test method
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.