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
bikla78bikla78 

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();
}
}

Message Edited by bikla78 on 08-18-2009 11:01 PM
jpwagnerjpwagner

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.

 

 

trigger UnassignedConsultantRemoved on OpportunityLineItem (before delete) {
Set<Id> pbeSet = new Set<Id>();

for (OpportunityLineItem OppLineItem : Trigger.old)
{
if (opplineitem.opportunityid =='006R0000003rc72IAA')
{
pbeSet.add(opplineitem.pricebookentryid);
}
}
for(Pricebookentry pb:[select id, name, lastmodifieddate from pricebookentry where id IN: pbeSet])
{
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 });
}
}

 

 

ahab1372ahab1372

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

  1. insert the Account, then
  2. use the AccountID to create the opportunity
  3. insert the opportunity
  4. use the opportunityID to create the LineItem
  5. insert the lineitem

 

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.

 

bikla78bikla78

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

 

 

 

 

 

 

@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' );



try {
insert acc;
}
catch(DmlException acc)
{
System.debug(acc.getMessage());
}
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 );

try {
insert opp;
}
catch(DmlException opp)
{
System.debug(opp.getMessage());
}
OpportunityLineItem olt1 = new OpportunityLineItem(Opportunityid = opp.id,Unitprice= 180.00, Quantity = 1);

try {
insert olt1;
}
catch(DmlException olt1)
{
System.debug(olt1.getMessage());
}

test.starttest();

ID newid = olt1.id;


delete olt1;
test.stoptest();
}
}

 


Message Edited by bikla78 on 08-21-2009 09:15 AM
ahab1372ahab1372
check you Debug Log for the insert operation of olt1. I think it could not be inserted because olt1 does not have a pricebookEntryID which I believe is required. You don't get an error message for that because you put all insert operations in try-catch (btw I wouldn't do that in a test method - if insert fails I want to know right away).
bikla78bikla78

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

 

 

@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' );



try {
insert acc;
}
catch(DmlException e)
{
System.debug(e.getMessage());
}
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 );

try {
insert opp;
}
catch(DmlException e)
{
System.debug(e.getMessage());
}
PriceBookentry pbe = new Pricebookentry(pricebook2id = '01s300000000EfuAAE', product2id = '01t300000001jkIAAQ', isactive = TRUE, UnitPrice= 180.00);

try{
insert pbe;
}
catch(DmlException e)
{
System.debug(e.getMessage());
}


OpportunityLineItem olt1 = new OpportunityLineItem(Opportunityid = opp.id,pricebookentryid = pbe.id, Unitprice= 180.00, Quantity = 1);

try {
insert olt1;
}
catch(DmlException e)
{
System.debug(e.getMessage());
}

test.starttest();

ID newid = olt1.id;

delete olt1;
test.stoptest();
}
}

 

ahab1372ahab1372
can you insert a System.Debug after every insert? Then you will see if the olt1 actually has an ID. What does the debug log say?
bikla78bikla78

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?

 

      try {    
insert acc;
}
catch(DmlException e)
{
System.debug(e.getMessage());
}
Message Edited by bikla78 on 08-26-2009 12:34 AM