+ Start a Discussion

trigger using a addError() and having a good test method for it.

I have a small trigger that will not allow a contact to be deleted if the contact is part of a active Ship_To for a entitlement. If the contact.Id is found to be in a active  record then prevent the deletion by calling the addError() to notify the user.


The problem is writting a test method. If my test method actually tries to delete the contact then, of course, the addError is called but then the test class fails because of the addError. If I change the test method not to try and delete the contact then, of course, that section of code is never execute... BUT the % of covered code is only 40%. Is there away to boost the %  ? I can add a bunch of extra lines like; Integer Count2 etc... to rais ethe %  but it seems there should be a better way.

trigger ValidateContact on Contact (before delete) {
// This trigger prevents the deletion of any Contact that
// has an associated ShipTo.
// Trigger.oldMap is a trigger context variable that maps Ids to every
// Contact that is about to be deleted.
// Create a list of associated ShipTo's:

Integer Count = 1;
for (Ship_To__c s : [select Contact__c, Contract_Number__c,Contact_Salutation_First_and_Last_Name__c, Entitlement_Status__c from Ship_To__c
where Contact__c in :Trigger.oldMap.keySet() ]) {

// For every ShipTo that is active, retrieve the related ShipTo
// from Trigger.oldMap and prevent it from being deleted by placing
// an error message on the Contact.
if(s.Entitlement_Status__c == 'Current' ||s.Entitlement_Status__c == 'In Progress' ||s.Entitlement_Status__c == 'Expired' )
'Cannot delete this contact: ' + s.Contact_Salutation_First_and_Last_Name__c + ' because they are specified as a ShipTo Contact in a Current, In Progress or Expired Asset or Entitlement. Contract = '+ s.Contract_Number__c + '. Number of Entitlements found for this Contact: ' + Count);


  Here's the Test method:

static testMethod void testValidateContactTrig()
        Account a = new Account(Name='Helen Account Trigger Test');
        insert a;
        Contact c = new Contact(accountid=a.Id, lastname='MyLastName', Status__c='Active');
        insert c;
       Opportunity  opp = new Opportunity();
        opp.CurrencyIsoCode = 'USD';   
        opp.AccountId = a.Id;
        opp.Type = 'Term';
        opp.Type_2__c = 'Tools';
        opp.Sector__c = 'Academic';
        opp.CloseDate = system.today();
        opp.StageName = 'Proposal';
        opp.ForecastCategoryName = 'Pipeline';
        opp.OwnerId = '00550000000n1k8';
        insert opp;
      Contracts__c  con = new Contracts__c();
        con.CurrencyIsoCode = 'USD';
        con.Opportunity__c = opp.Id;
        con.Status__c = 'In Progress'; 
        con.Contract_Type__c = 'Renewal'; 
        insert con;
      Asset_Entitlement__c asset = new Asset_Entitlement__c(); 
        asset.Account_del__c = opp.AccountId;             
        asset.Opportunity_Owner__c = opp.OwnerId;     
        asset.CurrencyIsoCode = opp.CurrencyIsoCode;      
        //asset.Product__c = pricebookMap.get(oppLines.PricebookEntryId).Product2.Id;                              
        asset.End_Date__c = system.today() - 10;       
        asset.Invoice_Date__c = system.today();       
        asset.Unit_Price__c = 12;       
        asset.Opportunity__c = opp.Id;
        asset.Start_Date__c = system.today();                  
        asset.Status__c = 'Expired';
        asset.Entitlement_Status__c = 'In Progress';
        asset.Term__c = 12;       
        asset.RecordTypeId = '012500000001C2f';
        asset.Contracts__c = con.Id;                
        asset.Quantity__c = 1;
        asset.DNR__c = false;
        asset.Renew_Flag__c = false; 
        asset.Part_Number__c = '2091Test-MNT';   //THS 3/5/2009
        asset.License_Type__c = 'Term'; 
        insert asset;
       Ship_To__c ST = new Ship_To__c();
         ST.Asset_Entitlement__c = asset.Id;
         ST.Contact__c = c.Id;
         ST.Contact_Touch_Count__c = null;
         insert ST;
        //delete c; // try to delete contact that

Best Answer chosen by Admin (Salesforce Developers) 
Thank you for the suggestion.

All Answers


keep the delete statement in the try and catch block as follows.



    delete c;


Catch(Exception e)




Thank you for the suggestion.
This was selected as the best answer