+ Start a Discussion

Test Errors in trying to create a trigger that keys off a managed package record

I've created a simple trigger that marks a checkbox (True) on a contact record, when a new child record is created that also has a specific checkbox checked.  The child records are created through an AppExchange packed App, in other words it's not possible to click a New button and create one of these child records through the UI.

My trigger worked just fine, compiled a-okay.

In creating a Test class however, I'm receiving the following error:

System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, ManagedPackageObject: Execution AfterInsert

I can't really test if my trigger works in a Sandbox given that it's a manage package that connects to and communicates with an external web app.  Any ideas for how I can get my test to insert the new record I need to get the test to run correctly?

Thanks in advance.


For reference:


trigger ONEN_Contact_MarkBadEmail on vr__VR_Email_History_Contact__c (after insert, after update) {

 // every time we a VR Email History is added to the contact record, or updated from Vertical Reponse
 // and the VR Email History Contact has a bounced checkbox marked as true, update the contact field
 // Bad Email Addresss to True.
 list<Id> ContactIdsToUpdate = new list<Id>();
 vr__VR_Email_History_Contact__c BouncedEmail = new vr__VR_Email_History_Contact__c();
 list<Contact> ContactsToUpdate = new list<Contact>();
 // create a list of Contact IDs that have VR Email History record with Bounced checked
 for (vr__VR_Email_History_Contact__c thisEmailRecord : trigger.new) {
  if ( thisEmailRecord.vr__Bounced__c = True ) {
   ContactIdsToUpdate.add (thisEmailRecord.vr__Contact__c);
 if ( ContactIdsToUpdate.size() > 0 ) {
  for (Id BouncedContacts : ContactIdsToUpdate) {
   Contact thisContact = new Contact (
   Id = BouncedContacts, 
   Bad_Email_Address__c = True
   ContactsToUpdate.add (thisContact);
  insert ContactsToUpdate;

public class ONEN_Test_Contact_MarkBadEmail {

 static testMethod void Contact_MarkedAsBadEmail () {
  Contact testContact = new Contact (
   LastName = 'Schmoe',
   Bad_Email_Address__c = False
  insert testContact;
  vr__VR_Email_History_Contact__c testEmailHistory = new vr__VR_Email_History_Contact__c (
   Name = 'Test Campaign Email',
   vr__Bounced__c = False,
   vr__Contact__c = testContact.Id
  insert testEmailHistory;
  testContact = [select id, Bad_Email_Address__c from Contact where id=:testContact.Id];
  system.assertEquals(testContact.Bad_Email_Address__c, True);



Message Edited by DManelski on 06-20-2008 12:36 PM

Message Edited by DManelski on 06-20-2008 12:40 PM
I stand corrected, or not perhaps.  I did locate a New button and create a new child record in my sandbox but received an error on creation:

Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger ONEN_Contact_MarkBadEmail caused an unexpected exception, contact your administrator: ONEN_Contact_MarkBadEmail: execution of AfterInsert caused by: System.Exception: Record is read-only: Trigger.ONEN_Contact_MarkBadEmail: line 14, column 14

You can't modify the records in the trigger's array of affected items in an "after" trigger; the data is already saved (but not committed) and can't be modified. Instead, use a "before" trigger and modify the records. You can't use update/insert/etc because that would be an infinite loop, but any changes you make to any record in the Trigger.New array will take on those values.
That did the trick, thank you.  In addition, I changed my trigger to update the contacts rather than insert new ones, works perfectly now. 

Message Edited by DManelski on 06-20-2008 05:40 PM