I am creating test coverage for a new Lead Process we've built for our company. To properly test this deployment, I need a list of Leads to show as being Inserted by our Pardot API connector user, and I need to run them through the default lead assignment rules. Since Apex doesn't run assignment rules by default, I'm trying to use the Database.insert(List<SObject>,APEX_OBJECT DmlOptions); constructor to insert the records, passing a DMLOptions Object where AssignmentRuleHeader.useDefaultRule = true

Database.DMLOptions dml = new Database.DMLOptions();
dml.AssignmentRuleHeader.useDefaultRule = true;

I have wrapped the Lead creation and the Insert in a System.runAs(pardotUser) block, and the @TestSetup method doesn't throw any errors when I run the tests:

    List<Lead> pardotLeads = new List<Lead>();
        Integer idx = 0;
        For(Integer i = 0;i < 20;i++){
            Lead l = new Lead(Company = 'Starting Lead ' + i,
                              FirstName = 'Test ' + i,
                              LastName = 'Test ' + i,
                              CountryCode = 'US');
            if(math.mod(idx,2) == 0){
                l.Unit_Type__c = 'Single Location';
                l.Unit_Type__c = 'Multi Location';
        System.debug('Pardot Leads: ' + pardotLeads);

However, when I use Database.insert(pardotLeads,dml), all of my other test classes that query for these records and assert that the list size is 20 all fail, indicating that the list size is 0.

    private static void LeadAcceptance_TEST() {
        List<Lead> leadList = [SELECT Id,FirstName,OwnerId,First_Assignment_Date__c,Last_Assignment_Date__c,First_Acceptance_Date__c,Last_Acceptance_Date__c FROM Lead WHERE Company LIKE 'Starting Lead %'];
        System.assertEquals(20,leadList.size(),'Should Be 20 Leads in List At Start'); // FAILS HERE

If I remove the Database.insert(pardotLeads,dml) and replace it with a Database.insert(pardotLeads) without the DML argument, my test classes all correctly find the 20 records, but then some of my test classes fail in other ways because the records aren't being run through Lead Assignment Rules.

I thought this may be related to Org-Wide Defaults, since our Lead object IS Private, but I looked at the Sharing Settings and we have a rule that shares all Leads from ALL Internal Users with ALL Internal Users as read only. Just to be extra safe, I even changed the OWD in my Sandbox back to Public Read/Write and ran them again - same issue.

I also logged in as the Pardot Connector User manually and tested by inserting a lead manually, and it worked/assigned as expected. What am I missing here? Why can't my test classes see any leads when I insert them using this constructor?

  • It sounds like whatever combination of dmlOptions along with the @TestSetup are causing an issue and not actually creating the records. When you say you "tested by inserting a lead manually", did you test with the Database.insert(pardotLeads,dml); using the dml options as well? I would also try doing the record inserts within the test method itself, just to see if you can single out @TestSetup as contributing to the issue – Brian Miller Apr 1 at 21:34
  • Thanks for your comment Brian, @TestSetup does definitely seem to be contributing here, as I have another test class that does the DML and Insert all within 1 test and that passes while the others fail. I tried manually inserting via the New Lead UI in Salesforce, but did not try to run the Database.insert through ExecAnon with the DML so I'll try that. I'm going to keep plugging away, I think I have a way to workaround it if its truly a TestSetup issue by inserting without DMLOptions, then later re-querying in each class and updating using DMLOptions, but it'll be uglier so i'm avoiding it. – Morgan Marchese Apr 1 at 21:43

Your Answer

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

Browse other questions tagged or ask your own question.