0

In my batch class, there is a condition that checks

    if(Opp.lastactivitydate != null || (Opp.LastModifiedDate > Opp.CreatedDate && Date.ValueOf(Opp.LastModifiedDate) <= Opp.renew_opp_reminder__c))

I want to know how to create test data for an opportunity that has LastActivityDate != null

I have tried:

            //setup
            Account accttest = new Account(
                Name                    = 'TestAcc',
                Company_English__c      = 'TestComp',
                DBD_ID__c               = '12345',
                Tax_ID__c               = '-'
            );
            insert accttest;
            
            Opportunity opptest = new Opportunity(
                Name                            = 'Test opp ',
                CloseDate                       = System.today().addMonths(1),
                Policy_Effective_Date__c        = System.today(),
                Renew_Opp_Reminder__c           = System.today(),
                AccountId                       = accttest.Id,
                StageName                       = 'Qualification',
                Renew_From_Lost__c              = true,
                Catch_Up_Noti_Send__c           = true
            );
            insert opptest;

I can't add LastActivityDate = System.today() because the field is not writeable. Please help me.

4
  • Please refer this help article: help.salesforce.com/s/articleView?id=000386257&type=1
    – jagmohan
    Commented Feb 8, 2023 at 6:39
  • I have just tried the following article, and I can now set the LastActivityDate, but when I run the test, it's still not getting into the condition. Do you have any suggestions? Thanks
    – sirps
    Commented Feb 8, 2023 at 7:48
  • You might have to check what values you are using for LastModifiedDate, CreatedDate and renew_opp_reminder__c fields.
    – jagmohan
    Commented Feb 8, 2023 at 7:59
  • I debug the if condition with those values and get "true" as a result, but the test still does not cover the condition.
    – sirps
    Commented Feb 8, 2023 at 9:05

1 Answer 1

0

Opportunity.LastActivityDate is set when the Opportunity has a completed Task or an event due date.

LastActivityDate

Type date

Properties Filter, Group, Nillable, Sort

Description Value is one of the following, whichever is the most recent:

  • Due date of the most recent event logged against the record.

  • Due date of the most recently closed task associated with the record.

So, your test mock up needs to insert a completed task after inserting the Opportunity

insert new Task(
    ActivityDate = Date.today(),
    Subject = 'foo',
    Status = 'Completed',
    WhatId = oppTest.Id
);

Bonus content

All that being said, if the test you care about is in your batchable execute() method and you don't need to do DML on the Opportunity, you can unit test just the execute() method by passing in a list of records as SObjects. These SObjects can be constructed with audit fields, autonumber fields, formula fields, etc. using the JSON.serialize/deserialize method. There are frameworks that do this, I like Matt Addy's SObjectFabricator

Even better is to look into the Unit of Work pattern for advanced mocking options once you develop your Apex skills more

1
  • This is working. Thank you so much!
    – sirps
    Commented Feb 9, 2023 at 1:56

You must log in to answer this question.

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