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
bmartinez76bmartinez76 

Need help with test class.

I created my trigger and it seems to work fine. When I try to create a bulk insert test I get 100% coverage but get an error which I can't seem to overcome. Any help would be greatly appreciated.

 

Error

22:56:46.254|FATAL_ERROR|System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, LastFTFDateforAccountStamp: execution of BeforeInsert

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

Trigger.LastFTFDateforAccountStamp: line 27, column 9: []

Class.AEREGIONROLECLASSONACTIVITIES.testBulkInsert: line 85, column 10
External entry point

 

Trigger

 

trigger LastFTFDateforAccountStamp on Event (before insert, after update){ Set<Id> Events = new Set<id> (); List<Account> AccountList = new List<Account>(); for (Event e : Trigger.new) Events.add(e.id); Map<Id, Event> eventdate= new Map<Id,Event> ([Select ActivityDatetime from event where id in :Events]); for (Event e : Trigger.new){ if (e.call_type__c =='In Person' && e.EventStatus__c=='Completed'&& e.ActivityDatetime < datetime.now()) { //update account if event is in Person and completed Account a = new Account( Id = e.AccountId ); a.Last_FTF_Activity__c=e.endDatetime; AccountList.add(a); } else if (e.call_type__c =='Sales Phone Call' && e.EventStatus__c=='Completed'&& e.ActivityDatetime < datetime.now()) { Account a = new Account( Id = e.AccountId ); a.Last_SPC_Activity__c=e.endDatetime; AccountList.add(a); update AccountList; } } }

 

 Test Class

public with sharing class AEREGIONROLECLASSONACTIVITIES { static testMethod void testSingleInsert() { Profile p = [select id from profile where name='Standard User']; UserRole ur= [select ID from UserRole where name='Support Analyst']; User u = new User(alias = 'test123', email='test123@noemail.com', emailencodingkey='UTF-8', lastname='Testing', languagelocalekey='en_US', localesidkey='en_US', profileid = p.Id, country='United States', timezonesidkey='America/Los_Angeles', username='test123@noemail.com'); insert u; Lead L = new Lead(LastName='Tester',Company='Test Inc',OwnerId=u.Id,ownerRoleid__c=ur.id); insert L; Contact co = new Contact( firstname='John', lastname='Doe', Survey_sent__c=true); insert co; Account a= new Account( Name = 'Test Account', OwnerId = u.Id ); insert a; case c = new Case(Taken_By__c='Internal',customer_type__C='Internal',priority='low', origin='Email',status='Assigned',escalation__C='Level 2',subject='Brian Trigger Test', Description='Brian Trigger Test',Update_Contact_Survey_Value__c = True,Send_Internal_Survey_to_User__c=true,contactid=co.id,OwnerId = u.Id ); insert c; Event e2= new Event(subject='Test', ActivityDatetime= datetime.now(), DurationInMinutes=60,eventstatus__c='Completed', Product_Subject__c='Arrow',format__c='Demo',Call_type__c='In Person',OwnerId = u.Id); insert e2; update e2; Account bb= new Account( Name = 'Test Account', Last_SPC_Activity__c=e2.ActivityDatetime, OwnerId = u.Id ); insert bb; Opportunity o= new Opportunity(Name='Brian Test', type='COA', ordertype__c='Defensive COA',stagename='S0-Suspect Pool', closedate=date.today()); insert o; System.assertEquals(u.unavailable__c,[Select Unavailable__c From CASE Where Id = :c.Id].unavailable__c); } static testMethod void testBulkInsert() { List<Event> events = new List<Event>(); Profile p = [select id from profile where name='Standard User']; User u = new User(alias = 'test123', email='test1234@noemail.com', emailencodingkey='UTF-8', lastname='Testing', languagelocalekey='en_US', localesidkey='en_US', profileid = p.Id, country='United States', timezonesidkey='America/Los_Angeles', username='test1234@noemail.com'); insert u; Account a= new Account( Name = 'Test Account', OwnerId = u.Id ); insert a; for (Integer i=0;i<200;i++) { Event e2= new Event(subject='Test', ActivityDatetime= datetime.now(), DurationInMinutes=60,eventstatus__c='Completed', Product_Subject__c='Arrow',format__c='Demo',Call_type__c='Sales Phone Call',OwnerId =u.Id); events.add(e2); } insert events; } }

 


 

Always ThinkinAlways Thinkin

Hi,

I can see in your trigger that you reference the AccountId on the Event but I don't see in your test class where you associate an AccountId with the Event(s) you create - does it just get pulled in automatically as a result of associating the user with the account and then the user with the event?

bmartinez76bmartinez76
I tried associating an account id, but it says that event.account.id field is not writeable, so I'm assuming that it automatically pulls in that information.
Hargobind_SinghHargobind_Singh

Instead of Event.Account.Id, please use Event.AccountId , which is the actual field name in Event Object..

 

 

 

 

Hargobind

 

 

 

bmartinez76bmartinez76

that is how I had it, but i made a typo posting it here.

 

 

 

When I try using that method I get a save error : Field is not writable:Event.Accountid

 

 

 

 

Account a= new Account( Name = 'Test Account', OwnerId = u.Id ); insert a; for (Integer i=0;i<200;i++) { Event e2= new Event(subject='Test', accountid=a.id, ActivityDatetime= datetime.now(), DurationInMinutes=60,eventstatus__c='Completed', Product_Subject__c='Arrow',format__c='Demo',Call_type__c='Sales Phone Call',OwnerId =u.Id); events.add(e2); } insert events;

 


 


 

Message Edited by bmartinez76 on 02-04-2010 08:41 AM
Always ThinkinAlways Thinkin

Hi,

now that I look at the schema I can see that AccountID is indeed not updateable. I wonder whether it is automatically populated based on the Account related to the Contact the way Cases are automatically updated?

 

I've usually used the WhatID field to relate Tasks (basically the save as Events: an Activity object) to another sObject. You could use that in your trigger but you would have to filter out Events that are not assigned to an Account in the WhatID field.

 

Alternately, if you can determine if Contact's parent Accounts populate the ID field then you could create a Contact in your test class and assign it to the Event to get the Account ID in there. 

bmartinez76bmartinez76

Always Thinkin, I tried doing the contact thing, but that doesn't automatically bring in the account ID. would you mind providing some sample code on how you usually use WhatID? I'm not a programmer by nature and just starting to learn apex. I think I'm starting to exhaust my capabilities with this trigger and test class. I've researched as much as I can that I feel like the guy in your avatar lol.

Hargobind_SinghHargobind_Singh

Didn't get a chance to do an experiment.., but usually the non-updateable fields are those which can be set when inserting the records., but cannot be updated later on.

 

Did you try to specify the Account during the insert record ? Because, I've clearly remember creating Event records related directly to Accounts.

 

HS

 

bmartinez76bmartinez76

Yes, but perhaps I'm doing it wrong. I tried the following, but it's still not working.

 

 

Account a= new Account( Name = 'Test Account', OwnerId = u.Id ); insert a; for (Integer i=0;i<200;i++) { Event e2= new Event(whatid=a.id, subject='Test', accountid=a.id, ActivityDatetime= datetime.now(), DurationInMinutes=60,eventstatus__c='Completed', Product_Subject__c='Arrow',format__c='Demo',Call_type__c='Sales Phone Call',OwnerId =u.Id); events.add(e2); } insert events;