+ Start a Discussion
Stefaan Somers 12Stefaan Somers 12 

How to test controller extension with selected records without error Modified records?

Hi,

I have the following extension controller :
/**
 * Created by stefaans on 4/06/2019.
 */

public class BatchUpdatePositionsController {
    ApexPages.StandardSetController ctrl = null;
    public List<cxsrec__cxsPosition__c> positionsToUpdate = new List<cxsrec__cxsPosition__c>();
    public String jobId='';

    public BatchUpdatePositionsController(ApexPages.StandardSetController controller){
        if (ctrl ==null){
            ctrl = controller;
            if (!Test.isRunningTest()) ctrl.addFields(new List<String>{'RecordType.DeveloperName','cxsrec__Status__c','Company__r.name'});
            List<cxsrec__cxsPosition__c> positions = (List<cxsrec__cxsPosition__c>) ctrl.getSelected();
            System.debug('Number of requests to update :' + positions.size());
            for (cxsrec__cxsPosition__c position : positions) {
                if (position.RecordType.DeveloperName == 'XXX' && position.cxsrec__Status__c =='Open'){
                    position.cxsrec__Status__c              =   'XXX';
                    position.Channel_Vivaldis_website__c    =   false;
                    positionsToUpdate.add(position);

            }
                ctrl.setPageSize(positionsToUpdate.size());

            }
        }
    }

    public pageReference updateRequests(){
         AsyncBatchUpdate updateJob = new AsyncBatchUpdate(this.positionsToUpdate);
        ID jobID = System.enqueueJob(updateJob);
        System.debug('positionsToUpdate : ' + positionsToUpdate);
        ApexPages.addMessage(new ApexPages.message(ApexPages.severity.INFO, 'Aanvragen worden succesvol aangepast'));

        return null;
    }



}


This code just runs fine when I test it in the UI.
 

So now I've written the following test-class.
 

/**
 * Created by stefaans on 5/06/2019.
 */
@isTest
public class BatchUpdatePositionsControllerTest {
    static BatchUpdatePositionsController ext;

    @testSetup
    static void setup(){
        cxsrec__cxsLanguage__c lang = new cxsrec__cxsLanguage__c(Name='Nederlands',cxsrec__Code__c = 'nl_NL');
        insert lang;
        Account acct = Test_datafactory.createAccount('testAccount', 'Straat', '1', '1','3300','Tienen','xxxx', 'A01','memo');
        cxsrec__cxsFunction__c jobTemplate = Test_datafactory.createFunction('Consultant');
        Contact contact = Test_datafactory.createContact(acct, 'Jaspers');
        cxsrec__cxsPosition__c job = Test_datafactory.createJob(jobTemplate, acct, contact, 'Consultant', 'Open', 'A01');
        job.cxsrec__Language_rel__c = lang.Id;
        update job;
        cxsrec__cxsPosition__c job2 = Test_datafactory.createJob(jobTemplate, acct, contact, 'Consultant', 'Open', 'A01');
        job.cxsrec__Language_rel__c = lang.Id;
        update job2;

    }


    @isTest
    private static void updateRequests(){
        List<cxsrec__cxsPosition__c> jobs = [select id, name, cxsrec__Status__c, RecordType.DeveloperName from cxsrec__cxsPosition__c];
        Test.startTest();
        ApexPages.StandardSetController con = new ApexPages.StandardSetController(jobs);
        con.setSelected(jobs);
        ext = new BatchUpdatePositionsController(con);
        ext.updateRequests();
        Test.stopTest();
        System.assertNotEquals('',ext.jobId);
    }


}

But whe I run this test-class, I always get the error :
System.VisualforceException: Modified rows exist in the records collection!
External entry point

Any idea on how I can resolve this?