it should delete records with no email. error: System.DmlException: Delete failed. First exception on row 0 ENTITY_IS_DELETED, entity is deleted: []

it should delete records with no email. error: System.DmlException: Delete failed. First exception on row 0 ENTITY_IS_DELETED, entity is deleted: []

List<Contact> conList = [SELECT Id, Email FROM Contact WHERE LastName='Smith'];

List<Contact> listToUpdate = new List<Contact>(); for(Contact con:conList) { if(con.Email != null) continue;

else listToUpdate.add(con);

delete listToUpdate; }

它应该删除没有email.error的记录:System.DmlException:删除失败。第 0 行的第一个异常,id 为 0035j00000LnS5fAAF;第一个错误:ENTITY_IS_DELETED,实体被删除:[]

你格式化代码的方式是垃圾。您没有看到您已将删除操作放入循环中。如果您有很多联系人并且您对上一次迭代中删除的内容调用了删除 - 是的,这就是您会遇到的错误。

这个更好:

List<Contact> conList = [SELECT Id, Email FROM Contact WHERE LastName='Smith'];

List<Contact> listToUpdate = new List<Contact>(); 
for(Contact con:conList) {
    if(con.Email != null){
        continue;
    } else {
        listToUpdate.add(con);
    }
}
delete listToUpdate; 

这是最好的

delete [SELECT Id
    FROM Contact
    WHERE LastName='Smith' AND Email = null];

因为它将查询最少的行数并且根本没有循环。