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
Abhi_TripathiAbhi_Tripathi 

How to delete child contact records of a parent Account Record USING SOQL QUERY.

hey,

 

    I am trying to delete contacts (Child records) of an Account, but only when i remove the LastName(Mandatory field) of a contact record.

Actually  i have visualforce page where LastName field of Contact is an input field, if user wants to update he can change the LastName and the record will be Updated...this thing is happening in my code

     BUT trying to delete the records by removing the LastName and leaving it BLANK...Thats not happening.

 

Regards 

Abhi Tripathi

Best Answer chosen by Abhi_Tripathi
ManjunathManjunath

Hi,

 

First get all the contact then apply select to that list.

 

Check the below example.

 

for(Contact con : conRecords){ 

     if(con.LastName == null && con.LastName == ''){
       DeleteContactsList.add(con);
    }

}
 if(DeleteContactsList.size() > 0){
       system.debug('$$$$$ DeleteContactsList' + DeleteContactsList);
       delete DeleteContactsList;
 }

Regards,

 

All Answers

Devendra@SFDCDevendra@SFDC
Hi,

LastName is a required field on Contact record, hence you can not keep it blank.

You can keep one CheckBox field and based on that field value execute a apex method for deleting contact records.

Thanks,
Devendra
Abhi_TripathiAbhi_Tripathi

Thanks for the Reply sir...

but i have created that LastName field into an InputFiels on the vf page, that thing was happening but in a wrong manner, when i leave that LastName field blank other then that oll the child contact records were deleted.

   so i think this can be done...but donno how..!!

 

Devendra@SFDCDevendra@SFDC
You want to delete contact records of one account and there is a specific condition on Contact record which needs to be checked before deleting contact records, correct?

Can you please explain the scenario?

Thanks,
Devendra
Abhi_TripathiAbhi_Tripathi

This is the code which i am trying to execute...if the user edits the LastName of the Contact and leave it blank then after click of a button the records of child contact will be deleted from the database.
 

 

for(Contact con : conRecords){ 

     if(con.LastName == null && con.LastName == ''){
         
        
          DeleteContactsList.add(con);
    }
 i  f(DeleteContactsList.size() > 0){
       system.debug('$$$$$ DeleteContactsList' + DeleteContactsList);
       delete DeleteContactsList;
    }
}

Devendra@SFDCDevendra@SFDC
Hi,

Why you are navigating to all the contact record using "conRecords" ?

Thanks,
Devendra
Abhi_TripathiAbhi_Tripathi

i am Navigating oll the child contacts only...i have provided you only the required code...not the whole thing..the conRecords is the only child contact list of an account.

 

thanks

ManjunathManjunath

Hi,

 

First get all the contact then apply select to that list.

 

Check the below example.

 

for(Contact con : conRecords){ 

     if(con.LastName == null && con.LastName == ''){
       DeleteContactsList.add(con);
    }

}
 if(DeleteContactsList.size() > 0){
       system.debug('$$$$$ DeleteContactsList' + DeleteContactsList);
       delete DeleteContactsList;
 }

Regards,

 

This was selected as the best answer
Kunal01Kunal01
Pleas try out below code :

for(Contact con : conRecords){ 

     if(con.LastName == null || con.LastName == ''){
       DeleteContactsList.add(con);
    }


}
 if(DeleteContactsList.size() > 0){
       system.debug('$$$$$ DeleteContactsList' + DeleteContactsList);
       delete DeleteContactsList;
 }


~KR
Abhi_TripathiAbhi_Tripathi
Thanks Kunal, but its a 2 year old query :) but anyway thanks for the help