Home SALESFORCEAPEX Salesforce Apex Test class for an Opportunity record delete Trigger

Salesforce Apex Test class for an Opportunity record delete Trigger

Use Case:
-Test Class coverage for the Opportunity Record delete trigger
-Test Class coverage to AddError
-Test Class coverage to Non System admin profile

Main Apex Trigger:

Not allowed to delete the opportunity records if its closed

trigger OpportunityDeleteTrigger on Opportunity ( before delete ) {
    String name = [SELECT Name FROM Profile WHERE Id =:UserInfo.getProfileId()].Name;
    if( name != 'System Administrator' ) {
        if( Trigger.isBefore ) {
            if( Trigger.isDelete ) {
                for( Opportunity objOpportunity : Trigger.old ) {
                    if( objOpportunity.IsClosed == true ) {
                        objOpportunity.addError( 'This opportunity cannot be deleted becase it has been closed.  Please contact Sales Operations.' );


Test Class:

1. Create apex class with @isTest anotation.
2. Create a user record with non admin users
3. Use System.runAs to execute your test class as per the non admin profile
4. Create test opportunity record with appropriate StageName value as per the Closed Won field to make sure the IsClosed value as True
5. Delete the created opporutnity
6. Use system.assertEquals to verify the IsClosed value.

 * This Apex Test Class Created to cover the Opportunity Trigger
 *  - Created Test User with non system admin profile
 *  - Used Syste.runAs to execute the test user
 *  - Created Opportunity Record
 *  - Deleted Opporutnity
 *  - Check AddError validation as per the Trigger
 * @author  Sakthivel Madesh
 * @date    Sep 25, 2019
public class OpportunityDeleteTriggerTestClass {
    static testMethod void createOppMethod() {
        try {            
                Profile prof = [SELECT Id FROM Profile WHERE Name = 'Standard User']; //get a profile Id
                User testUser = new User(Alias = 'TDemo', Email = '[email protected]', EmailEncodingKey = 'ISO-8859-1', FirstName = 'Demo', LanguageLocaleKey = 'en_US', LastName = 'User', LocaleSidKey = 'en_US', ProfileId = prof.Id, TimeZoneSidKey = 'America/Denver', Username = '[email protected]'); 
                insert testUser;
                System.runAs(testUser) {
                    Opportunity createOpp = new Opportunity();
                    createOpp.Name='test opp';
                    createOpp.StageName='Closed Won';
                    createOpp.Probability = 95;
                    insert createOpp;
                    Delete createOpp;
                    //Apex Trigger addError Checking
                    Opportunity testResult = [SELECT Id, IsClosed FROM Opportunity WHERE Id =: createOpp.Id];
                    system.assertEquals(true, testResult.IsClosed);
        } catch(Exception e) {


Source to study about test class:

Get Started with Apex Unit Tests from Trailhead – https://trailhead.salesforce.com/en/content/learn/modules/apex_testing/apex_testing_intro

About Test Class from Apex Developer Guide- https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_qs_test.htm

Different types of methods for Test Class – https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_test.htm

You may also like

Leave a Comment