Skip to main content
added 22 characters in body
Source Link
Eric
  • 54.4k
  • 11
  • 104
  • 196

In order to prevent delete transaction to be committed to DB, you need to addError to record, that should not be deleted

trigger PreventAccountDelete on Account (before delete) {

    for(Account acc : trigger.old){
        acc.addError('Account Cannot be deleted');
    }
}

to test this behavior run the following unit-test

@isTest
private class PreventAccountDeleteTest {
    
    @isTest
    static void testPreventAccDel(){
        Account accRecord = new Account(name='testAccount');
        insert accRecord;
        
        try{
            Test.startTest();
          
       delete accRecord;try{
            Test.stopTest();delete accRecord;
            System.assertEquals(false, 'An Error was expected');
        }catch(DMLexception e){
            System.assertEquals(true, e.getMessage().contains('Account Cannot be deleted'),'Account Cannot be deleted');                       
        }
        
        Test.stopTest();

    }
}

In order to prevent delete transaction to be committed to DB, you need to addError to record, that should not be deleted

trigger PreventAccountDelete on Account (before delete) {

    for(Account acc : trigger.old){
        acc.addError('Account Cannot be deleted');
    }
}

to test this behavior run the following unit-test

@isTest
private class PreventAccountDeleteTest {
    
    @isTest
    static void testPreventAccDel(){
        Account accRecord = new Account(name='testAccount');
        insert accRecord;
        
        try{
            Test.startTest();
                delete accRecord;
            Test.stopTest();
            System.assertEquals(false, 'An Error was expected');
        }catch(DMLexception e){
            System.assertEquals(true, e.getMessage().contains('Account Cannot be deleted'),'Account Cannot be deleted');                       
        }
    }
}

In order to prevent delete transaction to be committed to DB, you need to addError to record, that should not be deleted

trigger PreventAccountDelete on Account (before delete) {

    for(Account acc : trigger.old){
        acc.addError('Account Cannot be deleted');
    }
}

to test this behavior run the following unit-test

@isTest
private class PreventAccountDeleteTest {
    
    @isTest
    static void testPreventAccDel(){
        Account accRecord = new Account(name='testAccount');
        insert accRecord;
            
        Test.startTest();
         
        try{
            delete accRecord;
            System.assertEquals(false, 'An Error was expected');
        }catch(DMLexception e){
            System.assertEquals(true, e.getMessage().contains('Account Cannot be deleted'),'Account Cannot be deleted');                       
        }
        
        Test.stopTest();

    }
}
added 4 characters in body
Source Link

In order to prevent delete transaction to be committed to DB, you need to addError to record, that should not be deleted

trigger PreventAccountDelete on Account (before delete) {

    for(Account acc : trigger.old){
        acc.addError('Account Cannot be deleted');
    }
}

to test this behavior run the following unit-test

@isTest
private class PreventAccountDeleteTest {
    
    @isTest
    static void testPreventAccDel(){
        Account accRecord = new Account(name='testAccount');
        insert accRecord;
        
        try{
            Test.startTest();
                delete accRecord;
            Test.stopTest();
            System.assertEquals(false, 'An Error was expected');
        }catch(DMLexception e){
            System.assertEquals(true, e.getMessage().contains('Account Cannot be deleted'),'Account Cannot be deleted');                       
        }
        System.assertEquals(false, 'An Error was expected');
    }
}

In order to prevent delete transaction to be committed to DB, you need to addError to record, that should not be deleted

trigger PreventAccountDelete on Account (before delete) {

    for(Account acc : trigger.old){
        acc.addError('Account Cannot be deleted');
    }
}

to test this behavior run the following unit-test

@isTest
private class PreventAccountDeleteTest {
    
    @isTest
    static void testPreventAccDel(){
        Account accRecord = new Account(name='testAccount');
        insert accRecord;
        
        try{
            Test.startTest();
                delete accRecord;
            Test.stopTest();
        }catch(DMLexception e){
            System.assertEquals(true, e.getMessage().contains('Account Cannot be deleted'),'Account Cannot be deleted');                       
        }
        System.assertEquals(false, 'An Error was expected');
    }
}

In order to prevent delete transaction to be committed to DB, you need to addError to record, that should not be deleted

trigger PreventAccountDelete on Account (before delete) {

    for(Account acc : trigger.old){
        acc.addError('Account Cannot be deleted');
    }
}

to test this behavior run the following unit-test

@isTest
private class PreventAccountDeleteTest {
    
    @isTest
    static void testPreventAccDel(){
        Account accRecord = new Account(name='testAccount');
        insert accRecord;
        
        try{
            Test.startTest();
                delete accRecord;
            Test.stopTest();
            System.assertEquals(false, 'An Error was expected');
        }catch(DMLexception e){
            System.assertEquals(true, e.getMessage().contains('Account Cannot be deleted'),'Account Cannot be deleted');                       
        }
    }
}
added 66 characters in body
Source Link

In order to prevent delete transaction to be committed to DB, you need to addError to record, that should not be deleted

trigger PreventAccountDelete on Account (before delete) {

    for(Account acc : trigger.old){
        acc.addError('Account Cannot be deleted');
    }
}

to test this behavior run the following unit-test

@isTest
private class PreventAccountDeleteTest {
    
    @isTest
    static void testPreventAccDel(){
        Account accRecord = new Account(name='testAccount');
        insert accRecord;
        
        try{
            Test.startTest();
                delete accRecord;
            Test.stopTest();
        }catch(DMLexception e){
            System.assertEquals(true, e.getMessage().contains('Account Cannot be deleted'),'Account Cannot be deleted');                       
        }
        System.assertEquals(false, 'An Error was expected');
    }
}

In order to prevent delete transaction to be committed to DB, you need to addError to record, that should not be deleted

trigger PreventAccountDelete on Account (before delete) {

    for(Account acc : trigger.old){
        acc.addError('Account Cannot be deleted');
    }
}

to test this behavior run the following unit-test

@isTest
private class PreventAccountDeleteTest {
    
    @isTest
    static void testPreventAccDel(){
        Account accRecord = new Account(name='testAccount');
        insert accRecord;
        
        try{
            Test.startTest();
                delete accRecord;
            Test.stopTest();
        }catch(DMLexception e){
            System.assertEquals(true, e.getMessage().contains('Account Cannot be deleted'),'Account Cannot be deleted');                       
        }
    }
}

In order to prevent delete transaction to be committed to DB, you need to addError to record, that should not be deleted

trigger PreventAccountDelete on Account (before delete) {

    for(Account acc : trigger.old){
        acc.addError('Account Cannot be deleted');
    }
}

to test this behavior run the following unit-test

@isTest
private class PreventAccountDeleteTest {
    
    @isTest
    static void testPreventAccDel(){
        Account accRecord = new Account(name='testAccount');
        insert accRecord;
        
        try{
            Test.startTest();
                delete accRecord;
            Test.stopTest();
        }catch(DMLexception e){
            System.assertEquals(true, e.getMessage().contains('Account Cannot be deleted'),'Account Cannot be deleted');                       
        }
        System.assertEquals(false, 'An Error was expected');
    }
}
added 12 characters in body
Source Link
Loading
edited body
Source Link
Loading
Source Link
Loading