Find your content:

Search form

You are here

System.ListException: Missing ID at index 0


I am trying to add code coverage for the method below and keep getting this error. Basically it's a link that deletes the attachment on a table. The method is called from commandlink that passes the attachment id as a parameter. I am a noob and I don't know how to pass the id correctly in the test.


public String attachToDel {get;set;}

 public pagereference delAttach() 

        Attachment toDel=new Attachment(id=AttachToDel);

        Delete toDel;



List<Attachment> newAttachments=myPageCon.newAttachments;

            newAttachments[0].Name = 'test attachment';

            Blob bodyBlob=Blob.valueOf('Unit Test Attachment Body');

            System.assertEquals(1, myPagecon.getAttachments().size());
            myPageCon.delAttach();  <----This causes the error.

Section in the page where I pass the id to commandlink

<apex:commandLink value="{!$Label.Delete_Attachment}" action="{!delAttach}" >
      <apex:param name="AttachIdParam"  value="{!}" assignTo="!attachToDel}"/>

Thanks in advance.

Attribution to: PepeFloyd

Possible Suggestion/Solution #1

Its is a bit hard to answer correctly without knowing what's happening in your controller code. How does the commandlink populate the attachToDel property ? If a page parameter is used you could query for it and set it like this

ApexPages.currentPage().getParameters().put('parameterID', 'value');

Attribution to: Samuel De Rycke

Possible Suggestion/Solution #2

you're not setting the attachToDel param (which you should change to type Id btw).

System.assertEquals(1, myPagecon.getAttachments().size());
//Set the value in the controller
myPageCon.attachToDel = 'whatever Id you want to test deletion of';

Attribution to: Daniel Blackhall
This content is remixed from stackoverflow or stackexchange. Please visit

My Block Status

My Block Content