5

I have a trigger set up on the Account object for After Delete. We have an Affiliates table that has a lookup field to Account.

If an Account is merged, I don't want to delete an Affiliate. It will be updated as usual. If an Account is deleted then I want to delete the Affiliate.

In my trigger I am simply deleting any Affiliates with an empty Account field. That takes care of everything I need.

I manually tested it in the sandbox and it worked as expecting. Keeping the affiliates on a merge and removing on a delete. My Test class is failing and I can't understand. The Select query is returning a row.

Trigger that runs After Delete for Account:

public class AFFL_AccountDeletion_TDTM extends TDTM_Runnable {


public override DmlWrapper run(List<SObject> listNew, List<SObject> listOld, TDTM_Runnable.Action triggerAction, Schema.DescribeSObjectResult objResult) {

    DmlWrapper dmlWrapper = new DmlWrapper();
    string accMasterRecordID;

    // Only run after delete
    if (triggerAction == TDTM_Runnable.Action.AfterDelete) {


        // Add related affiliations to list to delete
       dmlWrapper.objectsToDelete.addAll((List<SObject>)[SELECT Id FROM Affiliation__c WHERE Account__c =null]);
    }
    return dmlWrapper;
}
}

Test Class:

@isTest (seeAllData = false)
private class AFFL_AccountDeletion_TEST {

@isTest
static void test_delete() {

    Account a1 = [SELECT Id FROM Account WHERE Name = :'test'];
    Affiliation__c affl1 = [SELECT Id FROM Affiliation__c WHERE Account__c = :a1.Id];

    Test.startTest();

    delete a1;

    Test.stopTest();

    System.assert([SELECT Id FROM Affiliation__c WHERE Id = :affl1.Id].isEmpty());
}

@testSetup
static void setup() {

    Account a1 = new Account(Name = 'test');
    insert a1;

    insert new Affiliation__c(Account__c = a1.Id);
 }
}
2

TDTM (Table Driven Trigger Management) is driven by custom object records to define the handlers called by a trigger. Since custom objects are data, not metadata, they're not available in your test context, so the core trigger won't call your handler.

You can fix this by inserting the appropriate TDTM object in your @testSetup method, or by simply calling your trigger handler directly in the test class and synthesizing the trigger parameters. In this case, since your trigger handler doesn't use its parameters anyway, we can just pass null and save ourselves some lines of code.

    delete a1;
    new AFFL_AccountDeletion_TDTM().run(null, null, null, null);
  • Thank you David, I bet that is the issue. I've been searching to try and find examples for the two ways you suggested to solve it but haven't come across anything. Can you please share an example? Thanks again! – Chancie Pants Mar 7 '18 at 18:10
  • I generally don't work with TDTM so I don't have an example (I bet the NPSP codebase on GitHub does, though). The easy way is just to call new AFFL_AccountDeletion_TDTM().run(PARAMETERS); after your delete in the test method. – David Reed Mar 7 '18 at 18:13
0

Ok - this is what I ended up with for my Test Class. The TdtmToken section of the code is what corrected it.

@isTest (seeAllData = false)
private class AFFL_AccountDeletion_TEST {

@isTest
static void test_delete() {

Account a1 = [SELECT Id FROM Account WHERE Name = :'test'];
Affiliation__c affl1 = [SELECT Id FROM Affiliation__c WHERE Account__c = :a1.Id];

Test.startTest();

delete a1;

Test.stopTest();

System.assert([SELECT Id FROM Affiliation__c WHERE Id = :affl1.Id].isEmpty());
}

@testSetup
static void setup() {

//This is where we are brining in the class it needs to run      
List<TDTM_Global_API.TdtmToken> tokens = TDTM_Global_API.getTdtmConfig();
tokens.add(new TDTM_Global_API.TdtmToken('AFFL_AccountDeletion_TDTM', 'Account', 'AfterDelete', 5));
TDTM_Global_API.setTdtmConfig(tokens);

Account a1 = new Account(Name = 'test');
insert a1;

insert new Affiliation__c(Account__c = a1.Id);
 }
}

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.