You need to sign in to do that
Don't have an account?

Help Writing Test Class for Trigger
Hi,
Could you please help me write a test class for my trigger? I managed to get 10% covered, but since I am not a programmer I am having a hard time understanding how to correct it to get more. Thank you in advance!
This is my Trigger:
trigger CreateAssetonClosedWon on Opportunity (after insert, after update) { for(Opportunity o: trigger.new){ if(o.isWon == true && o.HasOpportunityLineItem == true && o.RecordTypeId == '012200000004fxZ'){ String opptyId = o.Id; OpportunityLineItem[] OLI = [Select UnitPrice, Quantity, PricebookEntry.Product2Id, PricebookEntry.Product2.Name, Description, Converted_to_Asset__c From OpportunityLineItem where OpportunityId = :opptyId and Converted_to_Asset__c = false]; Asset[] ast = new Asset[]{}; Asset a = new Asset(); for(OpportunityLineItem ol: OLI){ a = new Asset(); a.AccountId = o.AccountId; a.Product2Id = ol.PricebookEntry.Product2Id; a.Quantity = ol.Quantity; a.Price = ol.UnitPrice; a.PurchaseDate = o.CloseDate; a.Status = 'Purchased'; a.Description = ol.Description; a.Name = ol.PricebookEntry.Product2.Name; ast.add(a); ol.Converted_to_Asset__c = true; } update OLI; insert ast; } } }
Please use this to start. First you will need to create test data for your trigger. Then you will need to simulate the test scenario to test your trigger, in this case you will need to update an opportunity with the stage as Closed.
It gives an error message doing the test execusion:
Error Message: System.ListException: List index out of bounds: 0
Stack Trace: Class.testCreateAssetonClosedWon.testCreateAssetonClosedWon: line 13, column 1
Line 13 is " oli.PriceBookEntryID = [select id from PriceBookEntry limit 1][0].ID;"
What does it want me to change?
A quick fix for that would be to change
to
You are missing SeeAllData=true in your test class.Please change from :-
@IsTest
to
@IsTest(SeeAllData=true).
Also,you please make sure that you have active PricebookEntry in your org.
Error Message: System.DmlException: Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, field integrity exception: PricebookEntryId (pricebook entry is inactive): [PricebookEntryId]
Stack Trace: Class.testCreateAssetonClosedWon.testCreateAssetonClosedWon: line 16, column 1
Line 16 is the insert oli part
oli.PriceBookEntryID = [select id from PriceBookEntry WHERE IsActive = TRUE limit 1][0].ID;
This should help!
Error Message: System.DmlException: Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, field integrity exception: PricebookEntryId (pricebook entry currency code does not match opportunity currency code): [PricebookEntryId]
It seems I solved the currency error (no idea if it is the right way, but no error for that now) But then I got another error since before closing an opportunity we have to add a Delivery Contact under Contact Roles on the opportunity. Think I solved that too (no error message for that now).
But we also have to add a territory before closing. I have no idea how to get that into the test class. Anyone who can help? This is my test class code now.
Also please let me know if it does not do what it is supposed to now. I have no idea what I am doing...
I am guessing I have to make the opportunity close since that is when the products get automatically turned into assets on the account page? But I have never had to add a territory to a test class before which makes me question if this really is the correct way to do it.
Now have a 100% code coverage!
I have something similar going on. Can you post your final code?
I tried deploying in production a new field and workflow and workflow field update, and it resulted in the following error:
Failure Message: "System.DmlException: Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, field integrity exception: PricebookEntryId (pricebook entry is inactive): [PricebookEntryId]", Failure Stack Trace: "Class.ForceLIUpdate.testOpportunityTrigger: line 38, column 1"