function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Marco PinderMarco Pinder 

Edit code to only action Active Contacts



Could someone please assist me with the below Apex Class and advise what I would need to change in order to only have it action Active Contacts, rather than Active and Inactive ones as it does in it's current form.






global class TrackContactUpdates implements Database.Batchable<Sobject> {
    global Database.QueryLocator start(Database.BatchableContext bc) {
        return Database.getQueryLocator('select id from contact' + (Test.isRunningTest()?' order by createddate desc limit 1':''));
    global void execute(Database.BatchableContext bc,List<Contact> contacts) {
        Map<id,Contact> contactRecords = new Map<Id,Contact>(contacts);
        for(Contact c:[select id,
                        (select id,createddate from contractcontactroles order by createddate desc limit 1),
                        (select id,createddate from opportunitycontactroles order by createddate desc limit 1),
                        (select id,createddate from accountcontactroles order by createddate desc limit 1)
                       from contact where id in :contacts]) {
            contactRecords.get( = c.contractcontactroles.isempty()?null:c.contractcontactroles[0].createddate;
            contactRecords.get( = c.opportunitycontactroles.isempty()?null:c.opportunitycontactroles[0].createddate;
            contactRecords.get( = c.accountcontactroles.isempty()?null:c.accountcontactroles[0].createddate;
        update contactRecords.values();
    global void finish(Database.BatchableContext bc) {
    private static testMethod void test() {
        TrackContactUpdates t = new TrackContactUpdates();
        Account a = new Account(Name='Test');
        insert a;
        Contact c = new Contact(Firstname='Test',LastName='Test',AccountId=a.Id);
        insert c;
        Opportunity o = new Opportunity(Name='Test',,amount=1.00,stagename='Closed Won',;
        insert o;
        Contract ct = new Contract(Name='Test',,Status='Draft',Sales_Region__c='UK');
        insert ct;
        AccountContactRole acr = new AccountContactRole(Role='Vendor',,,isprimary=false);
        insert acr;
        acr = [select id,createddate from accountcontactrole where id =];
        OpportunityContactRole ocr = new OpportunityContactRole(Role='Vendor',,,isprimary=false);
        insert ocr;
        ocr = [select id,createddate from opportunitycontactrole where id =];
        ContractContactRole ccr = new ContractContactRole(Role='Vendor',,;
        insert ccr;
        ccr = [select id,createddate from contractcontactrole where id =];
        TrackContactUpdates tcu = new TrackContactUpdates();
        c = [select id,last_contract_role_Date__c,last_account_role_date__c,last_opportunity_role_date__c from contact where id =];