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

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');
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'];
// {delete deleteLeads;}
//catch (DmlException e)
// {
// Process exception here
// }

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;

//sqlContact = "select lastname from contact where accountid in";
//resultContact = [];


oldRecords = Trigger.old;


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