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
Mikey999Mikey999 

Need help creating a trigger to NOT delete an Opportunity if a field is checked on the OPP

First off, I am very new to development and or SFDC dev, so please excuse my ignorance.  I need some help creating a trigger that will NOT allow a user to delete an opportunity if one field Andover_VIP__c (which is a checkbox) is checked.  Any help is greatly appreciated.  Thanks in advance..   I thought it would be an easy validation rule, but from research the validation rule can't be used for this.

 

Thanks again.

Best Answer chosen by Admin (Salesforce Developers) 
Avidev9Avidev9
trigger OppTrigger on Opportunity (before delete) {
    for(Opportunity opp : trigger.Old){
        if(opp.Andover_VIP__c == true){
            opp.addError('Your Custom Error Message Here');
        }
    }
}

 This trigger should do the work

All Answers

Avidev9Avidev9
trigger OppTrigger on Opportunity (before delete) {
    for(Opportunity opp : trigger.Old){
        if(opp.Andover_VIP__c == true){
            opp.addError('Your Custom Error Message Here');
        }
    }
}

 This trigger should do the work

This was selected as the best answer
Mikey999Mikey999

Awesome worked great in Dev, now I have to figure out how to get it into production.. Thanks!!

Mikey999Mikey999

Moved into production.. Again thanks for your help, I just needed something to get started, I am assuming I will be dangerous now LOL.. (not really since I have to support all of SFDC now).

Mikey999Mikey999

Hmm I have tried this trigger exactly like you wrote, and a few different ways to write it.. All ways work in Sandbox but when I try to deploy it the validation keeps failing says 0%, at least 1% test coverage is required any idea?  Thanks again!