You need to sign in to do that
Don't have an account?
Aman Bhalla
Apex Code delete all Tasks based on Criteria
Hi All,
First time playing around with Developer Console. I'm trying to delete all Tasks that have been created by a user (85,000 records). I can't use Data Loader since it will max out the number of records I want to delete so instead, I was able to create a query to find all the records. I'm now trying to use APEX Code to delete all the records listed and am having some trouble.
I am trying to find all tasks that were created by a user where the subject line does not equal "Initial Lead Notification Sent" or "Secondary Lead Notification Sent". Once its executed and I have a list, I would like to delete all records. Here's what I have so far:
APEX CODE
// fetching all Tasks Records via SOQL
List<task> TaskList = new List<task>();
TaskList = [SELECT Id, subject, WhoID, ActivityDate, FROM
task WHERE CreatedByID = '0051400000Bazbn' AND subject != 'Initial Lead Notification Sent' AND subject != 'Secondary Lead Notification Sent'];
// SOQL query for given criteria
// Delete the fetched records
delete [SELECT Id FROM TaskList LIMIT 1000];
SOQL
select id, subject, WhoID, ActivityDate FROM task where CreatedByID = '0051400000Bazbn' AND subject != 'Initial Lead Notification Sent' AND subject != 'Secondary Lead Notification Sent'
Error:
Row 3 column 10, Unexpected token "=".
First time playing around with Developer Console. I'm trying to delete all Tasks that have been created by a user (85,000 records). I can't use Data Loader since it will max out the number of records I want to delete so instead, I was able to create a query to find all the records. I'm now trying to use APEX Code to delete all the records listed and am having some trouble.
I am trying to find all tasks that were created by a user where the subject line does not equal "Initial Lead Notification Sent" or "Secondary Lead Notification Sent". Once its executed and I have a list, I would like to delete all records. Here's what I have so far:
APEX CODE
// fetching all Tasks Records via SOQL
List<task> TaskList = new List<task>();
TaskList = [SELECT Id, subject, WhoID, ActivityDate, FROM
task WHERE CreatedByID = '0051400000Bazbn' AND subject != 'Initial Lead Notification Sent' AND subject != 'Secondary Lead Notification Sent'];
// SOQL query for given criteria
// Delete the fetched records
delete [SELECT Id FROM TaskList LIMIT 1000];
SOQL
select id, subject, WhoID, ActivityDate FROM task where CreatedByID = '0051400000Bazbn' AND subject != 'Initial Lead Notification Sent' AND subject != 'Secondary Lead Notification Sent'
Error:
Row 3 column 10, Unexpected token "=".
Anyways, all you have to use is one statement.
delete [SELECT Id FROM Task WHERE CreatedByID = '0051400000Bazbn' AND subject not in('Initial Lead Notification Sent','Secondary Lead Notification Sent')];
All Answers
Anyways, all you have to use is one statement.
delete [SELECT Id FROM Task WHERE CreatedByID = '0051400000Bazbn' AND subject not in('Initial Lead Notification Sent','Secondary Lead Notification Sent')];
Batch Apex Code:
Inorder to execute this code all you need to do is to go to your Developer Console - Debug - Open Execute Anonymous Window and type below code,
Hope this helps!
If this solves the puzzle the please mark it as Best Answer!