1

Please can you check my code and tell me where I have gone wrong? Object Name - course_master__c, multipicklist field name - course_name__c.

I want to create a SEARCH button based on the multipicklist field value and I want to view the related records.

APEX code:

public class customSearchCourseController{

  public String cname{get;set;}
  public List<Course_Master__c> c {get;set;}
  
  public customSearchCourseController(){
    c = new List<Course_Master__c>();
  }
  
  public void search(){

    /* commenting this section
    List<String> stringList = new List<String>{'Angular', 'AWS', 'DB2', 'Dotnet', 'Hadoop', 'Java', 'Javascript', 'JS', 'Oracle', 'PHP', 'Salesforce', 'SQL', 'Tableau'};
    
    String result = '\'' + String.join(stringList, '\',\'')+'\'';
    */
   
    String query = 'SELECT Id,Course_Name__c FROM Course_Master__c WHERE course_name__c INCLUDES (' + cname + ')';
    c = Database.query(query);
  } 
    
  public void clear(){
    c.clear();
  }
}

Visualforce Page:

<apex:page controller="customSearchCourseController">
<apex:form >
<apex:inputText value="{!cname}"/>

<apex:commandButton value="Search Button" action="{!search}"/>
 <apex:commandButton value="Clear Records" action="{!clear}"/>

<apex:pageBlock > 
<apex:pageBlockTable value="{!c}" var="CVAR">
<apex:column Value="{!CVAR.Course_Name__c}"/>

</apex:pageBlockTable>
</apex:pageBlock>
</apex:form>
</apex:page>
0

There is an significant limitation when using INCLUDES queries - they don't work with bind variables. You need to either statically define them or build a dynamic SOQL query.

String query = 'SELECT Id,course_name__c 
                FROM Course_Master__c 
                WHERE course_name__c INCLUDES (' + result + ')';
c = Database.query(query);

If you are having trouble debugging this, make another variable and use it to show the output of the course master list as JSON. eg:

String courseMastersJSON {get;set;}

Then after the query, do this:

courseMasterJSON = JSON.serialize(c);

And in the VisualForce, display with:

<apex:outputText value="{!courseMasterJSON}">

If this fails, you have other issues that really should be addressed in another question.

5
  • i corrected my soql code but in the VFP when i lookup for a particular course (ex Java) i am getting all records for all values and not alone for Java. public void search(){ List<String> stringList = new List<String>{'Angular', 'AWS', 'DB2', 'Dotnet', 'Hadoop', 'Java', 'Javascript', 'JS', 'Oracle', 'PHP', 'Salesforce', 'SQL', 'Tableau'}; String result = '\'' + String.join(stringList, '\',\'')+'\''; String query = 'SELECT Id,course_name__c FROM Course_Master__c WHERE course_name__c INCLUDES (' + result + ')'; c = Database.query(query);} public void clear(){ c.clear();}} Jul 24 '21 at 11:19
  • You aren't actually putting cname anywhere in the search. it looks like cname is just a single value, so change the last line of the dynamic query to WHERE course_name__c INCLUDES (' + cname + ')'; Jul 25 '21 at 2:49
  • still no luck ; i am still able to see records of all values of the Multipicklist field course_name__c ; i dont know what else can be done. could you please assist Jul 26 '21 at 16:31
  • yes i have udpated my code in the question. kindly assist Jul 27 '21 at 6:23
  • Do you know how to use the Debug log? If you open the developer console and run the search, you'll be able to see the log output of this code. Another thing would be to make a new variable and show it on the screen. Description added to my answer. Jul 27 '21 at 6:48

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