List<Contact> contactDeleteList = new List<Contact>(); for(Contact con : [Select Id, (Select Id from Cases) from Contact where CreatedDate < 2012-12-31T00:00:00Z]) { if(con.Cases.size() = 0) { contactDeleteList.add(con); } }
Database.DeleteResult[] DR_Contacts =
Database.delete(Select Id, (Select Id from Cases) from Contact where CreatedDate < 2012-12-31T00:00:00Z, false);
The second argument, false, means keep deleting the other objects in the set if one of the deletes fail. I haven't tested this and governor limits will still apply so keep that in mind.
I have saved this code in my org and checked properly & it is getting saved properly. I dont think it would give any error please send me screen shot of error message.
public class Purge { // Method to delete public void DeleteContacts() { integer RecCount; integer i; RecCount=[Select count() from Contact where AccountId = '0017000000qz98bAAA' AND id not in (select ContactId from Case) ]; System.debug('=======count of record to be deleted ======='+RecCount ); List<Contact> DeleteList = New List<Contact>(); for (i=1 ; i<=RecCount ; i=i+10000) { DeleteList = [SELECT Id FROM Contact WHERE CreatedDate <= 2013-04-01T00:00:00Z AND AccountId =&
nbsp;'0017000000qz98bAAA' AND id not in (select ContactId from Case) LIMIT 10000]; } if(DeleteList.size()>0) Delete DeleteList ; } }
Please copy only below code:
==============================================================================
Public Class DeleteDataClass
{
// Constructor
public DeleteDataClass()
{
}
// Method to delete
public void DeleteContacts()
{
List<contact> ContactsWithoutCasestoDelete =
new List <Contact>([select id from Contact where CreatedDate < 2012-12-31T00:00:00Z AND id not in (select ContactId from Case)]);
System.debug('=======count of record to be deleted ======='+ContactsWithoutCasestoDelete.size() );
if(ContactsWithoutCasestoDelete.size()>0)
Delete ContactsWithoutCasestoDelete ;
}
}
All Answers
Following is the code as per your requirement:
List<Contact> contactDeleteList = new List<Contact>();
for(Contact con : [Select Id, (Select Id from Cases) from Contact where CreatedDate < 2012-12-31T00:00:00Z])
{
if(con.Cases.size() = 0)
{
contactDeleteList.add(con);
}
}
if(contactDeleteList.size() > 0)
delete contactDeleteList ;
Please mark as solution if it works fine for you, so it would help others too.
Thanks,
Neetu Bansal
Thanks Let me try this one
You might can simplify this with
The second argument, false, means keep deleting the other objects in the set if one of the deletes fail. I haven't tested this and governor limits will still apply so keep that in mind.
Here is one more apparoach with reducing Script Statements to 3 ;
List<contact> ContactsWithoutCasestoDelete =
new List <Contact>([select id from Contact where CreatedDate < 2012-12-31T00:00:00Z AND id not in (select ContactId from Case)]);
if(ContactsWithoutCasestoDelete.size()>0)
Delete ContactsWithoutCasestoDelete ;
Hi Neetu this nested query doesnot work
Hi There,
Just wondering if want to count those contact without cases is that possible
I tried following and getting a stupid error
@istest
public class DeleteContactwithoutCase
{
list <Contact> contdel=New list<Contact>();
integer i=0;
for (contact con:[select id, (select id from cases)from contact where createdDate < 2012-12-31T00:00:00Z]){
if(con.cases.size()==0)
{
contdel.add(con);
i++;
}
}
System.debug('The value is: ' +i);
}
Did you try way I suggested ?
I will give you Kudos Once it is working
so what should I do create a class ?
one more thing If I just want to count those records how Can I do it ?
Thanks,
Neetu Bansal
I think so Because it keep giving me error on right curly brackets which already exist
Here is complete code of Class and Page
Public Class DeleteDataClass
{
// Constructor
public DeleteDataClass()
{
}
// Method to delete
public void DeleteContacts()
{
List<contact> ContactsWithoutCasestoDelete =
new List <Contact>([select id from Contact where CreatedDate < 2012-12-31T00:00:00Z AND id not in (select ContactId from Case)]);
System.debug('=======count of record to be deleted ======='+ContactsWithoutCasestoDelete.size() );
if(ContactsWithoutCasestoDelete.size()>0)
Delete ContactsWithoutCasestoDelete ;
}
}
<apex:page Controller="DeleteDataClass" >
<apex:form>
<apex:commandbutton value="Delete Contacts" action="{!DeleteContacts}" />
</apex:form>
</apex:page>
Hi
I have saved this code in my org and checked properly & it is getting saved properly. I dont think it would give any error please send me screen shot of error message.
I just copied and pasted your code
Please copy only below code:
==============================================================================
Public Class DeleteDataClass
{
// Constructor
public DeleteDataClass()
{
}
// Method to delete
public void DeleteContacts()
{
List<contact> ContactsWithoutCasestoDelete =
new List <Contact>([select id from Contact where CreatedDate < 2012-12-31T00:00:00Z AND id not in (select ContactId from Case)]);
System.debug('=======count of record to be deleted ======='+ContactsWithoutCasestoDelete.size() );
if(ContactsWithoutCasestoDelete.size()>0)
Delete ContactsWithoutCasestoDelete ;
}
}
It does work
however it won't delete more 10000 records at a time and I have million of those any suggestions
what you guys think about following
query limit is 10000
and offset limit is 2000
I have few million which needs to be deleted
I got the count using this query and count field of contacts