+ Start a Discussion
Tina DamTina Dam 

Error Message on Test Class System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, UpdateElevateSurveyResponded: execution of AfterInsert

Hi,

I'm very new to coding and am getting an error message every time I run my test class.

I wrote a simple trigger for a custom object "TIMBA_Survey_Summaries" that has a lookup on the Contact object. The trigger updates the Contact checkbox field "Elevate_Survey_Responded__c" to TRUE when a new Timba Survey Summary record with "TIMBASURVEYS__Complete_Survey_Name__c == 'Elevate Survey'" is inserted into the system.

Here is the trigger:

// Update Contact field: "Elevate_Survey_Responded__c" to TRUE when a new TIMBASURVEYS__Survey_Summary__c record is inserted

trigger UpdateElevateSurveyResponded on TIMBASURVEYS__Survey_Summary__c (after insert) {

// Will store related Contact record ID

map< id, contact > contacts = new map< id, contact >();

// Create trigger for new or selected TIMBASURVEYS__Survey_Summary__c

for(TIMBASURVEYS__Survey_Summary__c record:trigger.new)       

if(record.TIMBASURVEYS__Complete_Survey_Name__c == 'Elevate Survey')
if(record.TIMBASURVEYS__Complete_Survey_Name__c != null)

     // Update checkbox field on the parent Contact record to TRUE   

     contacts.put(record.TIMBASURVEYS__RelatedContact__c, new contact(id=record.TIMBASURVEYS__RelatedContact__c, Elevate_Survey_Responded__c = TRUE));     



update contacts.values();

This is the test class:

@isTest
private class UpdateElevateSurveyResponded {

    static testmethod void UpdateElevateSurveyResponded() {
    User u = [SELECT Id FROM User WHERE Alias = 'kdam'];
        // set up some test data       
        Contact c1 = new Contact(
            LastName='Dam'
            );
        insert c1;
                    
        // start the test execution context
        Test.startTest();

        // create a survey that runs through trigger
        System.runAs(u) {
        TIMBASURVEYS__Survey_Summary__c survey1 = new TIMBASURVEYS__Survey_Summary__c(
            TIMBASURVEYS__Complete_Survey_Name__c='Elevate Survey'
            );
        insert survey1; 

        System.assertEquals('Elevate Survey',survey1.TIMBASURVEYS__Complete_Survey_Name__c);  
       
        //Bulk validation  
        System.debug('Inserting 200 survey records... (bulk validation)');
       
        List<TIMBASURVEYS__Survey_Summary__c> survey2 = new List<TIMBASURVEYS__Survey_Summary__c>();
        for(integer i=0; i<200; i++) {
            survey2.add( new TIMBASURVEYS__Survey_Summary__c(TIMBASURVEYS__Complete_Survey_Name__c='Elevate Survey') );
        }
        insert survey2;
      
                }//end RunAs(u1) 
               
        // stop the test
        Test.stopTest();
        }
    }


This is the error message I keep getting:

System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, UpdateElevateSurveyResponded: execution of AfterInsert

caused by: System.DmlException: Update failed. First exception on row 0; first error: MISSING_ARGUMENT, Id not specified in an update call: []

Trigger.UpdateElevateSurveyResponded: line 22, column 1: []

any help would be greatly appreciated. thanks in advance
Ivaylo BalabanovIvaylo Balabanov
Hi,

You are not able to edit records that way. You have to query them and after that edit them.
Something like that
List<Contact> cont=[Select Elevate_Survey_Responded__c from Contact where id in:contact.KeySet()];
for (Contact c:cont){
     c.Elevate_Survey_Responded__c=TRUE;
}
update cont;
Ivaylo BalabanovIvaylo Balabanov
Hi,
That is not exacly acurate that you are not able to edit the records but i usualy query te rcords which i update.
You could try.
Tina DamTina Dam
Thanks, it's probably something very simple...but I tried the above and it's giving me this error message when saving:

 "Error: Comple Error: Variable does not exist: cont at line 18 Column 16

User-added image