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
James McVicar 6James McVicar 6 

Trying to stop deletion of records on a custom object

We have a custom object called FMS Work Orders. Our users must have the "modfy all" permission, but I do NOT want them to delete Work Order records. I have followed this example to set up a trigger which stops the deletion of a record. The problem is the test class runs, cannot delete the record, which is the desired behaviour, but treats the test as a fail. Can anyone help pont out what's wrong with the test class?

Trigger:
trigger PreventWODeletion on FMS_Work_order__c (before delete) {
    for(FMS_Work_order__c FWO : TRIGGER.OLD){FWO.ADDERROR('Work Order cannot be deleted');}
}

Test Class
@isTest 
private class testPreventWODeletion
{
    
static testmethod void PreventWODeletion1() { 
    
 FMS_Work_order__c FWORecord = new FMS_Work_order__c(); 
 FWORecord.Issue_Description__c='Test123';
 FWORecord.Case_number__c='5006E000004jB49QAE';
 insert FWORecord; 

 try{ 
 delete FWORecord;
 }
    
 catch(DMLException e){ 
 system.assert(e.getMessage().contains('Work Order Cannot be deleted'),'Work Order Cannot be deleted'); 
 } 
    
 } 
}


 
Best Answer chosen by James McVicar 6
GhanshyamChoudhariGhanshyamChoudhari
system.assert(e.getMessage().contains('Work Order cannot be deleted'),true);

 

All Answers

devedeve
Hi James,

Can you please provide error you are getting
Narender Singh(Nads)Narender Singh(Nads)
Hi James,
Modify the try block of your code as this:
try{ 
 
 delete FWORecord;
 System.assert(false);


 }

Let me know if it helps.
Thanks!​ 
GhanshyamChoudhariGhanshyamChoudhari
system.assert(e.getMessage().contains('Work Order cannot be deleted'),true);

 
This was selected as the best answer
James McVicar 6James McVicar 6
Hi GhanshyamChoudhari,

Thank you, worked first time!