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
RajanRajan 

Dynamic SOQL

Hi friends,

I've to implement dynamic SOQL in my class. I have a class where I have written so many Remote action methods and in each method I have a SOQL query with different where clause and fetching data on the basis of multiple values for each methods. Since I have so many methods so SOQL queries count is also so many. I have to write a dynamic SOQL query so that I can call same query n every methods on the basis of where clause. Any suggesion or sample code for this requirement?

Thanks in Advance
Rajan
Best Answer chosen by Rajan
RajanRajan
Solved by myself

All Answers

Rahul KumarRahul Kumar (Salesforce Developers) 
Hi Rajan,
  • Dynamic SOQL refers to the creation of a SOQL string at runtime with Apex code. Dynamic SOQL enables you to create more flexible applications. For example, you can create a search based on input from an end user or update records with varying field names.
  • To create a dynamic SOQL query at runtime, use the database query method, in one of the following ways.
  • Return a single s Object when the query returns a single record:
sObject s = Database.query(string_limit_1);
 
<apex:page controller="Soql_Example1">
    <apex:form >
      <apex:pageBlock title="Accounts">
          <apex:pageBlockButtons location="top">
              <apex:selectlist size="1" value="{!selected}">
                  <apex:actionSupport event="onchange" action="{!dynamicQuery}" rerender="pb"/>
              <apex:selectOption itemLabel="-None-" itemValue="none"/>
                             <apex:selectOption itemLabel="5" itemValue="5"/>
                             <apex:selectOption itemLabel="10" itemValue="10"/>
                             <apex:selectOption itemLabel="15" itemValue="15"/>
                             <apex:selectOption itemLabel="20" itemValue="20"/>
              </apex:selectlist>
              <apex:inputText size="4" value="{!rows}"/>
          <apex:commandButton value="limit" action="{!setLimit}" rerender="pb"/>
                        <apex:commandButton value="Dynamic_limit" action="{!dynamicSetLimit}" rerender="pb"/>

          </apex:pageBlockButtons>

          <apex:pageblocktable value="{!accs1}" var="a" id="pb">
             <apex:column value="{!a.name}"/>       
                       <apex:column value="{!a.industry}"/>       
          </apex:pageblocktable>
        </apex:pageBlock>
    </apex:form>
</apex:page>
 
public class Soql_Example1 {
    public list<Account> accs1 {set;get;}
    public  integer rows       {set;get;}
    public string selected     {set;get;}
    
    Public Soql_Example1(){
        accs1=[select id,name,industry from account];
    }
    Public Void setLimit(){
        accs1=[select id,name,industry from account limit 10];  
    }//Dymamic Query
    public Void dynamicSetLimit(){
        accs1=[select id,name,industry from account limit :rows];
    }
    public void dynamicQuery(){
        if(selected!='none'){
        string query = 'select id, name,phone,Industry from Account limit ' +selected;
         accs1=Database.query(query);
        }else{
            accs1=new list<Account>();
        }
    }
}

Hope it will be helpful.

Please mark it as best answer if the information is informative.so that question is removed from an unanswered question and appear as a proper solution.

Thanks
Rahul Kumar
RajanRajan
Thanks Rahul for the reply.
I'm doing the same but not giving any result. My createria is as below
If feedback = Invalid the all Records with invalid data in feedback field should display
If feedback = Active or blank then all records escept Invalid should display.
My code is as below:
***************************
// *****recommendationfields+accountFields+contactFields+rationaleFields+cheatSheet***** is field varriable where I m passing all fields from different 5 objects


    if(feedback == 'Invalid'){
        string query = recommendationfields+accountFields+contactFields+rationaleFields+cheatSheet+ ' '+ 'FROM Recommendation__c'+' ' +'WHERE Feedback__c =:'+feedback+' '+'order by Opportunity_Score__c DESC';
        system.debug('Query:' +query);
        List<Recommendation__c> recList = database.query(query);
        system.debug('RecData:'+recList);
    } Else if(feedback == 'Active'){
        string query = recommendationfields+accountFields+contactFields+rationaleFields+cheatSheet+ ' '+ 'FROM Recommendation__c'+' ' +'WHERE Feedback__c =:'+feedback+' '+'order by Opportunity_Score__c DESC';
        system.debug('Query:' +query);
        List<Recommendation__c> recList = database.query(query);
        system.debug('RecData:'+recList);
    }
RajanRajan
Solved by myself
This was selected as the best answer