0

I have a visualforce page where I select multiple ids. Based on my selection I start a flow. I dont know how to simulate the selection in my testclass.

Visualforce

<apex:page standardController="cxsrec__cxsCandidate__c" recordSetVar="Candidate" extensions="Ya_ListToString">
    <apex:form >
        <apex:pageBlock title="mailing" mode="edit">
            <apex:pageMessages />
            <apex:pageBlockTable value="{!selected}" var="cand">
                <apex:column value="{!cand.name}"/>
                <apex:column headerValue="Id"/>
            </apex:pageBlockTable>
        </apex:pageBlock>
        <apex:commandButton value="Sendemail" action="{!returnIds}"/>
    </apex:form>
</apex:page>

Apex

public class Ya_ListToString{
    public string selectedIDs;
    public string returnstringIDs {get; set;}   
    public Flow.Interview.Persoon_toevoegen_aan_mailing_in_Bulk myFlow {get; set;}
    ApexPages.StandardSetController setCon;
    public Ya_ListToString(ApexPages.StandardSetController controller)
    {
        setCon = controller;
    }
    public pageReference returnIds() {
        // do something with the selected records
        for ( cxsrec__cxsCandidate__c candidate : (cxsrec__cxsCandidate__c[])setCon.getSelected())
        {
            String CandId = candidate.id;    
            selectedIDs += (CandId.substring(0,CandId.length()-3));
        }
        returnstringIDs = selectedIDs.replace('null','');
        system.debug('returnstringIDs: '+returnstringIDs);
        PageReference retURL = new PageReference('/apex/startflow');
        retURL.getParameters().put('returnstringIDs', returnstringIDs);
        retURL.setRedirect(true);
        return retURL;
    }
}

Unittest

@istest
public class Ya_ListToStringTest {
    static testmethod void listToStringTest(){ 
        //Testdata
        cxsrec__cxsCandidate__c cand = new cxsrec__cxsCandidate__c();
        cand.cxsrec__First_name__c = 'CandFirst';
        cand.cxsrec__Last_name__c = 'CandLast';
        insert cand;
        string candId = cand.Id;

        PageReference pageRef = Page.Ya_MultiselectIds;
        Test.setCurrentPage(pageRef);
        pageRef.getParameters().put('returnIds',candId.substring(0,candId.length()-3));

        ApexPages.StandardController sc = new ApexPages.standardController(cand);
        System.assertNotEquals(null, string.valueOf(pageRef));        
    }  
}

Update

@istest
public class Ya_ListToStringTest {
    static testmethod void listToStringTest(){ 
        //Testdata
        cxsrec__cxsCandidate__c cand = new cxsrec__cxsCandidate__c();
        cand.cxsrec__First_name__c = 'CandFirst';
        cand.cxsrec__Last_name__c = 'CandLast';
        insert cand;
        string candId = cand.Id;

        List<cxsrec__cxsCandidate__c> candList = new List<cxsrec__cxsCandidate__c>();
        candList.add(cand);
        Update CandList;

        PageReference pageRef = Page.Ya_MultiselectIds;
        Test.setCurrentPage(pageRef);
        pageRef.getParameters().put('returnIds',candId.substring(0,candId.length()-3));

        ApexPages.StandardController sc = new ApexPages.standardController(candList);
        sc.setSelected(candList);        

        System.assertNotEquals(null, string.valueOf(pageRef));        
    }  
}
2

Use the setSelected method after you instantiate the controller

List<Object> objects = TestHelper.getObjects(50);

    ApexPages.StandardSetController stdController = new ApexPages.StandardSetController(objects);
    stdController.setSelected(objects);
  • I added you suggestion but get: Method does not exist or incorrect signature: void setSelected(cxsrec__cxsCandidate__c) from the type ApexPages.StandardController.ApexPages.StandardController sc = new ApexPages.standardController(cand); sc.setSelected(cand); – Thomas Jun 11 at 14:27
  • You need to pass a list of records to both the controller and the setSelected method. is "cand" a list of records? – CommonCoreTawan Jun 11 at 14:30
  • added as list (see update) but get:Constructor not defined: [ApexPages.StandardController].<Constructor>(List<cxsrec__cxsCandidate__c>).Method does not exist or incorrect signature: void setSelected(List<cxsrec__cxsCandidate__c>) from the type ApexPages.StandardController – Thomas Jun 11 at 15:30
  • It's not standardController, it's StandardSETController – CommonCoreTawan Jun 11 at 15:44
  • 1
    oops missed that one Thanks!. – Thomas Jun 11 at 16:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.