Salesforce: Source Intent feature step 4- Session Activity purge

Modified on Fri, 22 Dec 2023 at 10:28 AM

Steps for configuring Session Activity purging batch:


With the Source Intent feature in Salesforce, Buyer Discovery Session Activity (user traffic data) records are inserted into the Salesforce Org daily. 


In order to avoid the Session Activity records from building up and taking too much storage in the Salesforce Org, a Scheduled Apex Job can be run to delete the Session Activity records that are older than 60 days.


Here are the Steps:


Creating the Apex Class


  • In your Salesforce Org, click the gear icon, then Developer Console



  • In the Developer Console, click File, then New, then Apex Class, and name the Apex class: PurgeOldRecords



  • In the text window, copy in the below Apex code:


global class PurgeOldRecords implements Schedulable, Database.Batchable<SObject> {
    global void execute(SchedulableContext sc) {
        PurgeOldRecords BatchJob = new PurgeOldRecords();
        Database.executeBatch(batchJob);
    }
    global Database.QueryLocator start(Database.BatchableContext bc) {
        Date cutoffDate = Date.today().addDays(-60);
        return Database.getQueryLocator('SELECT Id FROM BD_Session_Activity__c WHERE CreatedDate < :cutoffDate LIMIT 500000');
    }
    global void execute(Database.BatchableContext bc, List<BD_Session_Activity__c> records) {
        delete records;
    }
    global void finish(Database.BatchableContext bc) {
    }
}




  • Save the code with ctrl + S


Scheduling the Apex Class


  • In your Salesforce Org, Click the gear icon, then Setup



  • In Setup, find the Apex Jobs tab



  • In Apex Jobs tab, click Schedule Apex



  • Select the PurgeOldRecords Apex Class, and configure the schedule to run daily



  • Click Save


  • You can check the progress of the Scheduled job under the Scheduled Apex tab in Setup





Was this article helpful?

That’s Great!

Thank you for your feedback

Sorry! We couldn't be helpful

Thank you for your feedback

Let us know how can we improve this article!

Select atleast one of the reasons
CAPTCHA verification is required.

Feedback sent

We appreciate your effort and will try to fix the article