function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Nitzan MarinovNitzan Marinov 

Test Class failing test

I suspect that this test to the EventTrigger has not been working for a while but now it's stopping me from deploying some changes that seem to be related.

I think the problem is with the insert to event but I can't work out how to fix it.

@isTest
private class TESTCLASS_EventTrigger {
static testMethod void validateEventTrigger() {

   datetime myDateTime =
   datetime.newInstance(2012, 1, 1);
   
Clients__c c = TestUtilities.insertClient();
        insert c;
Event e = new Event(Subject='Test Class Call', WhatID ='a0E54000000Ct6e',
                    DurationInMinutes=30, StartDateTime=myDateTime, EndDateTime=myDateTime);
// Insert Client & Event
insert c;
insert e;

// Retrieve the new Event
e = [SELECT ID, Calendar_Title__c FROM Event WHERE Subject = 'Test Class Call'];
System.debug('Calendar_Title__c: ' + e.Calendar_Title__c);

// Test that the trigger correctly updated the price
System.assertEquals('Camilla Ferdinand: Test Class Call', e.Calendar_Title__c);
}
}

Error: 14:56:55:445 EXCEPTION_THROWN [13]|System.DmlException: Insert failed. First exception on row 0 with id a0E540000004i1TEAQ; first error: INVALID_FIELD_FOR_INSERT_UPDATE, cannot specify Id in an insert call: [Id]

Not quite sure what information I need to supply apart from the code and the error information. So please let me know if you need more information.

Many thanks
Best Answer chosen by Nitzan Marinov
Alexander TsitsuraAlexander Tsitsura
Hi Nitzan,

it's second insert Clients__c, that is failing.  After the first Insert Clients__c - you got an ID for the record.

As a common practice, if your question is answered, please choose 1 best answer. 
But you can give every answer a thumb up if that answer is helpful to you.

Thanks,
Alex
 

All Answers

Alexander TsitsuraAlexander Tsitsura
Hi Nitzan,

it's second insert Clients__c, that is failing.  After the first Insert Clients__c - you got an ID for the record.

As a common practice, if your question is answered, please choose 1 best answer. 
But you can give every answer a thumb up if that answer is helpful to you.

Thanks,
Alex
 
This was selected as the best answer
Nitzan MarinovNitzan Marinov

Thanks Alex. It was obvious after you pointed it out :-)

However,

I now get the following error:
15:43:17:411 EXCEPTION_THROWN [13]|System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, EventTrigger: execution of BeforeInsert

Here's the current code:
@isTest
private class TESTCLASS_EventTrigger {
static testMethod void validateEventTrigger() {

   datetime myDateTime =
   datetime.newInstance(2012, 1, 1);
   
Clients__c c = TestUtilities.insertClient();
Event e = new Event(Subject='Test Class Call', WhatID ='a0EC000000AkWqD',
                    DurationInMinutes=30, StartDateTime=myDateTime);
// Insert Client & Event
insert c;
insert e;

// Retrieve the new Event
e = [SELECT ID, Calendar_Title__c FROM Event WHERE Subject = 'Test Class Call'];
System.debug('Calendar_Title__c: ' + e.Calendar_Title__c);

// Test that the trigger correctly updated the price
System.assertEquals('Camilla Ferdinand: Test Class Call', e.Calendar_Title__c);
}
}

And here is the trigger code (which I haven't touched):
trigger EventTrigger on Event (before insert, before update) {

    Event[] eventToUpdate = trigger.new;
    Clients__c clientInfo = new Clients__c();
    Project__c projectInfo = new Project__c();
    
    for (Event e : eventToUpdate)
    {
        if(e.WhatId != Null)
        {
            If(string.valueOf(e.WhatId).startsWith('a0E'))
            {
                clientInfo = [SELECT ID, Name from Clients__c Where ID = :e.WhatId];
                e.Calendar_Title__c = clientInfo.Name + ': ' + e.Subject;
            }
            
            If(string.valueOf(e.WhatId).startsWith('a04'))
            {
                projectInfo = [SELECT ID, Name from Project__c Where ID = :e.WhatId];
                e.Calendar_Title__c = projectInfo.Name + ': ' + e.Subject;
            }
        
        }
    }

}