How to give SOQL limit size dynamically for retrieving records using Apex in Salesforce?

How to give SOQL limit size dynamically for retrieving records using Apex in Salesforce?

Sample Code:


Visualforce page:

<apex:page controller=”Sample”>
<apex:form >
    <apex:pageblock >
        <apex:pageBlockTable value=”{!MemList}” var=”M”>
            <apex:column value=”{!M.Name}”/>
        </apex:pageBlockTable>
    </apex:pageblock>
</apex:form>
</apex:page>


Apex Controller:

public class Sample {  
    Integer n = 5;
    public List<Member__c> MemList {get;set;}
    public Sample() {
    /* We can write like this too
        MemList = [SELECT Name FROM Member__c LIMIT : n];
    */
        String Soql = ‘SELECT Name FROM Member__c LIMIT ‘ + n;
        MemList = Database.query(Soql);
    }           
}

Output:

Leave a Reply