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
ss123ss123 

Need to delete all contact with no case attached

Need to delete all contact with no case attached to it and created before 12/31/12 How Can i do that non programmatically or programatically

Best Answer chosen by Admin (Salesforce Developers) 
sandeep@Salesforcesandeep@Salesforce

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

Neetu BansalNeetu Bansal
Hi,

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
ss123ss123

Thanks Let me try this one

ryanjuptonryanjupton

You might can simplify this with

 

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.

sandeep@Salesforcesandeep@Salesforce

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 ; 

ss123ss123

Hi Neetu this nested query doesnot work

ss123ss123

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);

}

sandeep@Salesforcesandeep@Salesforce

Did you try way I suggested ?

ss123ss123

I will give you Kudos Once it is working

so what should I do create a class ?

ss123ss123

one more thing If I just want to count those records how Can I do it ?

 

Neetu BansalNeetu Bansal
Please let me know the issue you faced in the nested query
Neetu BansalNeetu Bansal
The length of the list is the count of records.

Thanks,
Neetu Bansal
ss123ss123

I think so Because it keep giving me error on right curly brackets which already exist

ss123ss123
I do have a counter variable for that however it doesn't move further than that curly brackets for your code and also Sandeep's code
ss123ss123
Please help me if you can asap
sandeep@Salesforcesandeep@Salesforce

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>

 

ss123ss123
Hi Sandeep for some reason it is still giving me error for right curly braces I think it is due to nested queries
sandeep@Salesforcesandeep@Salesforce

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.

ss123ss123
Error:Compile Error:unexpected token'}' at line 19 column 0
I just copied and pasted your code
sandeep@Salesforcesandeep@Salesforce

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 ; 

}

 

}

This was selected as the best answer
ss123ss123

It does work

however it won't delete more 10000 records at a time and I have million of those any suggestions

ss123ss123

what you guys think about following

query limit is 10000

and offset limit is 2000

I have few million which needs to be deleted


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 ; 
}
}
ss123ss123

I got the count using this query and count field of contacts