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

Write test class for before delete

I have the following trigger, I have absolutely no idea how to write a test class to move this into production.  Not sure where to start, I am not a developer just needed this validation.. Starting to learn though.. Thanks in advance for any help.



trigger OppVIPnondelete on Opportunity (before delete) {
    for(Opportunity opp : trigger.Old){
        if(opp.Andover_VIP__c == true){
            opp.addError('This opportunity cannot be deleted, it has a VIP visit attached, if you have any questions please contact Joanne Donato');


You placed a new question ? You should have PM me ;)


private class OppDelTest {
    static testMethod void testDel() {
        Opportunity opp = new Opportunity(
            Name = 'Test',
            CloseDate =,
            StageName = 'Prospecting');
           //add all other required fields
        insert opp;
        try {
            delete opp;
        } catch (Exception ex) {}



Thanks sorry next time I will.. So I created the Apex Class for testing then I went to deploy and setup a outbound change set with both the trigger and the test class.  I then uploaded the the change set to production, then in production I ran a validate on the complete change set.  Is that correct?  When I ran the validate I recieved the following error:  Thanks again. The trigger works perfectly in dev.. Just moving it over is not very easy.. Thanks again!


OppDelTest.testDel()Class91Failure Message: "System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, opportunityName: execution of BeforeInsert caused by: System.NullPointerException: Attempt to de-reference a null object Trigger.opportunityName: line 105, column 1: []", Failure Stack Trace: "Clas...

I guess you have another trigger in place that is causing trouble, You need to fix that


Trigger Name : opportunityName


Check the above trigger. And now I guess you can do the fix ;)