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
adi salesforceadi salesforce 

Code to query a record and to delete a record.

Steven NsubugaSteven Nsubuga
Account acct = [SELECT Id FROM Account WHERE Name = 'Account Name' LIMIT 1];
delete acct;

 
Ajay K DubediAjay K Dubedi

Hi Adi,
Please try this code and check all previous questions which you have the post.
mark as solve which may help others.

Example: Delete all contacts belonging to Accounts Name FIELD having 'A' in them.

public class DeleteContact {  
    public static List<Contact> delMethod(){ 
        List<Contact> conList = new List<Contact>();
         conList = [SELECT ID ,Account.Name FROM Contact
                       WHERE Account.Name like '%A%'];
       
        
        if(conList.size()>0){
            database.delete(conList,false);
        }
        return conList;
    }   
}

Please let me know if you have any query.
Please mark it as best Answer if you find it helpful.

Thank You
Akshay_DhimanAkshay_Dhiman
Hi adi,
Please try this Query:
  General Examlple
  Object__c obj = [select Id from Object__c where Id = 'xxxxx'];
   Database.delete(obj,false);
   
   Example:
    Account acc= [select Id from  Account ];
   Database.delete(acc,false);
    
    Thanks 
    Akshay
SFDC Beginner745SFDC Beginner745
Hi Adi,

Please try this code in Anonymous Window and if helpful then mark as solve which may help others.

Ex: To Delete Account Records.

List<Account>  accountList =[Select id,Name from Account];
Database.delete(accountList ,false);

Thanks
Developer Forum