0

When I run this test unit, it fails and the error message is System.DmlException: Delete failed. First exception on row 0 with id 0064D000004yFkTQAU; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, Opportunities can not be deleted: [] Can anyone help, should I use a try,catch exception?

trigger OpportunityTrigger on Opportunity (after delete, after insert, after undelete, after update, before delete, before insert, before update) {
    try {
        map<Id, Opportunity> sendVOCS = new map<Id, Opportunity>();
        map<id, Opportunity> stageChanged = new map<Id, Opportunity>();

        if (trigger.isAfter) {
            system.debug('OpportunityTrigger isAfter');
            if (trigger.isUpdate) {
                system.debug('OpportunityTrigger isAfter isUpdate');
                for (Opportunity record : trigger.newMap.Values()) {
                    Opportunity oldMapMatch = trigger.oldMap.get(record.Id);
                    if (record.StageName != oldMapMatch.StageName) {
                        // Voice of Customer Survey criteria
                        if (record.StageName == 'Closed - Awarded' || record.StageName == 'Closed - Lost') {
                            sendVOCS.put(record.Id, record);
                        }
                        // add newMap opportunity to a id/opp map
                        stageChanged.put(record.Id, record);
                    }
                }
                // send voice of customer surveys if they should be - criteria is above
                if (sendVOCS.keySet().Size() > 0) {
                    SendMail sendMail = new SendMail();
                    sendMail.SendVoiceOfCustomerSurvey(sendVOCS);
                    system.debug('Send Voice of Customer Surveys');
                }
                if (stageChanged.keySet().Size() > 0) {
                    StatusAndStageLogging statusAndStageLogging = new StatusAndStageLogging();
                    statusAndStageLogging.logOpportunityStage(stageChanged, trigger.oldMap);
                    system.debug('Log Opportunity Stage');
                }

             if(Trigger.isDelete) { // verify for delete operation
                for (Opportunity opp : Trigger.Old) {
                  opp.addError ('Opportunities can not be deleted');
               }
             }            
            }
        }
    }
    catch (exception e){
        ErrorHandler error = new ErrorHandler();
        error.logError(e);
    }
}


@isTest
private class Test_OpportunityTrigger {

    static testMethod void stageChanged() {
        Market_Segment__c mk = new Market_Segment__c(Name = 'Automotive');
        insert mk;
        Account account = TestUtils.CreateAccounts(1)[0];
        account.Market_Segment__c = 'Automotive';
        insert account;
        Opportunity opportunity = TestUtils.CreateOpportunities(1)[0];
        opportunity.AccountId = account.Id;
        opportunity.Amount = 2000000;
        insert opportunity;
        opportunity.StageName = 'Closed - Awarded';
        update opportunity;
    }
@isTest
      static void PreventOpptyDelete(){
      Market_Segment__c mk = new Market_Segment__c(Name = 'Automotive');
      insert mk;
      Account account = TestUtils.CreateAccounts(1)[0];
      account.Market_Segment__c = 'Automotive';
      insert account;
      Opportunity opportunity = TestUtils.CreateOpportunities(1)[0];
      opportunity.AccountId = account.Id;
      opportunity.Amount = 2000000;
      insert opportunity;

      delete opportunity;
      }

}
1

should I use a try,catch exception?

When you're trying to test code that throws an exception, or causes an exception to be thrown through Validation Rules or addError(), you should catch that exception and make assertions to validate that the right exception was thrown for the right reason.

Here's the way I like to do that.

Boolean caught = false;
try {
    delete opportunity;
} catch (DMLException e) {
    System.assert(e.getMessage().contains('Opportunities can not be deleted'), 'exception message');
    caught = true;
}

System.assertEquals(true, caught, 'expected exception thrown');

Your test will then allow any other exceptions to go through and produce a failure.

1

This chunk of code of your trigger doesn't allow you to delete the opportunity.

 if(Trigger.isDelete) { // verify for delete operation
    for (Opportunity opp : Trigger.Old) {
    opp.addError ('Opportunities can not be deleted');
    }
 }
  • No validation rule, there is a Opportunity Trigger with that error message on – Alex van der Merwe Mar 18 at 13:52
  • so what does this trigger do and why is your second test method called "PreventOpptyDelete()"? Which action does prevent opp from removing? – Oleh Berehovskyi Mar 18 at 14:01
  • I edited the original post to show the trigger – Alex van der Merwe Mar 18 at 14:06
  • I've updated the answer as well – Oleh Berehovskyi Mar 18 at 14:14
  • Thanks, I am trying to get my unit test to pass instead of fail. And I think method 2 in the test unit is causing the issue – Alex van der Merwe Mar 18 at 14:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.