I am new to Salesforce. I want to delete all records where in the description field of the Account records there is a word "Test". How can I achieve this?


Try the below code :

List<Account> accList = [select id,name,Description from account];
Set<Account> accSet = new Set<Account>();
List<String> st = new List<String>();
for(Account a : accList){ 
    if(a.Description != null){  
        st = a.Description.Split (' ');
    for(String ss : st){
      if(ss == 'Test'){
accList = new List<Account>(accSet);
delete accList;

Hope it will help you

  • 2
    I'd be useful if you explained what your code is doing here and try to educate the OP, rather than encouraging "copy, paste, trust me" particularly with deleting data. – Dan Jones Apr 13 '17 at 8:39

You can do this by going into Salesforce Workbench, logging in, going to "Utilities" and "Apex Execute".

From there you can write a short query, assign it to a collection and use DELETE DML.

For instance:

List<Account> accountList = [SELECT Id FROM Account WHERE Description LIKE '%Test%'];

DELETE accountList;

Be careful using this tool as it's executed anonymously and will do exactly what you tell it. I'd advise going to "Queries" and "SOQL Query" and using it's query builder so you know exactly what your query will retrieve and as a consequence what it will delete beforehand.

  • 1
    Or just delete [SELECT Id FROM Account WHERE Description LIKE '%Test%']; – Robin De Bondt Apr 13 '17 at 8:28
  • 1
    @RobinDeBondt I've learned something new there. Didn't realise you could just call DML directly on a query. Makes sense though as it returns a collection. Thanks for that! I guess the only reason I wouldn't be too anxious to do something like that would be because I typically debug everything, like those collections. – Dan Jones Apr 13 '17 at 8:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.