3

Salesforce doc states when you delete an Account, its children orders are deleted

But: If you do this in an org with a real Account and a draft Order, when you delete the Account, you get this error:

Your attempt to delete MyAccount could not be completed 
because it is associated with the following orders.: 00433894

So, for sake of argument, let's assume the doc is wrong

Now, here is where it gets weird...

Consider an APEX testmethod that deletes an Org Account w/ Order

@IsTest (SeeAllData=true)
static void testAccountDeleteWithOrgOrder() {
    Database.DeleteResult[] results = 
       Database.delete(new List<Account> {new Account(Id='0016300000eqXLkAAM')},false);
    System.debug(LoggingLevel.INFO,'results='+results);
}

The delete fails (just like a delete from the UI) with results showing:

Database.DeleteResult[getErrors=(Database.Error[getFields=();
    getMessage=Your attempt to delete 1563488920160 could not be completed 
    because it is associated with the following orders.: 00433894

Same thing as above happens if you run the Database.delete code in anonymous Apex.

But if your testmethod mocks the Accounts and child Order (as one would normally do)

@IsTest
static void testAccountDeleteWithMockOrder() {

    Account a = new Account(Name = 'mockAccount');
    insert a;

    Order o = new Order(AccountId = a.Id, EffectiveDate = Date.today(), Status = 'Draft');
    insert o;

    Database.DeleteResult[] results = Database.delete(new List<Account> {a},false);
    System.debug(LoggingLevel.INFO,'results='+results);
}

Here, in the testmethod with mocked Accounts and Orders, the delete succeeds !?!

results=(Database.DeleteResult[getErrors=();getId=0016300000vIQUjAAO;isSuccess=true;])

Why doesn't the testmethod (V50) behave the same as PROD (also V50)?

1 Answer 1

4

Why is this important? Well, if you're writing a testmethod that is attempting to test partial success saves on Account deletion, you can't just mock Accounts and draft Orders, you need a different technique to get the partial save success.

Things I tried:

Action Result
Adding OrderItems to the mocked Order Account still deletes 😞
Adding Assets to the Account Account still deletes 😞
Activating the Order Account fails to delete 😊 ; partial save success can be tested!
Adding a Case to the Account Account fails to delete 😊 ; partial save success can be tested!

Note that activating an Order requires the Order to have OrderItems.

So, the simplest solution is probably to mock a Case as child to the Account

@IsTest
static void testAccountDeleteWithMockCase() {

  Account a = new Account(Name = 'mockAccount', RecordTypeId = Accounts.ID_RT_B2C);
  insert a;

  Case cs = new Case(AccountId = a.Id);
  insert cs;

  Database.DeleteResult[] results = Database.delete(new List<Account> {a},false);
  System.debug(LoggingLevel.INFO,'results='+results);
}
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .