Created
October 21, 2019 17:54
-
-
Save sgoley/58d15cb2dec5d10d3698888ae6dd03e1 to your computer and use it in GitHub Desktop.
Salesforce Anon Apex to delete from connected object through lookup on child
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Make a list of ID's of addresses on Suppliers : List 1 | |
List< Supplier__c > supList = [ SELECT Id,Address__c FROM Supplier__c ]; | |
List<Id> supids = new List<Id>(new Map<Address__c,Supplier__c>(supList).keySet()); | |
//Make a list of all address IDs : List 2 | |
List< Address__c > AdrList = [ SELECT Id FROM Address__c ]; | |
List<Id> adrids = new List<Id>(new Map<Id,Address__c>(AdrList).keySet()); | |
//Make a list of ID's in List 2 which are not in List 1 : List 3 | |
list< Id > List3 =[ SELECT Id FROM Address__c where Id not in:adrids Limit 1]; | |
// Delete list 3 | |
Database.DeleteResult[] deleteResults = Database.delete(List3, true); | |
for(Database.DeleteResult dr : deleteResults) { | |
if (!dr.isSuccess()) { | |
for(Database.Error err : dr.getErrors()) { | |
System.debug(LoggingLevel.Error, 'The following error has occurred.' | |
+ '\n' + err.getStatusCode() + ': ' + err.getMessage() | |
+ '\n fields that affected this error: ' + err.getFields()); | |
// Plus further error handling as required | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment