0

I am writing a test for some code I wrote for a trigger. I create all of the data in the test, and have SeeAllData = false. The test class invokes the trigger, but a query in the trigger helper code returns 0 results. I don't understand why, with SeeAllData = false I was lead to believe my code could find the test data in the query during the run.

Here is my test code, so far -

static testMethod void createSubscriptionsFromSubDetails() {

    Test.startTest();

    Product2 p = new Product2();
    p.name = 'Test Product';
    p.Family = 'Purchase Option';
    p.ProductCode = 'HW-0101';

    insert p;

    Account acct = new Account(Name='Test Account');
    acct.Last_Subscription_Detail_Update__c = DateTime.now();
    insert acct;

    List<SubscriptionDetails__c> subDetails = new List<SubscriptionDetails__c>();
    for(integer i = 1; i <= 5; i++)
    {
        SubscriptionDetails__c s = new SubscriptionDetails__c();
        s.Name = 'Test Sub Detail' + i;
        s.AccountId__c = acct.ID;
        s.StartDate__c = Date.newInstance(2020, 1, 9);
        s.EndDate__c = Date.newInstance(2023, 1, 9);   
        s.Price__c = 500;
        s.SalesforceProductCode__c = p.productcode;
        subDetails.add(s);
    }

    insert subDetails;

    //Update the account so the trigger fires

    Account insertedAccount = [Select Name, OwnerId from Account where Id =: acct.Id];

    insertedAccount.Last_Subscription_Detail_Update__c = DateTime.now();
    update insertedAccount;

    system.debug('test setup : ' + insertedAccount.ID);


    Test.stopTest();
} 

This eventually ends up in some code in a separate class doing this:

    public void createContracts(){

    subscriptionDetails = [SELECT name, ApplicationFeatureName__c, id, SalesforceProductCode__c, 
                           StartDate__c, EndDate__c, Price__c
                           FROM SubscriptionDetails__c 
                           WHERE ApplicationFeatureName__c != ''
                           AND AccountId__c =: accountID]; 

    if(subscriptionDetails.size() > 0)
    {
         ......
    }

But I see that subscriptionDetails is returning 0 results, so it doesn't have access to this data. (The account ID is correct, I debugged that)

Can anyone explain why? How do I set this up so my other code getting called during the test can see the test data?

Thanks

2
  • 2
    The first filter condition applicationfeaturename__c!="" Doesn't look like you are setting in anywhere in test,did you try without it did you got any data then?
    – User6670
    Commented Feb 13, 2020 at 2:11
  • Crap, that was it. Thank you! I feel so dumb, I realize I don't need that check in the query anymore so I just removed it and it worked.
    – user78939
    Commented Feb 15, 2020 at 2:42

1 Answer 1

0

When you are inserting the account

Account acct = new Account(Name='Test Account');
acct.Last_Subscription_Detail_Update__c = DateTime.now();
insert acct;

you have populated the Last_Subscription_Detail_Update__c with Datetime.now(). Now to fire the update trigger you are updating the same field with same value. So salesforce is not detecting any update hence trigger is not firing . Try updating some other field like description or same field with some other value like DateTime.now().addDays(1)
//Update the account so the trigger fires

Account insertedAccount = [Select Name, OwnerId from Account where Id =: acct.Id];

insertedAccount.Last_Subscription_Detail_Update__c = DateTime.now().addDays(1);
update insertedAccount;
1
  • this isn't true - an Update DML will always cause the beforeUpdate trigger to fire regardless of whether the data has changed or not
    – cropredy
    Commented Feb 13, 2020 at 18:41

You must log in to answer this question.

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