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
DaGunsterDaGunster 

Apex SOQL: Delete of Contact[] by AccountId works, but delete of Event[] by Account ID does not

Here is the trigger...

Scroll down to ...// TRY DELETING CONTACTS
This works.
The next two lines try to delete Events.
These fail.
The account # came from Apex Explorer and I know is correct.
don't worry about all the 'weird' lines in this - it's just a copy and paste from the code window in SF.

***
*** I need to be able to delete events and recurring events given an Account ID. ***
***

trigger MakeRouteEvents on Account (before insert, before update)
{

// Trigger events
// before insert, before update, before delete, after insert
// after update, after delete, after undelete

// Trigger Context Variables
// isExecuting, isInsert, isUpdate, isUpdate, isDelete, isBefore
// isAfter, isUndelete, new, newMap, old, oldMap, size

Integer i;
Account[] oldRecords;
Account[] newRecords;
Lead newLead; // testing
Lead[] deleteLeads;
Event[] existingEvents;
String strAccountId;
Contact[] existingContacts;

if (Trigger.isInsert)
System.debug('An Insert Trigger');
else
System.debug('Not Insert Trigger');


strAccountId = '0017000000NB5vCAAT';

// this block works
//newLead = new Lead(lastname = 'FryZZ', company='FryZZ And Sons');
//insert newLead;

// this block works also
//deleteLeads = [select id, name from lead where company= 'Jackson Controls'];
//try
// {delete deleteLeads;}
//catch (DmlException e)
// {
// Process exception here
// }

// TRY DELETING CONTACTS
existingContacts = [select Id, AccountId from Contact where AccountId = '0017000000NB5vCAAT'];
delete existingContacts;

//existingEvents = [select Id, AccountId from Event where AccountId = :strAccountId];
existingEvents = [select Id, AccountId from Event where AccountId = '0017000000NB5vCAAT'];
delete existingEvents;





i=5;
//sqlContact = "select lastname from contact where accountid in";
//resultContact = [ :Trigger.new];

//CustomerRouting.DropCustomerRouteEvents();


oldRecords = Trigger.old;
newRecords= Trigger.new;

System.debug(oldRecords[0].Number_of_Units__c);
System.debug(newRecords[0].Number_of_Units__c);

}
saasdevsaasdev
try using WhatId instead of AccountId in the Event Object
DaGunsterDaGunster
Thanks. That worked.