0

I implemented a trigger functionality (insert, update on leads), that applies the the territory-management rules/assignments functionality from accounts to leads (e.g. leads can be assigned a new owner with the rules and users in our standard territory-model).

The functionality works perfectly when using the UI and the API (i.e. create leads with the dataloader), but it does not work properly in my unit tests. If my tests are seeAllData=false, they fail, if I set seeAllData=true, they succeed. However, I can not reproduce why.

The following test-code fails with seeAllData=false and succeeds with seeAllData=true

@isTest
static void i_Lead_RetailLeadValidAddress_assignedToTerritoryRetailManager() {

    // SETUP
    // insert lead with munich Address
    List<Lead> ls = new List<Lead>();
    for (Integer i = 0; i < BULK_NUMBER; i++) {
        ls.add(new Lead(LastName = 'TestLead '+String.valueOf(i),
                        Company = 'testLeadCompany '+String.valueOf(i),
                        City = 'München',
                        CountryCode = 'DE',
                        Type__c = 'Partner',
                        AssignWithTerritoryRules__c = true,
                        LanguageCultureCode__c = 'de-DE'));
    }

    // ACTION
    Database.SaveResult[] srs = Database.insert(ls);

    // VERIFY
    // all leads are assigned to retail project manager of DE South
    User retailManager = [SELECT Id, Name, Username FROM User WHERE Id IN (SELECT UserId FROM UserTerritory2Association WHERE Territory2.Name = 'DE South' AND RoleInTerritory2 = 'Retail Sales') LIMIT 1];
    Map<Id, Lead> lsMap = new Map<Id, Lead>([SELECT OwnerId, Owner.Name FROM Lead]);
    for (Database.SaveResult sr : srs) {
        System.assert(sr.isSuccess());
        System.assertEquals(retailManager.Id, lsMap.get(sr.getId()).OwnerId, 'Expected User: '+retailManager+' Actual User: '+lsMap.get(sr.getId()));
    }

}

The business logic is as follows: We have two territory-managers per region, one for retail and one for projects. If Type__c = Partner I expect the Retail Sales Manager to be assigned.

I did some debugging / logs and found out the following:

  • Tests can access all records for UserTerritory2Association, Territory2 and User, even if seeAllData=false (e.g. I query the records in a test method)
  • The functionality, however, can not access these records, when it is invoked in a seeAllData=false context and can only access it in a seeAllData=true context

How can I work-around it? Setting up users and territory-associations manually interfere with the current model and the associations. Using the current model will make it pretty hard to deploy that functionality, since I will have to hard-code Territories and their associations (in my assertions) and it will make regression testing dependend on the currently active model (and even the assumptions of the existing regions, etc).

I am leaning toward deleting all associations in the testSetup and manually setting test-users and associations. That will at least make my tests robust against new sales-reps in our standard model but will not solve the issue of new models (since I hard-code the assumptions of regions and their rules)

Does anyone have a better idea?

  • If you could give a more concise and complete version of your code, it would be easier to understand what's happening. Specifically, there seems to be a lot of extraneous code here, but it's lacking any information about Territory Assignment. – Charles Koppelman Jan 8 at 17:07
  • Since Territories do not assign owners, I am going to assume that the problem lies elsewhere. – Charles Koppelman Jan 8 at 17:07

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.