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

Hi,

 

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.

 

Thanks,

 

Marco

 

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.id).last_contract_role_date__c = c.contractcontactroles.isempty()?null:c.contractcontactroles[0].createddate;
            contactRecords.get(c.id).last_opportunity_role_date__c = c.opportunitycontactroles.isempty()?null:c.opportunitycontactroles[0].createddate;
            contactRecords.get(c.id).last_account_role_date__c = 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',CloseDate=System.today(),amount=1.00,stagename='Closed Won',AccountId=a.id);
        insert o;
        Contract ct = new Contract(Name='Test',AccountId=a.id,Status='Draft',Sales_Region__c='UK');
        insert ct;
        AccountContactRole acr = new AccountContactRole(Role='Vendor',ContactId=c.id,AccountId=a.id,isprimary=false);
        insert acr;
        acr = [select id,createddate from accountcontactrole where id = :acr.id];
        OpportunityContactRole ocr = new OpportunityContactRole(Role='Vendor',ContactId=c.id,OpportunityId=o.id,isprimary=false);
        insert ocr;
        ocr = [select id,createddate from opportunitycontactrole where id = :ocr.id];
        ContractContactRole ccr = new ContractContactRole(Role='Vendor',ContactId=c.id,ContractId=ct.id);
        insert ccr;
        ccr = [select id,createddate from contractcontactrole where id = :ccr.id];
        Test.startTest();
        TrackContactUpdates tcu = new TrackContactUpdates();
        Database.executeBatch(tcu);
        Test.stopTest();
        c = [select id,last_contract_role_Date__c,last_account_role_date__c,last_opportunity_role_date__c from contact where id = :c.id];
        system.assertEquals(ccr.createddate,c.last_contract_role_Date__c);
        system.assertEquals(ocr.createddate,c.last_opportunity_role_Date__c);
        system.assertEquals(acr.createddate,c.last_account_role_Date__c);
    }
}