You need to sign in to do that
Don't have an account?
bikla78
OppLineItem Test Method Error
I have a trigger that sends an outbound email when an opportunitylineitem is deleted from a particular opportunity. However my test class is failing saying that "no index id on line 22". any help would be greatly appreciated
my trigger:
trigger UnassignedConsultantRemoved on OpportunityLineItem (before delete) {
{
Set<Id> Pricebookentryid = new Set<Id>();
for (OpportunityLineItem OppLineItem : Trigger.old)
{
if (opplineitem.opportunityid =='006R0000003rc72IAA')
{
PricebookentryId.add(opplineitem.pricebookentryid);
Map<Id,String> PricebookEntryMap = new Map<Id, String>();
Map<Id,Datetime> PricebookEntryDate = new Map<Id, Datetime>();
for(Pricebookentry pb:[select id, name, lastmodifieddate from pricebookentry where id IN: pricebookentryID])
{
Pricebookentrymap.put(pb.id,pb.name);
PricebookentryDate.put(pb.id, pb.lastmodifieddate);
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
String[] toAddresses = new String[] {'bkim@dlcinc.com'};
mail.setToAddresses(toAddresses);
mail.setSubject('DLC Consultant removed from Bench');
mail.setUseSignature(false);
string msg = ' <br>';
msg = msg + '<b>' + 'Consultant, ' + pb.name + ' has been removed from the bench on ' + '</i>' + pb.lastmodifieddate;
msg = msg ;
mail.setHtmlBody(msg);
// Send the email
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
}
}
}
}
}
My test class:
@isTest
private class TestOpportunityLineItemDelete {
static testMethod void myUnitTest() {
Account acc = new Account(Name = 'TestAccount', Type = 'Prospect', Industry = 'Banking', Physical_Street__c = 'Test Street',Physical_City__c = 'Los Angeles', Physical_State__c = 'CA', Physical_Zip_Postal_Code__c = '90017', Physical_Country__c = 'USA', Phone = '(888) 999-1234' );
Opportunity opp = new Opportunity(Name= 'Test', accountid = acc.Id, Type = 'CSD Transition', Stagename= 'Won-Engagement in Process', PracticeArea__c = 'Won-Engagement in Process',CloseDate=Date.today(), EngagementStartDate__c = Date.today(), EngagementEndDate__c = Date.today(),Work_Type__c = 'Project',Status__c = 'Full-time', OT_Multiplier__c = 1, DT_Multiplier__c =1 );
OpportunityLineItem olt1 = new OpportunityLineItem(Opportunityid = opp.id,Unitprice= 180.00, Quantity = 1);
try {
insert olt1;
}
catch(DmlException olt)
{
System.debug(olt.getMessage());
}
test.starttest();
ID newid = olt1.id;
delete olt1;
test.stoptest();
}
}
First of all, you'll need to get that SOQL query out of the for loop. (Also I do not see that you use the Maps you create.)
Second, are you sure you want to send an email for each pricebookEntry separately? Why not one email with multiple ids (oh and id would be more helpful to an admin user than name)?
Neither of those points will cause an error given your test case.
The error is likely the line "ID newid = olt1.id;"
Comment that line and see if your test succeeds.
You reference IDs that do not exist at that point of time.
In the line
OpportunityLineItem olt1 = new OpportunityLineItem(Opportunityid = opp.id,Unitprice= 180.00, Quantity = 1);
you assign the Opp.ID but the opportunity does not have an ID yet because the opp has not been inserted yet. Same with the AccIDabove that.
You need to
Insert a few System.Debug statements for the acc, opp and olt1 to see if the have values in the ID fields before you insert.
Thanks guys for all your comments- I appreciate your insight to this issue. I changed my test class so it sets each record variable followed by an insert statement so the id exists untill opplineitem but I am still receiving the error message below:
System.ListException: Missing id at index: 0
Line 40 column 3
Okay. I just added the pricebookentry record before the opplineitem but it i get the same error on line 51 this time. It still not be able to find the id
Ok but this is where i get stuck at. As you can see, each insert statement has a catch and system.debug but it does not seem to show any where in my debug log for trigger "UnassigedConsultantRemoved". Is there a reason why it's not catching it?