+ Start a Discussion
K@SK@S 

testclass error

Hi team,
I face this error
System.AssertException: Assertion Failed: Missing id at index: 0
I want to delete the item time through the error in.
please help on this
  static testMethod void Testtems(){
       User u2 = TestCaseHelpers.usrMethdLstWithProfileNameAndRoleName(2, 'Super System Admin', 'Nicole Team')[0];
        System.runAs(u2){
        
        list<OpportunityLineItem> oliList = new list<OpportunityLineItem>();
        //Create test line item
        OpportunityLineItem oliInst = new OpportunityLineItem();
        oliInst.OpportunityId = op.Id;
        oliInst.ServiceDate = startDate;
        oliInst.Quantity = 1;
        oliInst.UnitPrice = 1;
        oliInst.AB2__ABDropID__c = '-1';
        oliList.add(oliInst);
        insert oliList;
        try{
            delete oliList;
        }
        
        Catch(ListException e){
            System.assert(e.getMessage().contains('Error:You are trying to delete Opportunity Line Item(s) that have delivery; this function is not allowed here'),e.getMessage());       
        }
      
        }
      }
srlawr uksrlawr uk
I think the problem is your "olinst" opportunity line item isn't being inserted... and so when you try to delete the list, the item in it doesn't have an ID... (as it never made it to the database).

I am curious as to where op.id comes from at this line:
 
oliInst.OpportunityId = op.Id;

Obviously a line item needs an opportunity, but you arn't setting one up in this testMethod? That would cause the insert to fail, but I'm not sure why that doesn't kick off it's own exception when you try to insert the list.