+ Start a Discussion
Lee_CampbellLee_Campbell 

Cannot perform simple deletion test

Hi folks,

 

I wrote the following trigger to disallow users to delete Notes of any kind:

 

trigger CannotDeleteNotes on Note (before delete) {
    List<Note> atts = Trigger.old;
    for(Note a:atts) {
    a.addError('In the interest of audit history, the deleting of notes has been disabled.'); 
    }
}

 

 

And the following class to test the trigger:

 

@IsTest
private class Note_Trigger_Test{
    static testMethod void testDelete(){
           Note nots= new Note(Title='Test note');
           Test.startTest();
           Database.DeleteResult result = Database.delete (nots, false);
           Test.stopTest();
           
           System.assert(!result.isSuccess());
           }

}

 

 

But received the following error upon running the test:

 

System.ListException: Missing id at index 0  Class.Note_Trigger_Test.testDelete: line 6, column 1

 

And cannot for the life of me figure out why. The Trigger and the testMethod are both ridiculously simple, but no joy. Am I missing something fundamental?

 

Annoyingly, the Trigger works fine, but I can't deploy it without the code coverage...

 

Thanks for any help you can give,

Lee

Best Answer chosen by Admin (Salesforce Developers) 
jd123jd123

Hey 

you forgot to insert

 

 just include the line  

  insert nots;

 

Final code 

 

   

@IsTest
private class Note_Trigger_Test{
    static testMethod void testDelete(){
           Note nots= new Note(Title='Test note');
insert nots; Test.startTest(); Database.DeleteResult result = Database.delete (nots, false); Test.stopTest(); System.assert(!result.isSuccess()); } }

 

       

 

 

All Answers

jd123jd123

Hey 

you forgot to insert

 

 just include the line  

  insert nots;

 

Final code 

 

   

@IsTest
private class Note_Trigger_Test{
    static testMethod void testDelete(){
           Note nots= new Note(Title='Test note');
insert nots; Test.startTest(); Database.DeleteResult result = Database.delete (nots, false); Test.stopTest(); System.assert(!result.isSuccess()); } }

 

       

 

 

This was selected as the best answer
Lee_CampbellLee_Campbell

Yup. Just realised that, but thank you!

 

Also, when working with Notes, you need to create a parent of some kind (Opportunity, Case etc.) within your test, as Notes cannot exist on their own. Quite easily solved in the end.

 

Thanks