You need to sign in to do that
Don't have an account?

delete multiple records using SOQL
Hi, I am learning SF developer. I created 100 records in Account object using SQL query. then im trying to delete all those records using SOQL query and for loop here is my code:
list<Account> newlist=new list<Account>();
newlist=[SELECT Id,Name FROM Account WHERE Name='test name'];
system.debug('list'+newlist);
list<Account> deletelist=new list<Account>();
for(Account a: newlist){
a.Name='test name';
deletelist.add(a);
}
system.debug('list'+deletelist);
delete deletelist;
but i am unable to delete. i know i can use work bench for this but i want to delete using SOQL here is the error im facing:
Line: 10, Column: 1
System.DmlException: Delete failed. First exception on row 0 with id 0016F00003O5AOEQA3; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, acountdeletetrigger: execution of BeforeDelete caused by: System.NullPointerException: Attempt to de-reference a null object Class.accountdeleteclass.deletecheck: line 3, column 1 Trigger.acountdeletetrigger: line 3, column 1: []
please tell me whats wrong thanks
list<Account> newlist=new list<Account>();
newlist=[SELECT Id,Name FROM Account WHERE Name='test name'];
system.debug('list'+newlist);
list<Account> deletelist=new list<Account>();
for(Account a: newlist){
a.Name='test name';
deletelist.add(a);
}
system.debug('list'+deletelist);
delete deletelist;
but i am unable to delete. i know i can use work bench for this but i want to delete using SOQL here is the error im facing:
Line: 10, Column: 1
System.DmlException: Delete failed. First exception on row 0 with id 0016F00003O5AOEQA3; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, acountdeletetrigger: execution of BeforeDelete caused by: System.NullPointerException: Attempt to de-reference a null object Class.accountdeleteclass.deletecheck: line 3, column 1 Trigger.acountdeletetrigger: line 3, column 1: []
please tell me whats wrong thanks
There is been a trigger you have written name as Trigger:- acountdeletetrigger. This trigger is causing the error. You can check your trigger or you can stop the trigger and then try to delete the record.
Thanks
All Answers
I reviewed your code and the error in your code is you cannot perform DML operation (INSERT, UPDATE, DELETE, UPSERT, UNDELETE) inside a for-loop. So just query the list of account records and delete that list. Please try the following code:
List<Account> newList = new list<Account>();
newList = [SELECT Id,Name FROM Account WHERE Name='test name'];
system.debug('list'+newList);
delete newList;
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks and Regards,
Deepali Kulshrestha
There is been a trigger you have written name as Trigger:- acountdeletetrigger. This trigger is causing the error. You can check your trigger or you can stop the trigger and then try to delete the record.
Thanks
I just built a Library to delete bulk records in apex.
You can delete them in user or system mode in a batch size that suits you.
There are a host of other features too.
To use the library you can go to : https://github.com/vipul-goyal/Salesforce-Batch-To-Delete-Records
The code is in DX format.
Please star the repository if you find it worth it.
Thanks
i m written dml for delete operation
But i m delete my single record but i m trying to delete a more records at tym how does do it..