1

I need some help and i am stuck and would love some assistance.

Problem : Campaigns are being deleted despite contact being assigned to the campaign.

I have started to write my test class. My Aim of test class is to add a contact record to a campaign which creates a campaign member and the status of the campaign member = 'Completed'.

If a user tries to delete a campaign and any of the associated campaign members have the status of "completed" then an error message would appear and say "You cannot delete the Campaign".( havent built the trigger yet)

However i am getting the following error message

"System.AssertException: Assertion Failed: Expected: true, Actual: false". Stack Trace Class.PreventionOfDeletingCampaigns.PreventionOfDeletingCampaigns: line 85, column 1.

This is my class

    @isTest
private class PreventionOfDeletingCampaigns
{
    private static testmethod void  PreventionOfDeletingCampaigns()
    {


/**
***************************************************************************************** 
*                     This is test data for Creating an Account
***************************************************************************************** 
*/ 

        Account acct = new Account
              (Name='Test');
        insert acct;

/**    
***************************************************************************************** 
*                     Querying RT Object for Contact object
***************************************************************************************** 
*/  

RecordType rt = [select id,developername,Name from RecordType where SobjectType='Contact' and developername='Employee' Limit 1];
RecordType rtc = [select id,developername,Name from RecordType where SobjectType='Campaign' and developername='Sales_Campaign' Limit 1];

/**
***************************************************************************************** 
*                     This is test data for Creating an Contact 
*                         Passing Contact RT Id From Query Above
***************************************************************************************** 
*/     
        Contact cont = new Contact (
               AccountId= acct.id,
               MailingCountry ='United States',
               recordTypeId=rt.id,
               LastName ='Test Name');
        insert cont;

  /**
***************************************************************************************** 
*                     This is test data for Creating an Campaign
***************************************************************************************** 
*/ 

        Campaign Camp= new Campaign(
               Name='Test',
               //StartDate='Date.newInstance(2017,2,2)',
               recordTypeId=rtc.id);
        insert Camp; 

/**
***************************************************************************************** 
*                     This is test data for Creating an Campaign Member 
***************************************************************************************** 
*/

        CampaignMember member = new CampaignMember(
                ContactId = cont.Id,
                Status = 'Completed',
                CampaignId = camp.Id);
        insert member ;

/**
***************************************************************************************** 
*  validation check
* 
***************************************************************************************** 
*/ 

CampaignMember[] CM = [SELECT Id, ContactId FROM CampaignMember WHERE ContactId = :cont.Id];
//system.assertEquals(cm.ContactId!=null); //Check if contact record is in Campaign
system.assertEquals(1,CM.size(),'Either no contact is assigned');


       Boolean expectedExceptionThrown = false;
         try
         {
            delete cont;
        } 
        catch (System.DMLException e)
        {
            expectedExceptionThrown = e.getMessage().contains('You cannot delete the Campaign.');
        }
        System.AssertEquals(true, expectedExceptionThrown);
    }

}

Really Looking for some assistance. Many thanks

1

This is exactly how unit tests are supposed to work when you follow TDD. You write a test that fails, then you write the code to make it pass. The test is written as if you want to prevent deletion of a Contact, so that is the object on which you should write your trigger. You can prevent Campaign deletion instead, or in addition. My interpretation is that you actually want to prevent only Campaign deletion.

// in your test
try
{
    delete camp; // not cont
}

// you need to add this trigger
trigger Campaign on Campaign (before delete)
{
    // validation logic
}

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.