You need to sign in to do that
Don't have an account?
Martha V
I need help with a Delete Trigger Test
I have a delete trigger to stop deletion when certain conditions are true. The trigger is working fine. I've tested it in the sandbox. The test however is giving me some problems. I have looked in the community and used some of the suggestions given, however I haven't been able to make it work.
Here is my trigger & test
trigger whenCampaignDeleted on Campaign (before delete) {
List<Campaign> delCampaign = trigger.old;
Boolean dontDelete = False;
Boolean relCampaign = False;
for (Campaign eachCampaign : delCampaign)
{
//If the campaign is Adventure type check to see if there are campaign members
//with status "Attended" and if there is a related campaign: volunteer campaign
If (eachCampaign.RecordTypeId == '012o0000000xwhO')
{
If (eachCampaign.Related_Campaign__c != NULL)
relCampaign = True;
List<CampaignMember> cMs = [SELECT id, status FROM CampaignMember
WHERE CampaignId = :eachCampaign.Id];
For (CampaignMember eachCM : cMS)
{
If (eachCM.Status == 'Attended')
{dontDelete = True;}
}
If (dontDelete)
eachCampaign.adderror('Cannot delete a campaign that has members with status Attended. Their status must be changed first.');
If (relCampaign)
eachCampaign.adderror('Before deleting the campaign you must delete the volunteer campaign associated with it.');
}
Else
//If the campaign is a volunteer campaign then retreive the Adventure campaign related to it
//and delete the related campaign from the Adventure record
If (eachCampaign.RecordTypeId == '012o0000001AJaa')
{
if (eachCampaign.Related_Campaign__c != NULL)
{
Campaign relCampRecord = [SELECT ID, Related_Campaign__c FROM Campaign
WHERE Id = :eachCampaign.Related_Campaign__c];
relCampRecord.Related_Campaign__c = NULL;
Update relCampRecord;
}
}
}
}
—————— Test
After inserting a few records to test the trigger the following code performs the test (here I am just testing it with one record to single out the error, but it is set up to test a list of records).
try
{
delete myCampaigns[1];
System.assert(false);
}
catch (DMLException e)
{
// Check that the trigger interrupts the deletion
system.assertEquals('Before deleting the campaign you must delete the volunteer campaign associated with it.', e.getMessage());
}
When I run the test I get the following error
System.AssertException: Assertion Failed: Expected: Before deleting the campaign you must delete the volunteer campaign associated with it., Actual: Delete failed. First exception on row 0 with id 701n0000000DlUDAA0; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, Before deleting the campaign you must delete the volunteer campaign associated with it.: []
Can any one help? I am not sure how to change the System.assert to verify that the deletion is not done.
Thanks in advance for any help
Here is my trigger & test
trigger whenCampaignDeleted on Campaign (before delete) {
List<Campaign> delCampaign = trigger.old;
Boolean dontDelete = False;
Boolean relCampaign = False;
for (Campaign eachCampaign : delCampaign)
{
//If the campaign is Adventure type check to see if there are campaign members
//with status "Attended" and if there is a related campaign: volunteer campaign
If (eachCampaign.RecordTypeId == '012o0000000xwhO')
{
If (eachCampaign.Related_Campaign__c != NULL)
relCampaign = True;
List<CampaignMember> cMs = [SELECT id, status FROM CampaignMember
WHERE CampaignId = :eachCampaign.Id];
For (CampaignMember eachCM : cMS)
{
If (eachCM.Status == 'Attended')
{dontDelete = True;}
}
If (dontDelete)
eachCampaign.adderror('Cannot delete a campaign that has members with status Attended. Their status must be changed first.');
If (relCampaign)
eachCampaign.adderror('Before deleting the campaign you must delete the volunteer campaign associated with it.');
}
Else
//If the campaign is a volunteer campaign then retreive the Adventure campaign related to it
//and delete the related campaign from the Adventure record
If (eachCampaign.RecordTypeId == '012o0000001AJaa')
{
if (eachCampaign.Related_Campaign__c != NULL)
{
Campaign relCampRecord = [SELECT ID, Related_Campaign__c FROM Campaign
WHERE Id = :eachCampaign.Related_Campaign__c];
relCampRecord.Related_Campaign__c = NULL;
Update relCampRecord;
}
}
}
}
—————— Test
After inserting a few records to test the trigger the following code performs the test (here I am just testing it with one record to single out the error, but it is set up to test a list of records).
try
{
delete myCampaigns[1];
System.assert(false);
}
catch (DMLException e)
{
// Check that the trigger interrupts the deletion
system.assertEquals('Before deleting the campaign you must delete the volunteer campaign associated with it.', e.getMessage());
}
When I run the test I get the following error
System.AssertException: Assertion Failed: Expected: Before deleting the campaign you must delete the volunteer campaign associated with it., Actual: Delete failed. First exception on row 0 with id 701n0000000DlUDAA0; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, Before deleting the campaign you must delete the volunteer campaign associated with it.: []
Can any one help? I am not sure how to change the System.assert to verify that the deletion is not done.
Thanks in advance for any help
try
{
delete myCampaigns[1];
System.assert(false);
}
catch (DMLException e)
{
Boolean expectedExceptionThrown = e.getMessage().contains('Before deleting the campaign you must delete the volunteer campaign associated with ')) ? true : false;
System.AssertEquals(expectedExceptionThrown, true);
}
Let us know if this will help you
All Answers
try
{
delete myCampaigns[1];
System.assert(false);
}
catch (DMLException e)
{
Boolean expectedExceptionThrown = e.getMessage().contains('Before deleting the campaign you must delete the volunteer campaign associated with ')) ? true : false;
System.AssertEquals(expectedExceptionThrown, true);
}
Let us know if this will help you