+ Start a Discussion

TEST Method issue

This trigger ensures that contacts where contact.contact_type__c = 'Client' can not be deleted. The test case saved and compiled correctly.  When I "run tests" in this test method, I don;t receieve any failures for this test method.


Then I went to my trigger and tried to deploy it and it says  0% covered at leas 1% needs to be tested .  Line 3,5,7 and 9 not covered?

// This trigger ensures that client contacts are not deleted in Salesforce. This is a requirement to interface opportunity records into Oracle.
trigger ContactDeleteTrigger on Contact (before delete)
    if (Trigger.isDelete) 
        for (Contact c : Trigger.old) 
            if (c.contact_type__c == 'Client') 
                c.contact_type__c.addError('You can not delete a client contact. Please contact your administrator');







private class delete_contacts_test
    static testMethod void deleteContact() {
        //Create an Account
        Account A1 = new Account( Name = 'TestAccount', Type =  'Prospect', Industry = 'Banking', Physical_Street__c = 'Test Street',Physical_City__c = 'Los Angeles', Physical_State__c = 'CA', Physical_Zip_Postal_Code__c = '90017', Physical_Country__c = 'USA', Phone = '(888) 999-1234'  );
        insert A1;
        //Create a Contact, associating it with the above account
        Contact C1 = new Contact(
            AccountId = A1.id,
            contact_type__c = 'Client',
            mailing_country__c = 'USA',
            mailing_street__c = 'Wilshire',
            mailing_state__c= 'CA',
            mailing_Postal_Code__c ='90017',
            mailing_city__c = 'LA',
            title = 'janitor',
            functional_area__c = 'Finance',
            title_level__c = 'VP',
            Direct_Mail_Status__c ='Do Not Send',
            leadsource = 'Direct Mail'          
        insert C1;
    //Deleting the above contact causes the trigger to fire
        try { delete C1; }

        catch (System.DMLException e) {
    //verify that you received the correct error message
     //       System.assert(e.getMessage().contains('first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, You can\'t delete this record!'),
     //   e.getMessage());     

did you try by putting debug statement after insert C1. just to make sure it is inserting virtually and returning Id. Or else you can simply put

Contact c = [select Id from Contact where contact_type__c = 'Client' limit 1];

delete c;

in your test method. As I did not see any thing wrong you are doing.


When I placed the contact insert and delete statement it actually never inserts it because the trigger add error shows up in the debug log after i run tests


System.DMLexception:Delete failed ('You can not delete a client contact. Please contact your administrator');