Play Games

Search This Blog

Friday, September 25, 2020

Dynamic soql to query records with a variable to define limit in apex salesforce - Salesforce Globe For You

 Dynamic soql to query records with a variable to define limit in apex salesforce  - Salesforce Globe For You 

Solution:Assume iLimit is a variable to set limit.

The query will be like this

[Select id,name from Lead limit :iLimit]; 

Note: keep space after limit and add ':'

Example:

Apex class:LimitVariableController

public class LimitVariableController {

    public static void getLeads(integer iLimit) {

        List<Lead> lstLead = new List<Lead>();

        lstLead = [Select id,name from Lead limit :iLimit];

        system.debug('Leads List:'+lstLead);

    }

}

When we run the following line from anonymous window of developer console,

LimitVariableController.getLeads(2);

the output will be like this.





No comments:

Post a Comment